Skip to content

Commit da12eed

Browse files
authored
fix: correct image size URLs in beforeChange (#15214)
### What Fixes image size URLs in `beforeChange` pointing to original files instead of their correct sized files. ### Why The `beforeChange` hook was returning incorrect URLs for image size variants. Plugins relying on the URLs were getting wrong file references, causing issues for `plugin-cloud-storage`. ### Where Updated the `beforeChange` in `getBaseFields` to use the image size URL. ### Testing Added tests to `test/plugin-cloud-storage/int.spec.ts`
1 parent 8f50f83 commit da12eed

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

packages/payload/src/uploads/getBaseFields.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ export const getBaseUploadFields = ({ collection, config }: Options): Field[] =>
239239
generateFilePathOrURL({
240240
collectionSlug: collection?.slug as string,
241241
config,
242-
filename: data?.filename || originalDoc?.filename,
242+
filename:
243+
data?.sizes?.[size.name]?.filename ||
244+
originalDoc?.sizes?.[size.name]?.filename ||
245+
data?.filename ||
246+
originalDoc?.filename,
243247
relative: true,
244248
serverURL: req.payload.config.serverURL,
245249
urlOrPath: value,

test/plugin-cloud-storage/int.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,80 @@ describe('@payloadcms/plugin-cloud-storage', () => {
152152

153153
expect($metadata.httpStatusCode).toBe(200)
154154
})
155+
156+
it('should store correct URLs for sized images', async () => {
157+
const upload = await payload.create({
158+
collection: mediaSlug,
159+
data: {},
160+
filePath: path.resolve(dirname, '../uploads/image.png'),
161+
})
162+
163+
const apiResponse = await payload.findByID({
164+
collection: mediaSlug,
165+
id: upload.id,
166+
})
167+
expect(apiResponse.sizes).toBeTruthy()
168+
169+
const apiSizeKeys = Object.keys(apiResponse.sizes || {})
170+
for (const sizeKey of apiSizeKeys) {
171+
const size = apiResponse.sizes?.[sizeKey as keyof typeof apiResponse.sizes]
172+
if (!size) {
173+
continue
174+
}
175+
176+
expect(size.url).toEqual(`/api/${mediaSlug}/file/${size.filename}`)
177+
}
178+
179+
const rawDbData = await payload.db.findOne({
180+
collection: mediaSlug,
181+
where: { id: { equals: upload.id } },
182+
})
183+
expect(rawDbData).toBeTruthy()
184+
185+
const dbRecord = rawDbData as unknown as {
186+
filename: string
187+
sizes: Record<string, { filename: string; url: string }>
188+
url: string
189+
}
190+
type SizeData = { filename: string; url: string }
191+
192+
const sizeKeys = Object.keys(dbRecord.sizes)
193+
expect(sizeKeys.length).toBeGreaterThan(0)
194+
195+
for (const sizeKey of sizeKeys) {
196+
const size: SizeData = dbRecord.sizes[sizeKey] as SizeData
197+
expect(size.url).not.toEqual(`/api/${mediaSlug}/file/${dbRecord.filename}`)
198+
expect(size.url).toEqual(`/api/${mediaSlug}/file/${size.filename}`)
199+
}
200+
})
201+
202+
it('should handle collections without imageSizes correctly', async () => {
203+
const upload = await payload.create({
204+
collection: mediaWithPrefixSlug,
205+
data: {},
206+
filePath: path.resolve(dirname, '../uploads/image.png'),
207+
})
208+
209+
expect(upload.filename).toBeTruthy()
210+
expect(upload.url).toEqual(`/api/${mediaWithPrefixSlug}/file/${upload.filename}`)
211+
expect((upload as any).sizes).toBeFalsy()
212+
213+
const rawDbData = await payload.db.findOne({
214+
collection: mediaWithPrefixSlug,
215+
where: { id: { equals: upload.id } },
216+
})
217+
218+
expect(rawDbData).toBeTruthy()
219+
220+
const dbRecord = rawDbData as unknown as {
221+
filename: string
222+
url: string
223+
}
224+
225+
expect(dbRecord.filename).toEqual(upload.filename)
226+
expect(dbRecord.url).toEqual(`/api/${mediaWithPrefixSlug}/file/${upload.filename}`)
227+
expect((rawDbData as any)?.sizes).toBeFalsy()
228+
})
155229
})
156230
})
157231

0 commit comments

Comments
 (0)