Skip to content

Commit

Permalink
fix: base64 encode for metadata in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Jul 11, 2024
1 parent d0f0348 commit 1161559
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 56 deletions.
65 changes: 16 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@aws-sdk/lib-storage": "3.515.0",
"@aws-sdk/s3-request-presigner": "3.421.0",
"@fastify/accepts": "^4.3.0",
"@fastify/multipart": "^7.6.0",
"@fastify/multipart": "^8.3.0",
"@fastify/rate-limit": "^7.6.0",
"@fastify/swagger": "^8.3.1",
"@fastify/swagger-ui": "^1.7.0",
Expand Down
7 changes: 2 additions & 5 deletions src/storage/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,9 @@ export class Uploader {
const customMd = request.headers['x-metadata']

if (typeof customMd === 'string') {
if (userMetadata && Buffer.byteLength(customMd, 'utf8') > MAX_CUSTOM_METADATA_SIZE) {
throw ERRORS.EntityTooLarge(undefined, 'metadata')
}

try {
userMetadata = JSON.parse(customMd)
const json = Buffer.from(customMd, 'base64').toString('utf8')
userMetadata = JSON.parse(json)
} catch (e) {
// no-op
}
Expand Down
41 changes: 40 additions & 1 deletion src/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe('testing POST object via multipart upload', () => {
expect(S3Backend.prototype.uploadObject).toHaveBeenCalled()
})

test('successfully uploading an object with custom metadata', async () => {
test('successfully uploading an object with custom metadata using form data', async () => {
const form = new FormData()
form.append('file', fs.createReadStream(`./src/test/assets/sadcat.jpg`))
form.append(
Expand Down Expand Up @@ -395,6 +395,45 @@ describe('testing POST object via multipart upload', () => {
})
})

test('successfully uploading an object with custom metadata using stream', async () => {
const file = fs.createReadStream(`./src/test/assets/sadcat.jpg`)

const headers = {
authorization: `Bearer ${serviceKey}`,
'x-upsert': 'true',
'x-metadata': Buffer.from(
JSON.stringify({
test1: 'test1',
test2: 'test2',
})
).toString('base64'),
}

const response = await app().inject({
method: 'POST',
url: '/object/bucket2/sadcat-upload3018.png',
headers,
payload: file,
})
expect(response.statusCode).toBe(200)
expect(S3Backend.prototype.uploadObject).toHaveBeenCalled()

const client = await getSuperuserPostgrestClient()

const object = await client
.table('objects')
.select('*')
.where('name', 'sadcat-upload3018.png')
.where('bucket_id', 'bucket2')
.first()

expect(object).not.toBeFalsy()
expect(object?.user_metadata).toEqual({
test1: 'test1',
test2: 'test2',
})
})

test('fetch object metadata', async () => {
const form = new FormData()
form.append('file', fs.createReadStream(`./src/test/assets/sadcat.jpg`))
Expand Down

0 comments on commit 1161559

Please sign in to comment.