fix(Delegate): media cleanup issue and log deleteOne

This commit is contained in:
Matthieu Bessat 2020-08-11 10:56:56 +02:00
parent c9a2d79701
commit 09b52a7fc3
2 changed files with 9 additions and 1 deletions

View file

@ -25,6 +25,7 @@ export default class MediaService {
} }
static delete(key: string, context: string) { static delete(key: string, context: string) {
console.log('> MediaCleanup: in context "' + context + '" deleteOne', key)
MediaService.getS3().deleteObject({ MediaService.getS3().deleteObject({
Bucket: MediaService.getBucket(), Bucket: MediaService.getBucket(),
Key: key Key: key

View file

@ -101,8 +101,15 @@ export default class DelegateController {
proposedVersion.gallery = [] proposedVersion.gallery = []
} }
if (Array.isArray(organization.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 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) .map((media: any) => media.key)
MediaService.deleteMany(toDeleteMedias, 'galleryUpdated') MediaService.deleteMany(toDeleteMedias, 'galleryUpdated')
} }