Skip to content

Commit

Permalink
fix: custom metadata input names
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos authored Jul 16, 2024
1 parent 9486b37 commit 556b7f4
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/http/routes/object/getObjectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ async function requestHandler(
obj = await request.storage
.asSuperUser()
.from(bucketName)
.findObject(objectName, 'id,version,metadata,user_metadata,created_at')
.findObject(objectName, 'id,name,version,metadata,user_metadata,created_at')
} else {
obj = await request.storage
.from(bucketName)
.findObject(objectName, 'id,version,metadata,user_metadata,created_at')
.findObject(objectName, 'id,name,version,metadata,user_metadata,created_at')
}

return request.storage.renderer(method).render(request, response, {
Expand Down
4 changes: 2 additions & 2 deletions src/http/routes/tus/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ export async function onUploadFinish(

const uploader = new Uploader(req.upload.storage.backend, req.upload.storage.db)
let customMd: undefined | Record<string, string> = undefined
if (upload.metadata?.userMetadata) {
if (upload.metadata?.metadata) {
try {
customMd = JSON.parse(upload.metadata.userMetadata)
customMd = JSON.parse(upload.metadata.metadata)
} catch (e) {
// no-op
}
Expand Down
3 changes: 2 additions & 1 deletion src/storage/backend/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface BrowserCacheHeaders {
*/
export type ObjectResponse = {
metadata: ObjectMetadata
httpStatusCode: number
body?: ReadableStream<any> | Readable | Blob | Buffer
}

Expand All @@ -29,7 +30,7 @@ export type ObjectMetadata = {
lastModified?: Date
eTag: string
contentRange?: string
httpStatusCode: number
httpStatusCode?: number
}

export type UploadPart = {
Expand Down
2 changes: 2 additions & 0 deletions src/storage/backend/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class FileBackend implements StorageBackendAdapter {
eTag: checksum,
contentLength: chunkSize,
},
httpStatusCode: 206,
body,
}
} else {
Expand All @@ -110,6 +111,7 @@ export class FileBackend implements StorageBackendAdapter {
contentLength: fileSize,
},
body,
httpStatusCode: 200,
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/storage/backend/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface S3ClientOptions {

/**
* S3Backend
* Interacts with an s3-compatible file system with this S3Adapter
* Interacts with a s3-compatible file system with this S3Adapter
*/
export class S3Backend implements StorageBackendAdapter {
client: S3Client
Expand Down Expand Up @@ -118,9 +118,10 @@ export class S3Backend implements StorageBackendAdapter {
lastModified: data.LastModified,
contentRange: data.ContentRange,
contentLength: data.ContentLength || 0,
httpStatusCode: data.$metadata.httpStatusCode || 200,
size: data.ContentLength || 0,
httpStatusCode: data.$metadata.httpStatusCode || 200,
},
httpStatusCode: data.$metadata.httpStatusCode || 200,
body: data.Body,
}
}
Expand Down
26 changes: 21 additions & 5 deletions src/storage/renderer/info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Obj } from '@storage/schemas'
import { HeadRenderer } from './head'
import { FastifyRequest } from 'fastify'
import { FastifyRequest, FastifyReply } from 'fastify'
import { AssetResponse, RenderOptions } from './renderer'
import { Obj } from '@storage/schemas'
import { FastifyReply } from 'fastify/types/reply'

/**
* HeadRenderer
Expand All @@ -16,7 +15,18 @@ export class InfoRenderer extends HeadRenderer {

return {
...headAsset,
body: obj,
body: {
id: obj.id,
name: obj.name,
version: obj.version,
size: obj.metadata?.size ?? null,
content_type: obj.metadata?.mimetype ?? null,
cache_control: obj.metadata?.cacheControl ?? null,
etag: obj.metadata?.eTag ?? null,
metadata: obj.user_metadata,
last_modified: obj.metadata?.lastModified ?? null,
created_at: obj.created_at,
},
}
}

Expand All @@ -26,6 +36,12 @@ export class InfoRenderer extends HeadRenderer {
data: AssetResponse,
options: RenderOptions
) {
// no-op
response
.status(data.metadata.httpStatusCode ?? 200)
.header('Content-Type', 'application/json')
.header('ETag', data.metadata.eTag)
.header('Content-Length', data.metadata.contentLength)
.header('Last-Modified', data.metadata.lastModified?.toUTCString())
.header('CacheControl', data.metadata.cacheControl)
}
}
2 changes: 1 addition & 1 deletion src/storage/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export class Uploader {

body = formData.file
/* @ts-expect-error: https://github.com/aws/aws-sdk-js-v3/issues/2085 */
const customMd = formData.fields.userMetadata?.value
const customMd = formData.fields.metadata?.value ?? formData.fields.userMetadata?.value
/* @ts-expect-error: https://github.com/aws/aws-sdk-js-v3/issues/2085 */
mimeType = formData.fields.contentType?.value || formData.mimetype
cacheControl = cacheTime ? `max-age=${cacheTime}` : 'no-cache'
Expand Down
1 change: 1 addition & 0 deletions src/test/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ beforeAll(() => {
cacheControl: 'no-cache',
contentLength: 3746,
},
httpStatusCode: 200,
body: Buffer.from(''),
})
})
Expand Down
1 change: 1 addition & 0 deletions src/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function useMockObject() {
cacheControl: 'no-cache',
contentLength: 3746,
},
httpStatusCode: 200,
body: Buffer.from(''),
})

Expand Down
6 changes: 3 additions & 3 deletions src/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ describe('testing POST object via multipart upload', () => {
const form = new FormData()
form.append('file', fs.createReadStream(`./src/test/assets/sadcat.jpg`))
form.append(
'userMetadata',
'metadata',
JSON.stringify({
test1: 'test1',
test2: 'test2',
Expand Down Expand Up @@ -438,7 +438,7 @@ describe('testing POST object via multipart upload', () => {
const form = new FormData()
form.append('file', fs.createReadStream(`./src/test/assets/sadcat.jpg`))
form.append(
'userMetadata',
'metadata',
JSON.stringify({
test1: 'test1',
test2: 'test2',
Expand Down Expand Up @@ -468,7 +468,7 @@ describe('testing POST object via multipart upload', () => {

const data = await response.json()

expect(data.user_metadata).toEqual({
expect(data.metadata).toEqual({
test1: 'test1',
test2: 'test2',
})
Expand Down
4 changes: 2 additions & 2 deletions src/test/tus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('Tus multipart', () => {
objectName: objectName,
contentType: 'image/jpeg',
cacheControl: '3600',
userMetadata: JSON.stringify({
metadata: JSON.stringify({
test1: 'test1',
test2: 'test2',
}),
Expand Down Expand Up @@ -267,7 +267,7 @@ describe('Tus multipart', () => {
objectName: objectName,
contentType: 'image/jpeg',
cacheControl: '3600',
userMetadata: JSON.stringify({
metadata: JSON.stringify({
test1: 'test1',
test3: 'test3',
}),
Expand Down

0 comments on commit 556b7f4

Please sign in to comment.