diff --git a/infra/storage/Dockerfile b/infra/storage/Dockerfile index ed3db4f..3a89286 100644 --- a/infra/storage/Dockerfile +++ b/infra/storage/Dockerfile @@ -1,3 +1,3 @@ -FROM supabase/storage-api:v1.2.1 +FROM supabase/storage-api:v1.7.1 RUN apk add curl --no-cache \ No newline at end of file diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index 2468126..2bf5d72 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -11,15 +11,19 @@ export interface FetchOptions { noResolveJson?: boolean } -export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE' +export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' const _getErrorMessage = (err: any): string => err.msg || err.message || err.error_description || err.error || JSON.stringify(err) -const handleError = async (error: unknown, reject: (reason?: any) => void) => { +const handleError = async ( + error: unknown, + reject: (reason?: any) => void, + options?: FetchOptions +) => { const Res = await resolveResponse() - if (error instanceof Res) { + if (error instanceof Res && !options?.noResolveJson) { error .json() .then((err) => { @@ -46,7 +50,10 @@ const _getRequestParams = ( } params.headers = { 'Content-Type': 'application/json', ...options?.headers } - params.body = JSON.stringify(body) + + if (body) { + params.body = JSON.stringify(body) + } return { ...params, ...parameters } } @@ -66,7 +73,7 @@ async function _handleRequest( return result.json() }) .then((data) => resolve(data)) - .catch((error) => handleError(error, reject)) + .catch((error) => handleError(error, reject, options)) }) } @@ -99,6 +106,24 @@ export async function put( return _handleRequest(fetcher, 'PUT', url, options, parameters, body) } +export async function head( + fetcher: Fetch, + url: string, + options?: FetchOptions, + parameters?: FetchParameters +): Promise { + return _handleRequest( + fetcher, + 'HEAD', + url, + { + ...options, + noResolveJson: true, + }, + parameters + ) +} + export async function remove( fetcher: Fetch, url: string, diff --git a/src/lib/types.ts b/src/lib/types.ts index b77e533..3facffa 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -18,6 +18,7 @@ export interface FileObject { created_at: string last_accessed_at: string metadata: Record + user_metadata?: Record buckets: Bucket } @@ -43,6 +44,16 @@ export interface FileOptions { * The duplex option is a string parameter that enables or disables duplex streaming, allowing for both reading and writing data in the same stream. It can be passed as an option to the fetch() method. */ duplex?: string + + /** + * The metadata option is an object that allows you to store additional information about the file. This information can be used to filter and search for files. The metadata object can contain any key-value pairs you want to store. + */ + userMetadata?: Record + + /** + * Optionally add extra headers + */ + headers?: Record } export interface DestinationOptions { diff --git a/src/packages/StorageFileApi.ts b/src/packages/StorageFileApi.ts index 1f9417e..1289d9c 100644 --- a/src/packages/StorageFileApi.ts +++ b/src/packages/StorageFileApi.ts @@ -1,5 +1,5 @@ -import { isStorageError, StorageError } from '../lib/errors' -import { Fetch, get, post, remove } from '../lib/fetch' +import { isStorageError, StorageError, StorageUnknownError } from '../lib/errors' +import { Fetch, get, head, post, remove } from '../lib/fetch' import { resolveFetch } from '../lib/helpers' import { FileObject, @@ -80,22 +80,39 @@ export default class StorageFileApi { try { let body const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions } - const headers: Record = { + let headers: Record = { ...this.headers, ...(method === 'POST' && { 'x-upsert': String(options.upsert as boolean) }), } + const userMetadata = options.userMetadata + if (typeof Blob !== 'undefined' && fileBody instanceof Blob) { body = new FormData() body.append('cacheControl', options.cacheControl as string) body.append('', fileBody) + + if (userMetadata) { + body.append('userMetadata', this.encodeMetadata(userMetadata)) + } } else if (typeof FormData !== 'undefined' && fileBody instanceof FormData) { body = fileBody body.append('cacheControl', options.cacheControl as string) + if (userMetadata) { + body.append('userMetadata', this.encodeMetadata(userMetadata)) + } } else { body = fileBody headers['cache-control'] = `max-age=${options.cacheControl}` headers['content-type'] = options.contentType as string + + if (userMetadata) { + headers['x-metadata'] = this.toBase64(this.encodeMetadata(userMetadata)) + } + } + + if (fileOptions?.headers) { + headers = { ...headers, ...fileOptions.headers } } const cleanPath = this._removeEmptyFolders(path) @@ -525,6 +542,76 @@ export default class StorageFileApi { } } + /** + * Retrieves the details of an existing file. + * @param path + */ + async info( + path: string + ): Promise< + | { + data: FileObject + error: null + } + | { + data: null + error: StorageError + } + > { + const _path = this._getFinalPath(path) + + try { + const data = await get(this.fetch, `${this.url}/object/info/${_path}`, { + headers: this.headers, + }) + + return { data, error: null } + } catch (error) { + if (isStorageError(error)) { + return { data: null, error } + } + + throw error + } + } + + /** + * Retrieves the details of an existing file. + * @param path + */ + async exists( + path: string + ): Promise< + | { + data: boolean + error: null + } + | { + data: boolean + error: StorageError + } + > { + const _path = this._getFinalPath(path) + + try { + await head(this.fetch, `${this.url}/object/${_path}`, { + headers: this.headers, + }) + + return { data: true, error: null } + } catch (error) { + if (isStorageError(error) && error instanceof StorageUnknownError) { + const originalError = (error.originalError as unknown) as { status: number } + + if ([400, 404].includes(originalError?.status)) { + return { data: false, error } + } + } + + throw error + } + } + /** * A simple convenience function to get the URL for an asset in a public bucket. If you do not want to use this function, you can construct the public URL by concatenating the bucket URL with the path to the asset. * This function does not verify if the bucket is public. If a public URL is created for a bucket which is not public, you will not be able to download the asset. @@ -700,6 +787,17 @@ export default class StorageFileApi { } } + protected encodeMetadata(metadata: Record) { + return JSON.stringify(metadata) + } + + toBase64(data: string) { + if (typeof Buffer !== 'undefined') { + return Buffer.from(data).toString('base64') + } + return btoa(data) + } + private _getFinalPath(path: string) { return `${this.bucketId}/${path}` } diff --git a/test/storageFileApi.test.ts b/test/storageFileApi.test.ts index 78da022..4889234 100644 --- a/test/storageFileApi.test.ts +++ b/test/storageFileApi.test.ts @@ -163,6 +163,25 @@ describe('Object API', () => { expect(updateRes.data?.path).toEqual(uploadPath) }) + test('can upload with custom metadata', async () => { + const res = await storage.from(bucketName).upload(uploadPath, file, { + userMetadata: { + custom: 'metadata', + second: 'second', + third: 'third', + }, + }) + expect(res.error).toBeNull() + + const updateRes = await storage.from(bucketName).info(uploadPath) + expect(updateRes.error).toBeNull() + expect(updateRes.data?.user_metadata).toEqual({ + custom: 'metadata', + second: 'second', + third: 'third', + }) + }) + test('can upload a file within the file size limit', async () => { const bucketName = 'with-limit' + Date.now() await storage.createBucket(bucketName, { @@ -368,6 +387,41 @@ describe('Object API', () => { }), ]) }) + + test('get object info', async () => { + await storage.from(bucketName).upload(uploadPath, file) + const res = await storage.from(bucketName).info(uploadPath) + + expect(res.error).toBeNull() + expect(res.data).toEqual( + expect.objectContaining({ + created_at: expect.any(String), + id: expect.any(String), + metadata: { + cacheControl: 'max-age=3600', + contentLength: expect.any(Number), + eTag: expect.any(String), + httpStatusCode: 200, + lastModified: expect.any(String), + mimetype: expect.any(String), + size: expect.any(Number), + }, + user_metadata: {}, + version: expect.any(String), + }) + ) + }) + + test('check if object exists', async () => { + await storage.from(bucketName).upload(uploadPath, file) + const res = await storage.from(bucketName).exists(uploadPath) + + expect(res.error).toBeNull() + expect(res.data).toEqual(true) + + const resNotExists = await storage.from(bucketName).exists('do-not-exists') + expect(resNotExists.data).toEqual(false) + }) }) describe('Transformations', () => {