mirror of
https://github.com/PretendoNetwork/BOSS.git
synced 2026-07-08 21:13:56 -05:00
chore: remove usused code
This commit is contained in:
parent
49b45e7df8
commit
6998b64164
|
|
@ -1,4 +0,0 @@
|
|||
// ! Do not move this file, it must be in the root of the project for __dirname to resolve correctly
|
||||
// * Node.js does not have a built-in way to get the root directory of the project, so we need to set it manually
|
||||
// * When using a bundler, __dirname will not work as expected in nested files so we need a consistent way to get the root directory
|
||||
export const __appRoot = __dirname;
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
import { Status, ServerError } from 'nice-grpc';
|
||||
import { encryptWiiU } from '@pretendonetwork/boss-crypto';
|
||||
import { isValidCountryCode, isValidFileNotifyCondition, isValidFileType, isValidLanguage, md5, uploadCDNFile } from '@/util';
|
||||
import { isValidCountryCode, isValidFileNotifyCondition, isValidFileType, isValidLanguage, md5 } from '@/util';
|
||||
import { getTask, getTaskFile } from '@/database';
|
||||
import { File } from '@/models/file';
|
||||
import { config } from '@/config-manager';
|
||||
import { uploadCdnFile } from '@/cdn';
|
||||
import type { CallContext } from 'nice-grpc';
|
||||
import type { AuthenticationCallContextExt } from '@/services/grpc/boss/middleware/authentication-middleware';
|
||||
import type { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc';
|
||||
|
|
@ -112,7 +113,7 @@ export async function uploadFile(request: UploadFileRequest, context: CallContex
|
|||
// * upload key is not viable, as it is not always
|
||||
// * known during upload
|
||||
const key = `${bossAppID}/${taskID}/${contentHash}`;
|
||||
await uploadCDNFile(key, encryptedData);
|
||||
await uploadCdnFile('taskFile', key, encryptedData);
|
||||
} catch (error: unknown) {
|
||||
let message = 'Unknown file upload error';
|
||||
|
||||
|
|
|
|||
73
src/util.ts
73
src/util.ts
|
|
@ -1,37 +1,18 @@
|
|||
import crypto from 'node:crypto';
|
||||
import path from 'node:path';
|
||||
import fs from 'fs-extra';
|
||||
import { createChannel, createClient, Metadata } from 'nice-grpc';
|
||||
import { GetObjectCommand, PutObjectCommand, S3 } from '@aws-sdk/client-s3';
|
||||
import { AccountDefinition } from '@pretendonetwork/grpc/account/account_service';
|
||||
import { FriendsDefinition } from '@pretendonetwork/grpc/friends/friends_service';
|
||||
import { config, disabledFeatures } from '@/config-manager';
|
||||
import { config } from '@/config-manager';
|
||||
import { logger } from './logger';
|
||||
import type { FriendsClient } from '@pretendonetwork/grpc/friends/friends_service';
|
||||
import type { AccountClient } from '@pretendonetwork/grpc/account/account_service';
|
||||
import type { S3Client } from '@aws-sdk/client-s3';
|
||||
import type { GetNEXDataResponse } from '@pretendonetwork/grpc/account/get_nex_data_rpc';
|
||||
import type { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc';
|
||||
import type { GetUserFriendPIDsResponse } from '@pretendonetwork/grpc/friends/get_user_friend_pids_rpc';
|
||||
import type { NodeJsClient } from '@smithy/types';
|
||||
import type { Response } from 'express';
|
||||
import type { Readable } from 'node:stream';
|
||||
import type { Stats } from 'node:fs';
|
||||
|
||||
let s3: NodeJsClient<S3Client>;
|
||||
|
||||
if (!disabledFeatures.s3) {
|
||||
s3 = new S3({
|
||||
forcePathStyle: false,
|
||||
endpoint: config.cdn.s3.endpoint,
|
||||
region: config.cdn.s3.region,
|
||||
credentials: {
|
||||
accessKeyId: config.cdn.s3.key,
|
||||
secretAccessKey: config.cdn.s3.secret
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const gRPCAccountChannel = createChannel(`${config.grpc.account.address}:${config.grpc.account.port}`);
|
||||
const gRPCAccountClient: AccountClient = createClient(AccountDefinition, gRPCAccountChannel);
|
||||
|
||||
|
|
@ -167,55 +148,3 @@ export async function getFriends(pid: number): Promise<GetUserFriendPIDsResponse
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCDNFileStream(key: string): Promise<Readable | null> {
|
||||
try {
|
||||
if (disabledFeatures.s3) {
|
||||
return await getLocalCDNFile(key);
|
||||
} else {
|
||||
const response = await s3.send(new GetObjectCommand({
|
||||
Key: key,
|
||||
Bucket: config.cdn.s3.bucket
|
||||
}));
|
||||
|
||||
if (!response.Body) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.Body;
|
||||
}
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getLocalCDNFile(key: string): Promise<fs.ReadStream | null> {
|
||||
const filePath = path.join(config.cdn.disk_path, key);
|
||||
|
||||
if (await !fs.exists(filePath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return fs.createReadStream(filePath);
|
||||
}
|
||||
|
||||
export async function uploadCDNFile(key: string, data: Buffer): Promise<void> {
|
||||
if (disabledFeatures.s3) {
|
||||
await writeLocalCDNFile(key, data);
|
||||
} else {
|
||||
await s3.send(new PutObjectCommand({
|
||||
Key: key,
|
||||
Bucket: config.cdn.s3.bucket,
|
||||
Body: data,
|
||||
ACL: 'private'
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeLocalCDNFile(key: string, data: Buffer): Promise<void> {
|
||||
const filePath = path.join(config.cdn.disk_path, key);
|
||||
const folder = path.dirname(filePath);
|
||||
|
||||
await fs.ensureDir(folder);
|
||||
await fs.writeFile(filePath, data);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user