Skip to content

Commit b7adfb2

Browse files
authored
fix: escape non-ascii characters in filenames in s3 file provider (medusajs#14209)
* escape non-ascii characters in filenames in s3 file provider * fix url encoding
1 parent e3e3c72 commit b7adfb2

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

.changeset/shiny-hounds-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/file-s3": patch
3+
---
4+
5+
URL-encode S3 object metadata and returned URL

packages/modules/providers/file-s3/integration-tests/__tests__/services.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from "fs/promises"
21
import axios from "axios"
2+
import fs from "fs/promises"
33
import { S3FileService } from "../../src/services/s3-file"
44
jest.setTimeout(100000)
55

@@ -82,6 +82,23 @@ describe.skip("S3 File Plugin", () => {
8282
})
8383
})
8484

85+
it("uploads a file with non-ascii characters in the name", async () => {
86+
const fileContent = await fs.readFile(fixtureImagePath)
87+
const fixtureAsBinary = fileContent.toString("base64")
88+
89+
const resp = await s3Service.upload({
90+
filename: "catphoto-か.jpg",
91+
mimeType: "image/jpeg",
92+
content: fixtureAsBinary,
93+
access: "private",
94+
})
95+
96+
expect(resp).toEqual({
97+
key: expect.stringMatching(/tests\/catphoto-.*\.jpg/),
98+
url: expect.stringMatching(/https:\/\/.*\/catphoto-%E3%81%8B.*\.jpg/),
99+
})
100+
})
101+
85102
it("gets a presigned upload URL and uploads a file successfully", async () => {
86103
const fileContent = await fs.readFile(fixtureImagePath)
87104
const fixtureAsBinary = fileContent.toString("binary")

packages/modules/providers/file-s3/src/services/s3-file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class S3FileService extends AbstractFileProviderService {
148148
// Note: We could potentially set the content disposition when uploading,
149149
// but storing the original filename as metadata should suffice.
150150
Metadata: {
151-
"x-amz-meta-original-filename": file.filename,
151+
"original-filename": encodeURIComponent(file.filename),
152152
},
153153
})
154154

@@ -160,7 +160,7 @@ export class S3FileService extends AbstractFileProviderService {
160160
}
161161

162162
return {
163-
url: `${this.config_.fileUrl}/${fileKey}`,
163+
url: `${this.config_.fileUrl}/${encodeURI(fileKey)}`,
164164
key: fileKey,
165165
}
166166
}

0 commit comments

Comments
 (0)