From 09b52a7fc3b4920a308814d0566b864c7c2519cd Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 11 Aug 2020 10:56:56 +0200 Subject: [PATCH] fix(Delegate): media cleanup issue and log deleteOne --- src/MediaService.ts | 1 + src/controllers/DelegateController.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/MediaService.ts b/src/MediaService.ts index d96f59f..88911f5 100644 --- a/src/MediaService.ts +++ b/src/MediaService.ts @@ -25,6 +25,7 @@ export default class MediaService { } static delete(key: string, context: string) { + console.log('> MediaCleanup: in context "' + context + '" deleteOne', key) MediaService.getS3().deleteObject({ Bucket: MediaService.getBucket(), Key: key diff --git a/src/controllers/DelegateController.ts b/src/controllers/DelegateController.ts index 9978db6..6aa464b 100644 --- a/src/controllers/DelegateController.ts +++ b/src/controllers/DelegateController.ts @@ -101,8 +101,15 @@ export default class DelegateController { proposedVersion.gallery = [] } if (Array.isArray(organization.proposedVersion.gallery)) { + // we take all the media from the old proposed version + // we filter and we delete a media only if: + // not seen in the publishedVersion gallery + // not seen in the new proposed gallery let toDeleteMedias = organization.proposedVersion.gallery - .filter((media: any) => proposedVersion.gallery.filter((m: any) => m.key === media.key).length === 0) + .filter((media: any) => + organization.publishedVersion.gallery.filter((m: any) => m.key === media.key).length === 0 && + proposedVersion.gallery.filter((m: any) => m.key === media.key).length === 0 + ) .map((media: any) => media.key) MediaService.deleteMany(toDeleteMedias, 'galleryUpdated') }