|
| 1 | +import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify' |
| 2 | +import { FromSchema } from 'json-schema-to-ts' |
| 3 | +import { IncomingMessage, Server, ServerResponse } from 'http' |
| 4 | +import { AuthenticatedRangeRequest, Obj } from '../../types/types' |
| 5 | +import { isValidKey, transformPostgrestError } from '../../utils' |
| 6 | +import { getConfig } from '../../utils/config' |
| 7 | +import { normalizeContentType } from '../../utils' |
| 8 | +import { createResponse } from '../../utils/generic-routes' |
| 9 | +import { S3Backend } from '../../backend/s3' |
| 10 | +import { FileBackend } from '../../backend/file' |
| 11 | +import { GenericStorageBackend } from '../../backend/generic' |
| 12 | + |
| 13 | +const { region, globalS3Bucket, globalS3Endpoint, storageBackendType } = getConfig() |
| 14 | +let storageBackend: GenericStorageBackend |
| 15 | + |
| 16 | +if (storageBackendType === 'file') { |
| 17 | + storageBackend = new FileBackend() |
| 18 | +} else { |
| 19 | + storageBackend = new S3Backend(region, globalS3Endpoint) |
| 20 | +} |
| 21 | + |
| 22 | +const getObjectParamsSchema = { |
| 23 | + type: 'object', |
| 24 | + properties: { |
| 25 | + bucketName: { type: 'string', examples: ['avatars'] }, |
| 26 | + '*': { type: 'string', examples: ['folder/cat.png'] }, |
| 27 | + }, |
| 28 | + required: ['bucketName', '*'], |
| 29 | +} as const |
| 30 | + |
| 31 | +interface getObjectRequestInterface extends AuthenticatedRangeRequest { |
| 32 | + Params: FromSchema<typeof getObjectParamsSchema> |
| 33 | +} |
| 34 | + |
| 35 | +async function requestHandler( |
| 36 | + request: FastifyRequest<getObjectRequestInterface, Server, IncomingMessage>, |
| 37 | + response: FastifyReply< |
| 38 | + Server, |
| 39 | + IncomingMessage, |
| 40 | + ServerResponse, |
| 41 | + getObjectRequestInterface, |
| 42 | + unknown |
| 43 | + >, |
| 44 | + publicRoute = false |
| 45 | +) { |
| 46 | + const { bucketName } = request.params |
| 47 | + const objectName = request.params['*'] |
| 48 | + |
| 49 | + if (!isValidKey(objectName) || !isValidKey(bucketName)) { |
| 50 | + return response |
| 51 | + .status(400) |
| 52 | + .send(createResponse('The key contains invalid characters', '400', 'Invalid key')) |
| 53 | + } |
| 54 | + |
| 55 | + const postgrest = publicRoute ? request.superUserPostgrest : request.postgrest |
| 56 | + const objectResponse = await postgrest |
| 57 | + .from<Obj>('objects') |
| 58 | + .select('id') |
| 59 | + .match({ |
| 60 | + name: objectName, |
| 61 | + bucket_id: bucketName, |
| 62 | + }) |
| 63 | + .single() |
| 64 | + |
| 65 | + if (objectResponse.error) { |
| 66 | + const { status, error } = objectResponse |
| 67 | + request.log.error({ error }, 'error object') |
| 68 | + return response.status(400).send(transformPostgrestError(error, status)) |
| 69 | + } |
| 70 | + |
| 71 | + const s3Key = `${request.tenantId}/${bucketName}/${objectName}` |
| 72 | + |
| 73 | + try { |
| 74 | + const data = await storageBackend.headObject(globalS3Bucket, s3Key) |
| 75 | + |
| 76 | + response |
| 77 | + .status(data.httpStatusCode ?? 200) |
| 78 | + .header('Content-Type', normalizeContentType(data.mimetype)) |
| 79 | + .header('Cache-Control', data.cacheControl) |
| 80 | + .header('Content-Length', data.size) |
| 81 | + .header('ETag', data.eTag) |
| 82 | + .header('Last-Modified', data.lastModified?.toUTCString()) |
| 83 | + |
| 84 | + return response.send() |
| 85 | + } catch (err: any) { |
| 86 | + if (err.$metadata?.httpStatusCode === 304) { |
| 87 | + return response.status(304).send() |
| 88 | + } |
| 89 | + request.log.error(err) |
| 90 | + return response.status(400).send(createResponse(err.message, '400', err.name)) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +export async function publicRoutes(fastify: FastifyInstance) { |
| 95 | + fastify.head<getObjectRequestInterface>( |
| 96 | + '/public/:bucketName/*', |
| 97 | + { |
| 98 | + schema: { |
| 99 | + params: getObjectParamsSchema, |
| 100 | + headers: { $ref: 'authSchema#' }, |
| 101 | + summary: 'Get object info', |
| 102 | + description: 'returns object info', |
| 103 | + response: { '4xx': { $ref: 'errorSchema#' } }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + async (request, response) => { |
| 107 | + return requestHandler(request, response, true) |
| 108 | + } |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +export async function authenticatedRoutes(fastify: FastifyInstance) { |
| 113 | + const summary = 'Retrieve object info' |
| 114 | + fastify.head<getObjectRequestInterface>( |
| 115 | + '/authenticated/:bucketName/*', |
| 116 | + { |
| 117 | + schema: { |
| 118 | + params: getObjectParamsSchema, |
| 119 | + headers: { $ref: 'authSchema#' }, |
| 120 | + summary, |
| 121 | + response: { '4xx': { $ref: 'errorSchema#', description: 'Error response' } }, |
| 122 | + tags: ['object'], |
| 123 | + }, |
| 124 | + }, |
| 125 | + async (request, response) => { |
| 126 | + return requestHandler(request, response) |
| 127 | + } |
| 128 | + ) |
| 129 | + |
| 130 | + fastify.head<getObjectRequestInterface>( |
| 131 | + '/:bucketName/*', |
| 132 | + { |
| 133 | + schema: { |
| 134 | + params: getObjectParamsSchema, |
| 135 | + headers: { $ref: 'authSchema#' }, |
| 136 | + summary, |
| 137 | + description: 'use HEAD /object/authenticated/{bucketName} instead', |
| 138 | + response: { '4xx': { $ref: 'errorSchema#' } }, |
| 139 | + tags: ['deprecated'], |
| 140 | + }, |
| 141 | + }, |
| 142 | + async (request, response) => { |
| 143 | + return requestHandler(request, response) |
| 144 | + } |
| 145 | + ) |
| 146 | +} |
0 commit comments