Skip to content

Commit f47f371

Browse files
authored
@tus/azure-store: fix non-ASCII characters error on metadata (#725)
1 parent 892c0cc commit f47f371

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.changeset/weak-panthers-fold.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tus/azure-store": patch
3+
---
4+
5+
Fix error on saving metadata when it contains non-ASCII characters

packages/azure-store/src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type KvStore,
88
MemoryKvStore,
99
TUS_RESUMABLE,
10+
Metadata,
1011
} from '@tus/utils'
1112
import {
1213
type AppendBlobClient,
@@ -78,7 +79,11 @@ export class AzureStore extends DataStore {
7879
await appendBlobClient.setMetadata(
7980
{
8081
tus_version: TUS_RESUMABLE,
81-
upload: JSON.stringify(upload),
82+
upload: JSON.stringify({
83+
...upload,
84+
// Base64 encode the metadata to avoid errors for non-ASCII characters
85+
metadata: Metadata.stringify(upload.metadata ?? {}),
86+
}),
8287
},
8388
{}
8489
)
@@ -110,6 +115,9 @@ export class AzureStore extends DataStore {
110115
throw ERRORS.FILE_NOT_FOUND
111116
}
112117
const upload = JSON.parse(propertyData.metadata.upload) as Upload
118+
// Metadata is base64 encoded to avoid errors for non-ASCII characters
119+
// so we need to decode it separately
120+
upload.metadata = Metadata.parse(JSON.stringify(upload.metadata ?? {}))
113121

114122
await this.cache.set(appendBlobClient.url, upload)
115123

test/src/stores.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ export const shouldCreateUploads = () => {
7171
const upload = await this.datastore.getUpload(file.id)
7272
assert.deepStrictEqual(upload.metadata, file.metadata)
7373
})
74+
75+
it('should store `upload_metadata` with non-ASCII characters', async function () {
76+
const file = new Upload({
77+
id: testId('create-test-non-ascii'),
78+
size: 1000,
79+
offset: 0,
80+
metadata: {filename: '世界_domination_plan.pdf', is_confidential: null},
81+
})
82+
await this.datastore.create(file)
83+
const upload = await this.datastore.getUpload(file.id)
84+
assert.deepStrictEqual(upload.metadata, file.metadata)
85+
})
7486
})
7587
}
7688

0 commit comments

Comments
 (0)