|
| 1 | +import { FastifyInstance } from 'fastify' |
| 2 | +import { FromSchema } from 'json-schema-to-ts' |
| 3 | +import { Bucket } from '../../types/types' |
| 4 | +import { getPostgrestClient, transformPostgrestError } from '../../utils' |
| 5 | +import { getConfig } from '../../utils/config' |
| 6 | +import { getObject, initClient } from '../../utils/s3' |
| 7 | + |
| 8 | +const { region, projectRef, globalS3Bucket, globalS3Endpoint, serviceKey } = getConfig() |
| 9 | +const client = initClient(region, globalS3Endpoint) |
| 10 | + |
| 11 | +const getPublicObjectParamsSchema = { |
| 12 | + type: 'object', |
| 13 | + properties: { |
| 14 | + bucketName: { type: 'string', example: 'avatars' }, |
| 15 | + '*': { type: 'string', example: 'folder/cat.png' }, |
| 16 | + }, |
| 17 | + required: ['bucketName', '*'], |
| 18 | +} as const |
| 19 | +interface getObjectRequestInterface { |
| 20 | + Params: FromSchema<typeof getPublicObjectParamsSchema> |
| 21 | +} |
| 22 | + |
| 23 | +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types |
| 24 | +export default async function routes(fastify: FastifyInstance) { |
| 25 | + const summary = 'Retrieve a public object' |
| 26 | + fastify.get<getObjectRequestInterface>( |
| 27 | + '/public/:bucketName/*', |
| 28 | + { |
| 29 | + // @todo add success response schema here |
| 30 | + schema: { |
| 31 | + params: getPublicObjectParamsSchema, |
| 32 | + summary, |
| 33 | + response: { '4xx': { $ref: 'errorSchema#' } }, |
| 34 | + tags: ['object'], |
| 35 | + }, |
| 36 | + }, |
| 37 | + async (request, response) => { |
| 38 | + const { bucketName } = request.params |
| 39 | + const objectName = request.params['*'] |
| 40 | + |
| 41 | + const superUserPostgrest = getPostgrestClient(serviceKey) |
| 42 | + const { error, status } = await superUserPostgrest |
| 43 | + .from<Bucket>('buckets') |
| 44 | + .select('id, public') |
| 45 | + .eq('id', bucketName) |
| 46 | + .eq('public', true) |
| 47 | + .single() |
| 48 | + |
| 49 | + if (error) { |
| 50 | + request.log.error({ error }, 'error finding public bucket') |
| 51 | + return response.status(400).send(transformPostgrestError(error, status)) |
| 52 | + } |
| 53 | + |
| 54 | + const s3Key = `${projectRef}/${bucketName}/${objectName}` |
| 55 | + request.log.info(s3Key) |
| 56 | + try { |
| 57 | + const data = await getObject(client, globalS3Bucket, s3Key) |
| 58 | + return response |
| 59 | + .status(data.$metadata.httpStatusCode ?? 200) |
| 60 | + .header('Content-Type', data.ContentType) |
| 61 | + .header('Cache-Control', data.CacheControl) |
| 62 | + .header('ETag', data.ETag) |
| 63 | + .header('Last-Modified', data.LastModified) |
| 64 | + .send(data.Body) |
| 65 | + } catch (err) { |
| 66 | + if (err.$metadata?.httpStatusCode === 404) { |
| 67 | + return response.status(404).send() |
| 68 | + } else { |
| 69 | + return response.status(400).send({ |
| 70 | + message: err.message, |
| 71 | + statusCode: err.$metadata?.httpStatusCode, |
| 72 | + error: err.message, |
| 73 | + }) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + ) |
| 78 | +} |
0 commit comments