Skip to content

Commit 066997d

Browse files
rjgtavRicardo Tavares
andauthored
fix(storage-r2): upload with the correct contentType (#13988)
### What? Use the correct Content-Type when uploading files to R2. ### Why? While R2 can infer the Content-Type of most uploads, in some scenarios it will fail (e.g. when uploading an SVG image). ### How? By passing the file's MIME type as the Content-Type header. Bug report: https://discord.com/channels/967097582721572934/1422639568808841329/1422645245534797914 Co-authored-by: Ricardo Tavares <rtavares@cloudflare.com>
1 parent c819083 commit 066997d

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

packages/storage-r2/src/handleUpload.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export const getHandleUpload = ({ bucket, prefix = '' }: Args): HandleUpload =>
1515
return async ({ data, file }) => {
1616
// Read more: https://github.com/cloudflare/workers-sdk/issues/6047#issuecomment-2691217843
1717
const buffer = process.env.NODE_ENV === 'development' ? new Blob([file.buffer]) : file.buffer
18-
await bucket.put(path.posix.join(prefix, file.filename), buffer)
18+
await bucket.put(path.posix.join(prefix, file.filename), buffer, {
19+
httpMetadata: { contentType: file.mimeType },
20+
})
21+
1922
return data
2023
}
2124
}

packages/storage-r2/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface R2Bucket {
1414
key: string,
1515
value: ArrayBuffer | ArrayBufferView | Blob | null | ReadableStream | string,
1616
options?: {
17+
httpMetadata?: any | Headers
1718
onlyIf: any
1819
} & any,
1920
): Promise<any | null>

0 commit comments

Comments
 (0)