diff --git a/packages/backend/src/core/DriveService.ts b/packages/backend/src/core/DriveService.ts index 0409a4f53..891be4611 100644 --- a/packages/backend/src/core/DriveService.ts +++ b/packages/backend/src/core/DriveService.ts @@ -87,6 +87,9 @@ type UploadFromUrlArgs = { @Injectable() export class DriveService { + public static NoSuchFolderError = class extends Error {}; + public static InvalidFileNameError = class extends Error {}; + public static CannotUnmarkSensitiveError = class extends Error {}; private registerLogger: Logger; private downloaderLogger: Logger; private deleteLogger: Logger; @@ -650,15 +653,15 @@ export class DriveService { } @bindThis - public async update(file: MiDriveFile, values: Partial, updater: MiUser) { + public async updateFile(file: MiDriveFile, values: Partial, updater: MiUser) { const alwaysMarkNsfw = (await this.roleService.getUserPolicies(file.userId)).alwaysMarkNsfw; if (values.name && !this.driveFileEntityService.validateFileName(file.name)) { - throw new Error('invalid filename'); + throw new DriveService.InvalidFileNameError(); } if (values.isSensitive !== undefined && values.isSensitive !== file.isSensitive && alwaysMarkNsfw && !values.isSensitive) { - throw new Error('cannot unmark nsfw'); + throw new DriveService.CannotUnmarkSensitiveError(); } if (values.folderId != null) { @@ -668,7 +671,7 @@ export class DriveService { }); if (folder == null) { - throw new Error('folder-not-found'); + throw new DriveService.NoSuchFolderError(); } } diff --git a/packages/backend/src/server/api/endpoints/drive/files/update.ts b/packages/backend/src/server/api/endpoints/drive/files/update.ts index c9e2d7464..c01f3de03 100644 --- a/packages/backend/src/server/api/endpoints/drive/files/update.ts +++ b/packages/backend/src/server/api/endpoints/drive/files/update.ts @@ -89,14 +89,28 @@ export default class extends Endpoint { // eslint- throw new ApiError(meta.errors.accessDenied); } - const fileObj = await this.driveService.update(file, { - folderId: ps.folderId, - name: ps.name, - isSensitive: ps.isSensitive, - comment: ps.comment, - }, me); + let packedFile; - return fileObj; + try { + packedFile = await this.driveService.updateFile(file, { + folderId: ps.folderId, + name: ps.name, + isSensitive: ps.isSensitive, + comment: ps.comment, + }, me); + } catch (e) { + if (e instanceof DriveService.InvalidFileNameError) { + throw new ApiError(meta.errors.invalidFileName); + } else if (e instanceof DriveService.NoSuchFolderError) { + throw new ApiError(meta.errors.noSuchFolder); + } else if (e instanceof DriveService.CannotUnmarkSensitiveError) { + throw new ApiError(meta.errors.restrictedByRole); + } else { + throw e; + } + } + + return packedFile; }); } }