MediaService: Change deleteFile

The former deleteFile was moved to the public apis media controller and the actual deletion functionality was moved in a separate function to be called on user deletion.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-03-20 18:58:59 +01:00 committed by David Mehren
parent 63fcca6c0d
commit f6121b58e8
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
3 changed files with 25 additions and 48 deletions

View file

@ -132,7 +132,23 @@ export class MediaController {
): Promise<void> {
const username = req.user.userName;
try {
await this.mediaService.deleteFile(filename, username);
this.logger.debug(
`Deleting '${filename}' for user '${username}'`,
'deleteFile',
);
const mediaUpload = await this.mediaService.findUploadByFilename(
filename,
);
if (mediaUpload.user.userName !== username) {
this.logger.warn(
`${username} tried to delete '${filename}', but is not the owner`,
'deleteFile',
);
throw new PermissionError(
`File '${filename}' is not owned by '${username}'`,
);
}
await this.mediaService.deleteFile(mediaUpload);
} catch (e) {
if (e instanceof PermissionError) {
throw new UnauthorizedException(e.message);