Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 0685717

Browse files
committed
fix: ensures that proper Content-Type headers are set on pass-through static files
1 parent 5d18d27 commit 0685717

9 files changed

Lines changed: 60 additions & 21 deletions

File tree

dev/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"dependencies": {
1717
"@aws-sdk/client-s3": "^3.142.0",
1818
"@azure/storage-blob": "^12.11.0",
19-
"@google-cloud/storage": "^6.4.1",
19+
"@google-cloud/storage": "^6.4.2",
2020
"dotenv": "^8.2.0",
2121
"express": "^4.17.1",
2222
"payload": "^1.0.27"

dev/src/collections/Users.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import type { CollectionConfig } from 'payload/types'
33
const Users: CollectionConfig = {
44
slug: 'users',
55
auth: true,
6-
admin: {
7-
useAsTitle: 'upn',
8-
},
96
access: {
107
read: () => true,
118
},
129
fields: [
13-
// no fields in this demo necessary
10+
{
11+
name: 'avatar',
12+
type: 'upload',
13+
relationTo: 'media',
14+
},
15+
{
16+
name: 'background',
17+
type: 'upload',
18+
relationTo: 'media',
19+
},
1420
],
1521
}
1622

dev/src/payload.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ if (process.env.PAYLOAD_PUBLIC_CLOUD_STORAGE_ADAPTER === 's3') {
2424
config: {
2525
endpoint: process.env.S3_ENDPOINT,
2626
forcePathStyle: process.env.S3_FORCE_PATH_STYLE === 'true',
27+
region: process.env.S3_REGION,
2728
credentials: {
2829
accessKeyId: process.env.S3_ACCESS_KEY_ID,
2930
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,

dev/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,10 +2254,10 @@
22542254
resolved "https://registry.yarnpkg.com/@google-cloud/promisify/-/promisify-3.0.0.tgz#5cd6941fc30c4acac18051706aa5af96069bd3e3"
22552255
integrity sha512-91ArYvRgXWb73YvEOBMmOcJc0bDRs5yiVHnqkwoG0f3nm7nZuipllz6e7BvFESBvjkDTBC0zMD8QxedUwNLc1A==
22562256

2257-
"@google-cloud/storage@^6.4.1":
2258-
version "6.4.1"
2259-
resolved "https://registry.yarnpkg.com/@google-cloud/storage/-/storage-6.4.1.tgz#83334150d4e224cb48691de4d7f9c38e143a0970"
2260-
integrity sha512-lAddmRJ8tvxPykUqJfONBQA5XGwGk0vut1POXublc64+nCdB5aQMxwuBMf7J1zubx19QGpYPQwW6wR7YTWrvLw==
2257+
"@google-cloud/storage@^6.4.2":
2258+
version "6.4.2"
2259+
resolved "https://registry.npmjs.org/@google-cloud/storage/-/storage-6.4.2.tgz#e81bfdda64e88d7b06668287d31119804cad988a"
2260+
integrity sha512-rnwx++XIxy63t1CREGlY2+H+tvuvYmkiIqJdq0IRzGaah9bDetV+qJVl5Mvi5aPhgoqn3UNnKGv9SPcETimFgw==
22612261
dependencies:
22622262
"@google-cloud/paginator" "^3.0.7"
22632263
"@google-cloud/projectify" "^3.0.0"

src/adapters/azure/staticHandler.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ export const getHandler = ({ containerClient }: Args): StaticHandler => {
1010
try {
1111
const blockBlobClient = containerClient.getBlockBlobClient(req.params.filename)
1212

13-
const downloadBlockBlobResponse = await blockBlobClient.download(0)
13+
const blob = await blockBlobClient.download(0)
1414

15-
if (downloadBlockBlobResponse?.readableStreamBody) {
16-
return downloadBlockBlobResponse.readableStreamBody.pipe(res)
15+
res.set({
16+
'Content-Length': blob.contentLanguage,
17+
'Content-Type': blob.contentType,
18+
ETag: blob.etag,
19+
})
20+
21+
if (blob?.readableStreamBody) {
22+
return blob.readableStreamBody.pipe(res)
1723
}
1824

1925
return next()

src/adapters/gcs/handleUpload.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Storage } from '@google-cloud/storage'
1+
import type { Storage } from '@google-cloud/storage'
22
import type { CollectionConfig } from 'payload/types'
33
import type { HandleUpload } from '../../types'
44

@@ -13,8 +13,11 @@ export const getHandleUpload = ({ gcs, bucket, acl }: Args): HandleUpload => {
1313
return async ({ data, file }) => {
1414
const gcsFile = gcs.bucket(bucket).file(file.filename)
1515
await gcsFile.save(file.buffer, {
16-
contentType: file.mimeType,
16+
metadata: {
17+
contentType: file.mimeType,
18+
},
1719
})
20+
1821
if (acl) {
1922
await gcsFile[`make${acl}`]()
2023
}

src/adapters/gcs/staticHandler.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Storage } from '@google-cloud/storage'
1+
import type { Storage } from '@google-cloud/storage'
22
import type { StaticHandler } from '../../types'
33

44
interface Args {
@@ -9,7 +9,17 @@ interface Args {
99
export const getHandler = ({ gcs, bucket }: Args): StaticHandler => {
1010
return async (req, res, next) => {
1111
try {
12-
return gcs.bucket(bucket).file(req.params.filename).createReadStream().pipe(res)
12+
const file = gcs.bucket(bucket).file(req.params.filename)
13+
14+
const [metadata] = await file.getMetadata()
15+
16+
res.set({
17+
'Content-Length': metadata.contentType,
18+
'Content-Type': metadata.contentLength,
19+
ETag: metadata.etag,
20+
})
21+
22+
return file.createReadStream().pipe(res)
1323
} catch (err: unknown) {
1424
return next()
1525
}

src/adapters/s3/mock.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
exports.S3 = function () {
22
return null
33
}
4+
5+
exports.HeadObjectCommand = function () {
6+
return null
7+
}

src/adapters/s3/staticHandler.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@ interface Args {
99

1010
export const getHandler = ({ s3, bucket }: Args): StaticHandler => {
1111
return async (req, res, next) => {
12+
const params = {
13+
Bucket: bucket,
14+
Key: req.params.filename,
15+
}
16+
1217
try {
13-
const response = await s3.getObject({
14-
Bucket: bucket,
15-
Key: req.params.filename,
18+
const object = await s3.getObject(params)
19+
20+
res.set({
21+
'Content-Length': object.ContentLength,
22+
'Content-Type': object.ContentType,
23+
ETag: object.ETag,
1624
})
1725

18-
if (response?.Body) {
19-
return (response.Body as Readable).pipe(res)
26+
if (object?.Body) {
27+
return (object.Body as Readable).pipe(res)
2028
}
2129

2230
return next()
2331
} catch (err: unknown) {
32+
req.payload.logger.error(err)
2433
return next()
2534
}
2635
}

0 commit comments

Comments
 (0)