fix: serve svg+xml as svg (#13277)

Based from https://github.com/payloadcms/payload/pull/13276

Fixes https://github.com/payloadcms/payload/issues/7624

If an uploaded image has `.svg` ext, and the mimeType is read as
`application/xml` adjust the mimeType to `image/svg+xml`.

---------

Co-authored-by: Philipp Schneider <47689073+philipp-tailor@users.noreply.github.com>
This commit is contained in:
Jarrod Flesch
2025-07-25 17:00:51 -04:00
committed by GitHub
parent e8f6cb5ed1
commit bc802846c5
3 changed files with 233 additions and 1 deletions

View File

@@ -93,9 +93,14 @@ export const getFileHandler: PayloadHandler = async (req) => {
const data = streamFile(filePath)
const fileTypeResult = (await fileTypeFromFile(filePath)) || getFileTypeFallback(filePath)
let mimeType = fileTypeResult.mime
if (filePath.endsWith('.svg') && fileTypeResult.mime === 'application/xml') {
mimeType = 'image/svg+xml'
}
let headers = new Headers()
headers.set('Content-Type', fileTypeResult.mime)
headers.set('Content-Type', mimeType)
headers.set('Content-Length', stats.size + '')
headers = collection.config.upload?.modifyResponseHeaders
? collection.config.upload.modifyResponseHeaders({ headers }) || headers