Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/core/storage-js/src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,41 @@ export default class StorageFileApi extends BaseApiClient<StorageError> {
return this.handleOperation(async () => {
let body
const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
const headers: Record<string, string> = {
let headers: Record<string, string> = {
...this.headers,
...{ 'x-upsert': String(options.upsert as boolean) },
}

const metadata = options.metadata

if (typeof Blob !== 'undefined' && fileBody instanceof Blob) {
body = new FormData()
body.append('cacheControl', options.cacheControl as string)
if (metadata) {
body.append('metadata', this.encodeMetadata(metadata))
}
body.append('', fileBody)
} else if (typeof FormData !== 'undefined' && fileBody instanceof FormData) {
body = fileBody
body.append('cacheControl', options.cacheControl as string)
if (!body.has('cacheControl')) {
body.append('cacheControl', options.cacheControl as string)
}
if (metadata && !body.has('metadata')) {
body.append('metadata', this.encodeMetadata(metadata))
}
} else {
body = fileBody
headers['cache-control'] = `max-age=${options.cacheControl}`
headers['content-type'] = options.contentType as string
if (metadata) {
headers['x-metadata'] = this.toBase64(this.encodeMetadata(metadata))
}
}

if (fileOptions?.headers) {
for (const [key, value] of Object.entries(fileOptions.headers)) {
headers = setHeader(headers, key, value)
}
}

const data = await put(this.fetch, url.toString(), body as object, { headers })
Expand Down
75 changes: 75 additions & 0 deletions packages/core/storage-js/test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,5 +1206,80 @@ describe('StorageFileApi Edge Cases', () => {
expect(headers['Content-Type']).toBeUndefined()
expect(new Headers(headers).get('content-type')).toBe('image/png')
})

test('uploadToSignedUrl with metadata (Blob body)', async () => {
const testBlob = new Blob(['test content'], { type: 'text/plain' })
const metadata = { customKey: 'customValue', author: 'test' }

await storage
.from('test-bucket')
.uploadToSignedUrl('test-path', 'test-token', testBlob, { metadata })

expect(mockPut).toHaveBeenCalled()
const [, , body] = mockPut.mock.calls[0]
expect(body.constructor.name).toBe('FormData')
expect((body as FormData).get('metadata')).toBe(JSON.stringify(metadata))
})

test('uploadToSignedUrl with metadata (FormData body)', async () => {
const testFormData = new FormData()
testFormData.append('file', 'test content')
const metadata = { blah: 'abc213' }

await storage
.from('test-bucket')
.uploadToSignedUrl('test-path', 'test-token', testFormData, { metadata })

expect(mockPut).toHaveBeenCalled()
const [, , body] = mockPut.mock.calls[0] as [null, null, FormData]
expect(body).toBe(testFormData)
expect(body.get('metadata')).toBe(JSON.stringify(metadata))
})

test('uploadToSignedUrl with metadata (raw body sends x-metadata header)', async () => {
const metadata = { blah: 'abc213' }

await storage
.from('test-bucket')
.uploadToSignedUrl('test-path', 'test-token', 'test content', { metadata })

expect(mockPut).toHaveBeenCalled()
const [, , , { headers }] = mockPut.mock.calls[0]
expect(headers['x-metadata']).toBeDefined()
expect(Buffer.from(headers['x-metadata'], 'base64').toString('utf8')).toBe(
JSON.stringify(metadata)
)
})

test('uploadToSignedUrl passes headers', async () => {
const testFormData = new FormData()
testFormData.append('file', 'test content')

const testHeaderKey = 'x-test-header'
const testHeaderValue = 'abc123'

await storage.from('test-bucket').uploadToSignedUrl('test-path', 'test-token', testFormData, {
headers: { [testHeaderKey]: testHeaderValue },
})

expect(mockPut).toHaveBeenCalled()
const [, , body, { headers }] = mockPut.mock.calls[0]
expect(body).toBe(testFormData)
expect(headers[testHeaderKey]).toBe(testHeaderValue)
})

test('uploadToSignedUrl does not duplicate cacheControl when caller FormData has one', async () => {
const testFormData = new FormData()
testFormData.append('cacheControl', '7200')
testFormData.append('file', 'test content')

await storage
.from('test-bucket')
.uploadToSignedUrl('test-path', 'test-token', testFormData, { cacheControl: '3600' })

expect(mockPut).toHaveBeenCalled()
const [, , body] = mockPut.mock.calls[0] as [null, null, FormData]
expect(body.getAll('cacheControl')).toEqual(['7200'])
})
})
})
Loading