From 02f9be2c4a15eed6bba07f504319a2213ea411df Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Mon, 30 Jan 2023 11:52:09 -0500 Subject: [PATCH] feat: deletes old media upon re-upload #1897 --- src/collections/operations/update.ts | 39 ++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/collections/operations/update.ts b/src/collections/operations/update.ts index 20154580c8..be7037b200 100644 --- a/src/collections/operations/update.ts +++ b/src/collections/operations/update.ts @@ -1,6 +1,6 @@ import fs from 'fs'; import { promisify } from 'util'; - +import path from 'path'; import httpStatus from 'http-status'; import { Config as GeneratedTypes } from 'payload/generated-types'; import { MarkOptional } from 'ts-essentials'; @@ -8,7 +8,7 @@ import { Where, Document } from '../../types'; import { Collection } from '../config/types'; import sanitizeInternalFields from '../../utilities/sanitizeInternalFields'; import executeAccess from '../../auth/executeAccess'; -import { NotFound, Forbidden, APIError, ValidationError } from '../../errors'; +import { NotFound, Forbidden, APIError, ValidationError, ErrorDeletingFile } from '../../errors'; import { PayloadRequest } from '../../express/types'; import { hasWhereAccessResult } from '../../auth/types'; import { saveVersion } from '../../versions/saveVersion'; @@ -20,6 +20,8 @@ import { afterRead } from '../../fields/hooks/afterRead'; import { generateFileData } from '../../uploads/generateFileData'; import { getLatestEntityVersion } from '../../versions/getLatestCollectionVersion'; import { mapAsync } from '../../utilities/mapAsync'; +import fileExists from '../../uploads/fileExists'; +import { FileData } from '../../uploads/types'; const unlinkFile = promisify(fs.unlink); @@ -157,6 +159,39 @@ async function update( data = newFileData; + // ///////////////////////////////////// + // Delete any associated files + // ///////////////////////////////////// + + if (collectionConfig.upload) { + const { staticDir } = collectionConfig.upload; + + const staticPath = path.resolve(config.paths.configDir, staticDir); + + const fileToDelete = `${staticPath}/${doc.filename}`; + + if (await fileExists(fileToDelete)) { + fs.unlink(fileToDelete, (err) => { + if (err) { + throw new ErrorDeletingFile(t); + } + }); + } + + if (doc.sizes) { + Object.values(doc.sizes).forEach(async (size: FileData) => { + const sizeToDelete = `${staticPath}/${size.filename}`; + if (await fileExists(sizeToDelete)) { + fs.unlink(sizeToDelete, (err) => { + if (err) { + throw new ErrorDeletingFile(t); + } + }); + } + }); + } + } + // ///////////////////////////////////// // beforeValidate - Fields // /////////////////////////////////////