fix: escape non-ascii characters in filenames in s3 file provider (#14209)

* escape non-ascii characters in filenames in s3 file provider

* fix url encoding
This commit is contained in:
Pedro Guzman
2025-12-04 18:37:56 +01:00
committed by GitHub
parent e3e3c725db
commit b7adfb225b
3 changed files with 25 additions and 3 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/file-s3": patch
---
URL-encode S3 object metadata and returned URL

View File

@@ -1,5 +1,5 @@
import fs from "fs/promises"
import axios from "axios" import axios from "axios"
import fs from "fs/promises"
import { S3FileService } from "../../src/services/s3-file" import { S3FileService } from "../../src/services/s3-file"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -82,6 +82,23 @@ describe.skip("S3 File Plugin", () => {
}) })
}) })
it("uploads a file with non-ascii characters in the name", async () => {
const fileContent = await fs.readFile(fixtureImagePath)
const fixtureAsBinary = fileContent.toString("base64")
const resp = await s3Service.upload({
filename: "catphoto-か.jpg",
mimeType: "image/jpeg",
content: fixtureAsBinary,
access: "private",
})
expect(resp).toEqual({
key: expect.stringMatching(/tests\/catphoto-か.*\.jpg/),
url: expect.stringMatching(/https:\/\/.*\/catphoto-%E3%81%8B.*\.jpg/),
})
})
it("gets a presigned upload URL and uploads a file successfully", async () => { it("gets a presigned upload URL and uploads a file successfully", async () => {
const fileContent = await fs.readFile(fixtureImagePath) const fileContent = await fs.readFile(fixtureImagePath)
const fixtureAsBinary = fileContent.toString("binary") const fixtureAsBinary = fileContent.toString("binary")

View File

@@ -148,7 +148,7 @@ export class S3FileService extends AbstractFileProviderService {
// Note: We could potentially set the content disposition when uploading, // Note: We could potentially set the content disposition when uploading,
// but storing the original filename as metadata should suffice. // but storing the original filename as metadata should suffice.
Metadata: { Metadata: {
"x-amz-meta-original-filename": file.filename, "original-filename": encodeURIComponent(file.filename),
}, },
}) })
@@ -160,7 +160,7 @@ export class S3FileService extends AbstractFileProviderService {
} }
return { return {
url: `${this.config_.fileUrl}/${fileKey}`, url: `${this.config_.fileUrl}/${encodeURI(fileKey)}`,
key: fileKey, key: fileKey,
} }
} }