fix(next): pass a corrent content-type header in getFile route (#5799)

Co-authored-by: Jarrod Flesch <jarrodmflesch@gmail.com>
This commit is contained in:
Ritsu
2024-04-17 18:40:02 +03:00
committed by GitHub
parent a2fe3f66e3
commit 8bf03ae706
3 changed files with 17 additions and 9 deletions

View File

@@ -55,7 +55,8 @@
"swc-plugin-transform-remove-imports": "^1.12.1",
"terser-webpack-plugin": "^5.3.10",
"webpack": "^5.78.0",
"webpack-cli": "^5.1.4"
"webpack-cli": "^5.1.4",
"file-type": "16.5.4"
},
"dependencies": {
"@dnd-kit/core": "6.0.8",
@@ -78,7 +79,8 @@
"peerDependencies": {
"http-status": "1.6.2",
"next": "^14.2.0-canary.23",
"payload": "workspace:*"
"payload": "workspace:*",
"file-type": "16.5.4"
},
"publishConfig": {
"main": "./dist/index.js",

View File

@@ -1,5 +1,6 @@
import type { Collection, PayloadRequest } from 'payload/types'
import getFileType from 'file-type'
import fsPromises from 'fs/promises'
import httpStatus from 'http-status'
import path from 'path'
@@ -55,12 +56,18 @@ export const getFile = async ({ collection, filename, req }: Args): Promise<Resp
const filePath = path.resolve(`${fileDir}/${filename}`)
const stats = await fsPromises.stat(filePath)
const data = streamFile(filePath)
const headers = new Headers({
'content-length': stats.size + '',
})
const fileTypeResult = await getFileType.fromFile(filePath)
if (fileTypeResult?.mime) headers.set('content-type', fileTypeResult.mime)
return new Response(data, {
headers: new Headers({
'content-length': stats.size + '',
}),
headers,
status: httpStatus.OK,
})
} catch (error) {

7
pnpm-lock.yaml generated
View File

@@ -670,6 +670,9 @@ importers:
css-minimizer-webpack-plugin:
specifier: ^6.0.0
version: 6.0.0(esbuild@0.19.12)(webpack@5.91.0)
file-type:
specifier: 16.5.4
version: 16.5.4
mini-css-extract-plugin:
specifier: 1.6.2
version: 1.6.2(webpack@5.91.0)
@@ -9623,7 +9626,6 @@ packages:
readable-web-to-node-stream: 3.0.2
strtok3: 6.3.0
token-types: 4.2.1
dev: false
/file-type@17.1.6:
resolution: {integrity: sha512-hlDw5Ev+9e883s0pwUsuuYNu4tD7GgpUnOvykjv1Gya0ZIjuKumthDRua90VUn6/nlRKAjcxLUnHNTIUWwWIiw==}
@@ -12797,7 +12799,6 @@ packages:
/peek-readable@4.1.0:
resolution: {integrity: sha512-ZI3LnwUv5nOGbQzD9c2iDG6toheuXSZP5esSHBjopsXH4dg19soufvpUGA3uohi5anFtGb2lhAVdHzH6R/Evvg==}
engines: {node: '>=8'}
dev: false
/peek-readable@5.0.0:
resolution: {integrity: sha512-YtCKvLUOvwtMGmrniQPdO7MwPjgkFBtFIrmfSbYmYuq3tKDV/mcfAhBth1+C3ru7uXIZasc/pHnb+YDYNkkj4A==}
@@ -15113,7 +15114,6 @@ packages:
dependencies:
'@tokenizer/token': 0.3.0
peek-readable: 4.1.0
dev: false
/strtok3@7.0.0:
resolution: {integrity: sha512-pQ+V+nYQdC5H3Q7qBZAz/MO6lwGhoC2gOAjuouGf/VO0m7vQRh8QNMl2Uf6SwAtzZ9bOw3UIeBukEGNJl5dtXQ==}
@@ -15459,7 +15459,6 @@ packages:
dependencies:
'@tokenizer/token': 0.3.0
ieee754: 1.2.1
dev: false
/token-types@5.0.1:
resolution: {integrity: sha512-Y2fmSnZjQdDb9W4w4r1tswlMHylzWIeOKpx0aZH9BgGtACHhrk3OkT52AzwcuqTRBZtvvnTjDBh8eynMulu8Vg==}