Skip to content

Commit 117e1b2

Browse files
authored
Add basic storage info to Upload model (#624)
1 parent a0f9da1 commit 117e1b2

File tree

9 files changed

+39
-4
lines changed

9 files changed

+39
-4
lines changed

.changeset/hungry-games-compete.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@tus/file-store': minor
3+
'@tus/gcs-store': minor
4+
'@tus/s3-store': minor
5+
'@tus/utils': minor
6+
---
7+
8+
Add basic storage information to the Upload model. You can now access `upload.storage`
9+
which has `type` (`file`, `s3`, `gcs`), `path`, and when applicable `bucket`.

packages/file-store/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ export class FileStore extends DataStore {
6060
*/
6161
async create(file: Upload): Promise<Upload> {
6262
const dirs = file.id.split('/').slice(0, -1)
63+
const filePath = path.join(this.directory, file.id)
6364

6465
await fsProm.mkdir(path.join(this.directory, ...dirs), {recursive: true})
65-
await fsProm.writeFile(path.join(this.directory, file.id), '')
66+
await fsProm.writeFile(filePath, '')
6667
await this.configstore.set(file.id, file)
6768

69+
file.storage = {type: 'file', path: filePath}
70+
6871
return file
6972
}
7073

@@ -164,6 +167,7 @@ export class FileStore extends DataStore {
164167
offset: stats.size,
165168
metadata: file.metadata,
166169
creation_date: file.creation_date,
170+
storage: {type: 'file', path: file_path},
167171
})
168172
)
169173
})

packages/gcs-store/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class GCSStore extends DataStore {
3333

3434
const gcs_file = this.bucket.file(file.id)
3535

36+
file.storage = {type: 'gcs', path: file.id, bucket: this.bucket.name}
37+
3638
const options = {
3739
metadata: {
3840
metadata: {
@@ -41,6 +43,7 @@ export class GCSStore extends DataStore {
4143
sizeIsDeferred: `${file.sizeIsDeferred}`,
4244
offset: file.offset,
4345
metadata: JSON.stringify(file.metadata),
46+
storage: JSON.stringify(file.storage),
4447
},
4548
},
4649
}
@@ -82,6 +85,7 @@ export class GCSStore extends DataStore {
8285
sizeIsDeferred: `${upload.sizeIsDeferred}`,
8386
offset,
8487
metadata: JSON.stringify(upload.metadata),
88+
storage: JSON.stringify(upload.storage),
8589
},
8690
},
8791
}
@@ -150,6 +154,7 @@ export class GCSStore extends DataStore {
150154
size: size ? Number.parseInt(size, 10) : size,
151155
offset: Number.parseInt(metadata.size, 10), // `size` is set by GCS
152156
metadata: meta ? JSON.parse(meta) : undefined,
157+
storage: {type: 'gcs', path: id, bucket: this.bucket.name},
153158
})
154159
)
155160
})

packages/s3-store/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export class S3Store extends DataStore {
188188
offset: Number.parseInt(file.offset, 10),
189189
metadata: file.metadata,
190190
creation_date: file.creation_date,
191+
storage: file.storage,
191192
}),
192193
}
193194
await this.cache.set(id, metadata)
@@ -530,6 +531,7 @@ export class S3Store extends DataStore {
530531
upload.creation_date = new Date().toISOString()
531532

532533
const res = await this.client.createMultipartUpload(request)
534+
upload.storage = {type: 's3', path: res.Key as string, bucket: this.bucket}
533535
await this.saveMetadata(upload, res.UploadId as string)
534536
log(`[${upload.id}] multipart upload created (${res.UploadId})`)
535537

@@ -614,6 +616,7 @@ export class S3Store extends DataStore {
614616
offset: metadata.file.size as number,
615617
size: metadata.file.size,
616618
metadata: metadata.file.metadata,
619+
storage: metadata.file.storage,
617620
})
618621
}
619622

@@ -627,6 +630,7 @@ export class S3Store extends DataStore {
627630
...metadata.file,
628631
offset: offset + (incompletePartSize ?? 0),
629632
size: metadata.file.size,
633+
storage: metadata.file.storage,
630634
})
631635
}
632636

packages/server/test/DeleteHandler.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('DeleteHandler', () => {
7979
creation_date: undefined,
8080
offset: 1000,
8181
size: 1000,
82+
storage: {type: 'test', path: `${path}/abc`},
8283
})
8384
await assert.rejects(() => handler.send(req, res, context), {status_code: 400})
8485
})

packages/server/test/Server.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
12
/* eslint-disable no-throw-literal */
23
import 'should'
34

@@ -496,7 +497,9 @@ describe('Server', () => {
496497
const server = new Server({
497498
path: '/test/output',
498499
datastore: new FileStore({directory}),
499-
onUploadFinish() {
500+
onUploadFinish(_, __, upload) {
501+
assert.ok(upload.storage!.path, 'should have storage.path')
502+
assert.ok(upload.storage!.type, 'should have storage.type')
500503
throw {body: 'no', status_code: 500}
501504
},
502505
})

packages/utils/src/models/DataStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class DataStore extends EventEmitter {
5050
* the upload.
5151
*/
5252
async getUpload(id: string): Promise<Upload> {
53-
return new Upload({id, size: 0, offset: 0})
53+
return new Upload({id, size: 0, offset: 0, storage: {type: 'datastore', path: ''}})
5454
}
5555

5656
/**

packages/utils/src/models/Upload.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ type TUpload = {
33
size?: number
44
offset: number
55
metadata?: Record<string, string | null>
6+
storage?: {
7+
type: string
8+
path: string
9+
bucket?: string
10+
}
611
creation_date?: string
712
}
813

914
export class Upload {
1015
id: TUpload['id']
1116
metadata: TUpload['metadata']
12-
size?: TUpload['size']
17+
size: TUpload['size']
1318
offset: TUpload['offset']
1419
creation_date: TUpload['creation_date']
20+
storage: TUpload['storage']
1521

1622
constructor(upload: TUpload) {
1723
if (!upload.id) {
@@ -22,6 +28,7 @@ export class Upload {
2228
this.size = upload.size
2329
this.offset = upload.offset
2430
this.metadata = upload.metadata
31+
this.storage = upload.storage
2532

2633
this.creation_date = upload.creation_date ?? new Date().toISOString()
2734
}

test/stores.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export const shouldCreateUploads = function () {
4242

4343
it('should resolve to file', async function () {
4444
const newFile = await this.datastore.create(file)
45+
assert.ok(newFile.storage.path)
46+
assert.ok(newFile.storage.type)
4547
assert.equal(newFile instanceof Upload, true)
4648
})
4749

0 commit comments

Comments
 (0)