Skip to content

Commit

Permalink
Add basic storage info to Upload model
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Jun 6, 2024
1 parent a0f9da1 commit 6c20818
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/file-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class FileStore extends DataStore {
offset: stats.size,
metadata: file.metadata,
creation_date: file.creation_date,
storage: {type: 'file', path: file_path},
})
)
})
Expand Down
4 changes: 4 additions & 0 deletions packages/s3-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class S3Store extends DataStore {
offset: Number.parseInt(file.offset, 10),
metadata: file.metadata,
creation_date: file.creation_date,
storage: file.storage,
}),
}
await this.cache.set(id, metadata)
Expand Down Expand Up @@ -530,6 +531,7 @@ export class S3Store extends DataStore {
upload.creation_date = new Date().toISOString()

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

Expand Down Expand Up @@ -614,6 +616,7 @@ export class S3Store extends DataStore {
offset: metadata.file.size as number,
size: metadata.file.size,
metadata: metadata.file.metadata,
storage: metadata.file.storage,
})
}

Expand All @@ -627,6 +630,7 @@ export class S3Store extends DataStore {
...metadata.file,
offset: offset + (incompletePartSize ?? 0),
size: metadata.file.size,
storage: metadata.file.storage,
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/models/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class DataStore extends EventEmitter {
* the upload.
*/
async getUpload(id: string): Promise<Upload> {
return new Upload({id, size: 0, offset: 0})
return new Upload({id, size: 0, offset: 0, storage: {type: 'datastore', path: ''}})
}

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/utils/src/models/Upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ type TUpload = {
size?: number
offset: number
metadata?: Record<string, string | null>
storage?: {
type: string
path: string
bucket?: string
}
creation_date?: string
}

export class Upload {
id: TUpload['id']
metadata: TUpload['metadata']
size?: TUpload['size']
size: TUpload['size']
offset: TUpload['offset']
creation_date: TUpload['creation_date']
storage: TUpload['storage']

constructor(upload: TUpload) {
if (!upload.id) {
Expand All @@ -22,6 +28,7 @@ export class Upload {
this.size = upload.size
this.offset = upload.offset
this.metadata = upload.metadata
this.storage = upload.storage

this.creation_date = upload.creation_date ?? new Date().toISOString()
}
Expand Down
2 changes: 2 additions & 0 deletions test/stores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const shouldCreateUploads = function () {

it('should resolve to file', async function () {
const newFile = await this.datastore.create(file)
assert.ok(newFile.storage.path)
assert.ok(newFile.storage.type)
assert.equal(newFile instanceof Upload, true)
})

Expand Down

0 comments on commit 6c20818

Please sign in to comment.