diff --git a/src/models/repositories/drive-file.ts b/src/models/repositories/drive-file.ts index bc1e37489c2aafd8200f45f7ef591f5b6c648491..62416f69eaa25f906cc4a757753a92dfcfafbe05 100644 --- a/src/models/repositories/drive-file.ts +++ b/src/models/repositories/drive-file.ts @@ -12,6 +12,12 @@ import { fetchMeta } from '../../misc/fetch-meta'; export type PackedDriveFile = SchemaType<typeof packedDriveFileSchema>; +type PackOptions = { + detail?: boolean, + self?: boolean, + withUser?: boolean, +}; + @EntityRepository(DriveFile) export class DriveFileRepository extends Repository<DriveFile> { public validateFileName(name: string): boolean { @@ -89,20 +95,19 @@ export class DriveFileRepository extends Repository<DriveFile> { return parseInt(sum, 10) || 0; } + public async pack(src: DriveFile['id'], options?: PackOptions): Promise<PackedDriveFile | null>; + public async pack(src: DriveFile, options?: PackOptions): Promise<PackedDriveFile>; public async pack( src: DriveFile['id'] | DriveFile, - options?: { - detail?: boolean, - self?: boolean, - withUser?: boolean, - } - ): Promise<PackedDriveFile> { + options?: PackOptions + ): Promise<PackedDriveFile | null> { const opts = Object.assign({ detail: false, self: false }, options); - const file = typeof src === 'object' ? src : await this.findOneOrFail(src); + const file = typeof src === 'object' ? src : await this.findOne(src); + if (file == null) return null; const meta = await fetchMeta(); @@ -128,15 +133,12 @@ export class DriveFileRepository extends Repository<DriveFile> { }); } - public packMany( + public async packMany( files: any[], - options?: { - detail?: boolean - self?: boolean, - withUser?: boolean, - } + options?: PackOptions ) { - return Promise.all(files.map(f => this.pack(f, options))); + const items = await Promise.all(files.map(f => this.pack(f, options))); + return items.filter(x => x != null); } }