PublicApi: Add option to keep media to DELETE /notes/{note}

This adds a body to the route DELETE /notes/{note} of the public api to specify if the associated media uploads of the note should be kept or deleted.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-04-01 01:23:12 +02:00 committed by David Mehren
parent bd4231c1c5
commit 4640735d18
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 53 additions and 6 deletions

View file

@ -62,6 +62,7 @@ import {
} from '../../utils/descriptions';
import { MediaUploadDto } from '../../../media/media-upload.dto';
import { MediaService } from '../../../media/media.service';
import { NoteMediaDeletionDto } from '../../../notes/note.media-deletion.dto';
@ApiTags('notes')
@ApiSecurity('token')
@ -172,12 +173,21 @@ export class NotesController {
async deleteNote(
@Req() req: Request,
@Param('noteIdOrAlias') noteIdOrAlias: string,
@Body() noteMediaDeletionDto: NoteMediaDeletionDto,
): Promise<void> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.isOwner(req.user, note)) {
throw new UnauthorizedException('Deleting note denied!');
}
const mediaUploads = await this.mediaService.listUploadsByNote(note);
for (const mediaUpload of mediaUploads) {
if (!noteMediaDeletionDto.keepMedia) {
await this.mediaService.deleteFile(mediaUpload);
} else {
await this.mediaService.removeNoteFromMediaUpload(mediaUpload);
}
}
this.logger.debug('Deleting note: ' + noteIdOrAlias, 'deleteNote');
await this.noteService.deleteNote(note);
this.logger.debug('Successfully deleted ' + noteIdOrAlias, 'deleteNote');