Skip to content

Commit 872b021

Browse files
authored
@tus/s3-store: fix invalid character errors for "x-amz-meta-file” header (#470)
Breaking change: uploads done before this update can't be resumed.
1 parent eeac2eb commit 872b021

File tree

1 file changed

+5
-28
lines changed

1 file changed

+5
-28
lines changed

packages/s3-store/index.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ export class S3Store extends DataStore {
9898
await this.client.putObject({
9999
Bucket: this.bucket,
100100
Key: `${upload.id}.info`,
101-
Body: '',
101+
Body: JSON.stringify(upload),
102102
Metadata: {
103-
file: JSON.stringify(upload),
104103
'upload-id': uploadId,
105104
'tus-version': TUS_RESUMABLE,
106105
},
@@ -114,29 +113,25 @@ export class S3Store extends DataStore {
114113
* HTTP calls to S3.
115114
*/
116115
private async getMetadata(id: string): Promise<MetadataValue> {
117-
log(`[${id}] retrieving metadata`)
118116
const cached = this.cache.get(id)
119117
if (cached?.file) {
120-
log(`[${id}] metadata from cache`)
121118
return cached
122119
}
123120

124-
log(`[${id}] metadata from s3`)
125-
const {Metadata} = await this.client.headObject({
121+
const {Metadata, Body} = await this.client.getObject({
126122
Bucket: this.bucket,
127123
Key: `${id}.info`,
128124
})
129-
const file = JSON.parse(Metadata?.file as string)
125+
const file = JSON.parse((await Body?.transformToString()) as string)
130126
this.cache.set(id, {
131-
...Metadata,
132127
'tus-version': Metadata?.['tus-version'] as string,
128+
'upload-id': Metadata?.['upload-id'] as string,
133129
file: new Upload({
134130
id,
135131
size: file.size ? Number.parseInt(file.size, 10) : undefined,
136132
offset: Number.parseInt(file.offset, 10),
137133
metadata: file.metadata,
138134
}),
139-
'upload-id': Metadata?.['upload-id'] as string,
140135
})
141136
return this.cache.get(id) as MetadataValue
142137
}
@@ -400,34 +395,16 @@ export class S3Store extends DataStore {
400395
*/
401396
public async create(upload: Upload) {
402397
log(`[${upload.id}] initializing multipart upload`)
403-
type CreateRequest = Omit<AWS.CreateMultipartUploadCommandInput, 'Metadata'> & {
404-
Metadata: Record<string, string>
405-
}
406-
const request: CreateRequest = {
398+
const request: AWS.CreateMultipartUploadCommandInput = {
407399
Bucket: this.bucket,
408400
Key: upload.id,
409401
Metadata: {'tus-version': TUS_RESUMABLE},
410402
}
411-
const file: Record<string, string | number | Upload['metadata']> = {
412-
id: upload.id,
413-
offset: upload.offset,
414-
metadata: upload.metadata,
415-
}
416-
417-
if (upload.size) {
418-
file.size = upload.size.toString()
419-
file.isSizeDeferred = 'false'
420-
} else {
421-
file.isSizeDeferred = 'true'
422-
}
423403

424404
if (upload.metadata?.contentType) {
425405
request.ContentType = upload.metadata.contentType
426406
}
427407

428-
// TODO: rename `file` to `upload` to align with the codebase
429-
request.Metadata.file = JSON.stringify(file)
430-
431408
const res = await this.client.createMultipartUpload(request)
432409
log(`[${upload.id}] multipart upload created (${res.UploadId})`)
433410
await this.saveMetadata(upload, res.UploadId as string)

0 commit comments

Comments
 (0)