This commit is contained in:
syuilo 2023-09-24 16:24:13 +09:00
parent 4a7f6e6de4
commit cf573add27
2 changed files with 28 additions and 11 deletions

View file

@ -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<MiDriveFile>, updater: MiUser) {
public async updateFile(file: MiDriveFile, values: Partial<MiDriveFile>, 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();
}
}

View file

@ -89,14 +89,28 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
throw new ApiError(meta.errors.accessDenied);
}
const fileObj = await this.driveService.update(file, {
let packedFile;
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 fileObj;
return packedFile;
});
}
}