diff --git a/packages/backend/src/services/drive/add-file.ts b/packages/backend/src/services/drive/add-file.ts index ee49f6ae5..20ba24609 100644 --- a/packages/backend/src/services/drive/add-file.ts +++ b/packages/backend/src/services/drive/add-file.ts @@ -16,6 +16,7 @@ import { IRemoteUser, User } from '@/models/entities/user.js'; import { driveChart, perUserDriveChart, instanceChart } from '@/services/chart/index.js'; import { genId } from '@/misc/gen-id.js'; import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js'; +import { DriveFolder } from '@/models/entities/drive-folder.js'; import { deleteFile } from './delete-file.js'; import { GenerateVideoThumbnail } from './generate-video-thumbnail.js'; import { driveLogger } from './logger.js'; @@ -153,7 +154,10 @@ async function save(file: DriveFile, path: string, name: string, type: string, h * @param type Content-Type for original * @param generateWeb Generate webpublic or not */ -export async function generateAlts(path: string, type: string, generateWeb: boolean) { +export async function generateAlts(path: string, type: string, generateWeb: boolean): Promise<{ + webpublic: IImage | null; + thumbnail: IImage | null; +}> { if (type.startsWith('video/')) { try { const thumbnail = await GenerateVideoThumbnail(path); @@ -256,7 +260,7 @@ export async function generateAlts(path: string, type: string, generateWeb: bool /** * Upload to ObjectStorage */ -async function upload(key: string, stream: fs.ReadStream | Buffer, _type: string, filename?: string) { +async function upload(key: string, stream: fs.ReadStream | Buffer, _type: string, filename?: string): Promise { const type = (_type === 'image/apng') ? 'image/png' : (FILE_TYPE_BROWSERSAFE.includes(_type)) @@ -286,7 +290,7 @@ async function upload(key: string, stream: fs.ReadStream | Buffer, _type: string if (result) logger.debug(`Uploaded: ${result.Bucket}/${result.Key} => ${result.Location}`); } -async function deleteOldFile(user: IRemoteUser) { +async function deleteOldFile(user: IRemoteUser): Promise { const q = DriveFiles.createQueryBuilder('file') .where('file.userId = :userId', { userId: user.id }) .andWhere('file.isLink = FALSE'); @@ -387,7 +391,7 @@ export async function addFile({ } //#endregion - const fetchFolder = async () => { + const fetchFolder = async (): Promise => { if (!folderId) { return null; } @@ -425,7 +429,7 @@ export async function addFile({ file.createdAt = new Date(); file.userId = user ? user.id : null; file.userHost = user ? user.host : null; - file.folderId = folder?.id; + file.folderId = folder?.id ?? null; file.comment = comment; file.properties = properties; file.blurhash = info.blurhash || null; diff --git a/packages/backend/src/services/drive/delete-file.ts b/packages/backend/src/services/drive/delete-file.ts index c4f7dba1c..624fada32 100644 --- a/packages/backend/src/services/drive/delete-file.ts +++ b/packages/backend/src/services/drive/delete-file.ts @@ -7,7 +7,7 @@ import { fetchMeta } from '@/misc/fetch-meta.js'; import { InternalStorage } from './internal-storage.js'; import { getS3 } from './s3.js'; -export async function deleteFile(file: DriveFile, isExpired = false) { +export async function deleteFile(file: DriveFile, isExpired = false): Promise { if (file.storedInternal) { InternalStorage.del(file.accessKey!); @@ -33,7 +33,7 @@ export async function deleteFile(file: DriveFile, isExpired = false) { postProcess(file, isExpired); } -export async function deleteFileSync(file: DriveFile, isExpired = false) { +export async function deleteFileSync(file: DriveFile, isExpired = false): Promise { if (file.storedInternal) { InternalStorage.del(file.accessKey!); @@ -63,7 +63,7 @@ export async function deleteFileSync(file: DriveFile, isExpired = false) { postProcess(file, isExpired); } -async function postProcess(file: DriveFile, isExpired = false) { +async function postProcess(file: DriveFile, isExpired = false): Promise { // リモートファイル期限切れ削除後は直リンクにする if (isExpired && file.userHost !== null && file.uri != null) { DriveFiles.update(file.id, { @@ -89,7 +89,7 @@ async function postProcess(file: DriveFile, isExpired = false) { } } -export async function deleteObjectStorageFile(key: string) { +export async function deleteObjectStorageFile(key: string): Promise { const meta = await fetchMeta(); const s3 = getS3(meta); diff --git a/packages/backend/src/services/drive/image-processor.ts b/packages/backend/src/services/drive/image-processor.ts index 9b3c95af9..a85e0fbde 100644 --- a/packages/backend/src/services/drive/image-processor.ts +++ b/packages/backend/src/services/drive/image-processor.ts @@ -11,7 +11,7 @@ export type IImage = { * with resize, remove metadata, resolve orientation, stop animation */ export async function convertToJpeg(path: string, width: number, height: number): Promise { - return convertSharpToJpeg(await sharp(path), width, height); + return convertSharpToJpeg(sharp(path), width, height); } export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, height: number): Promise { @@ -39,7 +39,7 @@ export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, heig * with resize, remove metadata, resolve orientation, stop animation */ export async function convertToWebp(path: string, width: number, height: number, quality = 85): Promise { - return convertSharpToWebp(await sharp(path), width, height, quality); + return convertSharpToWebp(sharp(path), width, height, quality); } export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number, quality = 85): Promise { @@ -66,7 +66,7 @@ export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, heig * with resize, remove metadata, resolve orientation, stop animation */ export async function convertToPng(path: string, width: number, height: number): Promise { - return convertSharpToPng(await sharp(path), width, height); + return convertSharpToPng(sharp(path), width, height); } export async function convertSharpToPng(sharp: sharp.Sharp, width: number, height: number): Promise { diff --git a/packages/backend/src/services/drive/internal-storage.ts b/packages/backend/src/services/drive/internal-storage.ts index fa8f0ef83..e76749d6c 100644 --- a/packages/backend/src/services/drive/internal-storage.ts +++ b/packages/backend/src/services/drive/internal-storage.ts @@ -10,25 +10,25 @@ const _dirname = dirname(_filename); export class InternalStorage { private static readonly path = config.internalStoragePath || Path.resolve(_dirname, '../../../../../files'); - public static resolvePath = (key: string) => Path.resolve(InternalStorage.path, key); + public static resolvePath = (key: string): string => Path.resolve(InternalStorage.path, key); - public static read(key: string) { + public static read(key: string): fs.ReadStream { return fs.createReadStream(InternalStorage.resolvePath(key)); } - public static saveFromPath(key: string, srcPath: string) { + public static saveFromPath(key: string, srcPath: string): string { fs.mkdirSync(InternalStorage.path, { recursive: true }); fs.copyFileSync(srcPath, InternalStorage.resolvePath(key)); return `${config.url}/files/${key}`; } - public static saveFromBuffer(key: string, data: Buffer) { + public static saveFromBuffer(key: string, data: Buffer): string { fs.mkdirSync(InternalStorage.path, { recursive: true }); fs.writeFileSync(InternalStorage.resolvePath(key), data); return `${config.url}/files/${key}`; } - public static del(key: string) { + public static del(key: string): void { fs.unlink(InternalStorage.resolvePath(key), () => {}); } } diff --git a/packages/backend/src/services/drive/s3.ts b/packages/backend/src/services/drive/s3.ts index 80e34be95..f698d9869 100644 --- a/packages/backend/src/services/drive/s3.ts +++ b/packages/backend/src/services/drive/s3.ts @@ -3,7 +3,7 @@ import S3 from 'aws-sdk/clients/s3.js'; import { Meta } from '@/models/entities/meta.js'; import { getAgentByUrl } from '@/misc/fetch.js'; -export function getS3(meta: Meta) { +export function getS3(meta: Meta): S3 { const u = meta.objectStorageEndpoint != null ? `${meta.objectStorageUseSSL ? 'https://' : 'http://'}${meta.objectStorageEndpoint}` : `${meta.objectStorageUseSSL ? 'https://' : 'http://'}example.net`; diff --git a/packages/backend/src/services/drive/upload-from-url.ts b/packages/backend/src/services/drive/upload-from-url.ts index 65a062faf..a815d8374 100644 --- a/packages/backend/src/services/drive/upload-from-url.ts +++ b/packages/backend/src/services/drive/upload-from-url.ts @@ -58,7 +58,7 @@ export async function uploadFromUrl({ sensitive, }); logger.succ(`Got: ${driveFile.id}`); - return driveFile!; + return driveFile; } catch (e) { logger.error(`Failed to create drive file: ${e}`, { url,