Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/core/storage-js/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export interface FileOptions {
* the `Content-Type` header value. Should be specified if using a `fileBody` that is neither `Blob` nor `File` nor `FormData`, otherwise will default to `text/plain;charset=UTF-8`.
*/
contentType?: string
/**
* The `Content-Encoding` header value (e.g., `gzip`).
*/
contentEncoding?: string
/**
* When upsert is set to true, the file is overwritten if it exists. When set to false, an error is thrown if the object already exists. Defaults to false.
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/core/storage-js/src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export default class StorageFileApi {
headers['cache-control'] = `max-age=${options.cacheControl}`
headers['content-type'] = options.contentType as string

if (options.contentEncoding) {
headers['content-encoding'] = options.contentEncoding
}

if (metadata) {
headers['x-metadata'] = this.toBase64(this.encodeMetadata(metadata))
}
Expand Down Expand Up @@ -291,6 +295,9 @@ export default class StorageFileApi {
body = fileBody
headers['cache-control'] = `max-age=${options.cacheControl}`
headers['content-type'] = options.contentType as string
if (options.contentEncoding) {
headers['content-encoding'] = options.contentEncoding
}
}

const data = await put(this.fetch, url.toString(), body as object, { headers })
Expand Down
13 changes: 13 additions & 0 deletions packages/core/storage-js/test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ReadableStream from 'node:stream'
import { StorageApiError, StorageError } from '../src/lib/errors'
import BlobDownloadBuilder from '../src/packages/BlobDownloadBuilder'
import StreamDownloadBuilder from '../src/packages/StreamDownloadBuilder'
import StorageFileApi from '../src/packages/StorageFileApi'

// TODO: need to setup storage-api server for this test
const URL = 'http://localhost:8000/storage/v1'
Expand Down Expand Up @@ -851,5 +852,17 @@ describe('StorageFileApi Edge Cases', () => {
expect(body).toBe(testFormData)
expect(headers[testHeaderKey]).toBe(testHeaderValue)
})

test('upload sets Content-Encoding when provided', async () => {
await storage.from('test-bucket').upload('test-path', new Uint8Array([1, 2, 3]), {
contentType: 'application/octet-stream',
contentEncoding: 'gzip',
})

expect(mockPost).toHaveBeenCalled()
const [, , body, { headers }] = mockPost.mock.calls[0]
expect(headers['content-type']).toBe('application/octet-stream')
expect(headers['content-encoding']).toBe('gzip')
})
})
})
Loading