diff --git a/src/collections/operations/local/update.ts b/src/collections/operations/local/update.ts index 8a72469ce8..02e02ca4ab 100644 --- a/src/collections/operations/local/update.ts +++ b/src/collections/operations/local/update.ts @@ -4,6 +4,7 @@ import getFileByPath from '../../../uploads/getFileByPath'; import update from '../update'; import { PayloadRequest } from '../../../express/types'; import { getDataLoader } from '../../dataloader'; +import { File } from '../../../uploads/types'; import i18nInit from '../../../translations/init'; export type Options = { diff --git a/src/uploads/imageResizer.ts b/src/uploads/imageResizer.ts index d336092509..35b8f9c35b 100644 --- a/src/uploads/imageResizer.ts +++ b/src/uploads/imageResizer.ts @@ -52,10 +52,21 @@ export default async function resizeAndSave({ }: Args): Promise { const { imageSizes } = config.upload; const sizesToSave: FileToSave[] = []; + const sizeData = {}; - const sizes = imageSizes - .filter((desiredSize) => needsResize(desiredSize, dimensions)) + const promises = imageSizes .map(async (desiredSize) => { + if (!needsResize(desiredSize, dimensions)) { + sizeData[desiredSize.name] = { + url: null, + width: null, + height: null, + filename: null, + filesize: null, + mimeType: null, + }; + return; + } let resized = sharp(file).resize(desiredSize); if (desiredSize.formatOptions) { @@ -83,8 +94,7 @@ export default async function resizeAndSave({ buffer: bufferObject.data, }); - return { - name: desiredSize.name, + sizeData[desiredSize.name] = { width: bufferObject.info.width, height: bufferObject.info.height, filename: imageNameWithDimensions, @@ -93,22 +103,10 @@ export default async function resizeAndSave({ }; }); - const savedSizes = await Promise.all(sizes); + await Promise.all(promises); return { - sizeData: savedSizes.reduce( - (results, size) => ({ - ...results, - [size.name]: { - width: size.width, - height: size.height, - filename: size.filename, - mimeType: size.mimeType, - filesize: size.filesize, - }, - }), - {}, - ), + sizeData, sizesToSave, }; } diff --git a/src/uploads/types.ts b/src/uploads/types.ts index a5448adc95..86df560684 100644 --- a/src/uploads/types.ts +++ b/src/uploads/types.ts @@ -7,10 +7,8 @@ export type FileSize = { filename: string; filesize: number; mimeType: string; - name: string; width: number; height: number; - crop: string; } export type FileSizes = { diff --git a/test/uploads/int.spec.ts b/test/uploads/int.spec.ts index dbc9ee333d..c223e6f49f 100644 --- a/test/uploads/int.spec.ts +++ b/test/uploads/int.spec.ts @@ -7,6 +7,7 @@ import { RESTClient } from '../helpers/rest'; import config, { mediaSlug, relationSlug } from './config'; import payload from '../../src'; import getFileByPath from '../../src/uploads/getFileByPath'; +import type { Media } from './payload-types'; const stat = promisify(fs.stat); @@ -76,7 +77,7 @@ describe('Collections - Uploads', () => { expect(await fileExists(path.join(__dirname, './media', doc.sizes.icon.filename))).toBe(true); // Check api response - expect(doc.sizes.tablet.filename).toBeUndefined(); + expect(doc.sizes.tablet.filename).toBeNull(); expect(doc.sizes.icon.filename).toBeDefined(); }); @@ -159,6 +160,28 @@ describe('Collections - Uploads', () => { expect(await fileExists(path.join(__dirname, './media', mediaDoc.sizes.icon.filename))).toBe(true); }); + it('should remove extra sizes on update', async () => { + const filePath = path.resolve(__dirname, './image.png'); + const file = await getFileByPath(filePath); + const small = await getFileByPath(path.resolve(__dirname, './small.png')); + + const { id } = await payload.create({ + collection: mediaSlug, + data: {}, + file, + }); + + const doc = await payload.update({ + collection: mediaSlug, + id, + data: {}, + file: small, + }); + + expect(doc.sizes.icon).toBeDefined(); + expect(doc.sizes.tablet.width).toBeNull(); + }); + it('should allow update removing a relationship', async () => { const filePath = path.resolve(__dirname, './image.png'); const file = await getFileByPath(filePath);