From de893f7c403ed7d2aadd29092e1b217e23a10fa9 Mon Sep 17 00:00:00 2001 From: rubikscraft Date: Mon, 27 Jun 2022 16:23:23 +0200 Subject: [PATCH] improve image deletion method --- .../collections/image-db/image-db.service.ts | 47 +++++++------------ backend/src/managers/image/image.service.ts | 10 +--- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/backend/src/collections/image-db/image-db.service.ts b/backend/src/collections/image-db/image-db.service.ts index 75edb9d..712935b 100644 --- a/backend/src/collections/image-db/image-db.service.ts +++ b/backend/src/collections/image-db/image-db.service.ts @@ -80,7 +80,7 @@ export class ImageDBService { } } - public async findList( + public async delete( ids: string[], userid: string | undefined, ): AsyncFailable { @@ -88,45 +88,34 @@ export class ImageDBService { if (ids.length > 500) return Fail('Too many results'); try { - const found = await this.imageRepo.find({ + const deletable_images = await this.imageRepo.find({ where: { id: In(ids), user_id: userid, }, }); - if (found === undefined) return Fail('Images not found'); - return found; + const available_ids = deletable_images.map((i) => i.id); + + if (available_ids.length === 0) return Fail('Images not found'); + + await Promise.all([ + this.imageDerivativeRepo.delete({ + image_id: In(available_ids), + }), + this.imageFileRepo.delete({ + image_id: In(available_ids), + }), + + this.imageRepo.delete({ id: In(available_ids) }), + ]); + + return deletable_images; } catch (e) { return Fail(e); } } - public async delete(ids: string[]): AsyncFailable { - if (ids.length === 0) return true; - if (ids.length > 500) return Fail('Too many results'); - - try { - const derivativesResult = await this.imageDerivativeRepo.delete({ - image_id: In(ids), - }); - const filesResult = await this.imageFileRepo.delete({ - image_id: In(ids), - }); - const result = await this.imageRepo.delete({ id: In(ids) }); - - if ( - result.affected === 0 && - filesResult.affected === 0 && - derivativesResult.affected === 0 - ) - return Fail('Image not found'); - } catch (e) { - return Fail(e); - } - return true; - } - public async deleteAll(IAmSure: boolean): AsyncFailable { if (!IAmSure) return Fail('You must confirm that you want to delete all images'); diff --git a/backend/src/managers/image/image.service.ts b/backend/src/managers/image/image.service.ts index 4b0e44d..aae17c5 100644 --- a/backend/src/managers/image/image.service.ts +++ b/backend/src/managers/image/image.service.ts @@ -50,15 +50,7 @@ export class ImageManagerService { ids: string[], userid: string | undefined, ): AsyncFailable { - const images = await this.imagesService.findList(ids, userid); - if (HasFailed(images)) return images; - - const availableIds = images.map((image) => image.id); - - const deleteResult = await this.imagesService.delete(availableIds); - if (HasFailed(deleteResult)) return deleteResult; - - return images; + return await this.imagesService.delete(ids, userid); } public async upload(