|
| 1 | +import { S3ProtocolHandler } from '@storage/protocols/s3/s3-handler' |
| 2 | +import { S3Router } from '../router' |
| 3 | +import { ROUTE_OPERATIONS } from '../../operations' |
| 4 | +import { Multipart, MultipartValue } from '@fastify/multipart' |
| 5 | +import { fileUploadFromRequest, getStandardMaxFileSizeLimit } from '@storage/uploader' |
| 6 | +import { ERRORS } from '@internal/errors' |
| 7 | +import { pipeline } from 'stream/promises' |
| 8 | +import { ByteLimitTransformStream } from '@storage/protocols/s3/byte-limit-stream' |
| 9 | +import stream from 'stream' |
| 10 | + |
| 11 | +const PutObjectInput = { |
| 12 | + summary: 'Put Object', |
| 13 | + Params: { |
| 14 | + type: 'object', |
| 15 | + properties: { |
| 16 | + Bucket: { type: 'string' }, |
| 17 | + '*': { type: 'string' }, |
| 18 | + }, |
| 19 | + required: ['Bucket', '*'], |
| 20 | + }, |
| 21 | + Querystring: { |
| 22 | + type: 'object', |
| 23 | + }, |
| 24 | + Headers: { |
| 25 | + type: 'object', |
| 26 | + properties: { |
| 27 | + authorization: { type: 'string' }, |
| 28 | + host: { type: 'string' }, |
| 29 | + 'x-amz-content-sha256': { type: 'string' }, |
| 30 | + 'x-amz-date': { type: 'string' }, |
| 31 | + 'content-type': { type: 'string' }, |
| 32 | + 'content-length': { type: 'integer' }, |
| 33 | + 'cache-control': { type: 'string' }, |
| 34 | + 'content-disposition': { type: 'string' }, |
| 35 | + 'content-encoding': { type: 'string' }, |
| 36 | + expires: { type: 'string' }, |
| 37 | + }, |
| 38 | + required: ['content-length'], |
| 39 | + }, |
| 40 | +} as const |
| 41 | + |
| 42 | +const PostFormInput = { |
| 43 | + summary: 'PostForm Object', |
| 44 | + Params: { |
| 45 | + type: 'object', |
| 46 | + properties: { |
| 47 | + Bucket: { type: 'string' }, |
| 48 | + }, |
| 49 | + required: ['Bucket'], |
| 50 | + }, |
| 51 | +} as const |
| 52 | + |
| 53 | +export default function PutObject(s3Router: S3Router) { |
| 54 | + s3Router.put( |
| 55 | + '/:Bucket/*', |
| 56 | + { |
| 57 | + schema: PutObjectInput, |
| 58 | + operation: ROUTE_OPERATIONS.S3_UPLOAD, |
| 59 | + disableContentTypeParser: true, |
| 60 | + }, |
| 61 | + async (req, ctx) => { |
| 62 | + const s3Protocol = new S3ProtocolHandler(ctx.storage, ctx.tenantId, ctx.owner) |
| 63 | + |
| 64 | + const metadata = s3Protocol.parseMetadataHeaders(req.Headers) |
| 65 | + const contentLength = req.Headers['content-length'] |
| 66 | + let key = req.Params['*'] |
| 67 | + |
| 68 | + if (key.endsWith('/') && contentLength === 0) { |
| 69 | + // Consistent with how supabase Storage handles empty folders |
| 70 | + key += '.emptyFolderPlaceholder' |
| 71 | + } |
| 72 | + |
| 73 | + const bucket = await ctx.storage |
| 74 | + .asSuperUser() |
| 75 | + .findBucket(req.Params.Bucket, 'id,file_size_limit,allowed_mime_types') |
| 76 | + |
| 77 | + const uploadRequest = await fileUploadFromRequest(ctx.req, { |
| 78 | + objectName: key, |
| 79 | + allowedMimeTypes: bucket.allowed_mime_types || [], |
| 80 | + fileSizeLimit: bucket.file_size_limit || undefined, |
| 81 | + }) |
| 82 | + |
| 83 | + return s3Protocol.putObject( |
| 84 | + { |
| 85 | + Body: uploadRequest.body, |
| 86 | + Bucket: req.Params.Bucket, |
| 87 | + Key: key, |
| 88 | + CacheControl: uploadRequest.cacheControl, |
| 89 | + ContentType: uploadRequest.mimeType, |
| 90 | + Expires: req.Headers?.['expires'] ? new Date(req.Headers?.['expires']) : undefined, |
| 91 | + ContentEncoding: req.Headers?.['content-encoding'], |
| 92 | + Metadata: metadata, |
| 93 | + }, |
| 94 | + { signal: ctx.signals.body, isTruncated: uploadRequest.isTruncated } |
| 95 | + ) |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + s3Router.post( |
| 100 | + '/:Bucket|content-type=multipart/form-data', |
| 101 | + { |
| 102 | + schema: PostFormInput, |
| 103 | + operation: ROUTE_OPERATIONS.S3_UPLOAD, |
| 104 | + acceptMultiformData: true, |
| 105 | + }, |
| 106 | + async (req, ctx) => { |
| 107 | + const s3Protocol = new S3ProtocolHandler(ctx.storage, ctx.tenantId, ctx.owner) |
| 108 | + |
| 109 | + const file = ctx.req.multiPartFileStream |
| 110 | + |
| 111 | + if (!file) { |
| 112 | + throw ERRORS.InvalidParameter('Missing file') |
| 113 | + } |
| 114 | + |
| 115 | + const bucket = await ctx.storage |
| 116 | + .asSuperUser() |
| 117 | + .findBucket(req.Params.Bucket, 'id,file_size_limit,allowed_mime_types') |
| 118 | + |
| 119 | + const metadata = s3Protocol.parseMetadataHeaders(file?.fields || {}) |
| 120 | + const expiresField = normaliseFormDataField(file?.fields?.Expires) as string | undefined |
| 121 | + |
| 122 | + const maxFileSize = await getStandardMaxFileSizeLimit(ctx.tenantId, bucket.file_size_limit) |
| 123 | + |
| 124 | + return pipeline(file.file, new ByteLimitTransformStream(maxFileSize), async (fileStream) => { |
| 125 | + return s3Protocol.putObject( |
| 126 | + { |
| 127 | + Body: fileStream as stream.Readable, |
| 128 | + Bucket: req.Params.Bucket, |
| 129 | + Key: normaliseFormDataField(file?.fields?.key) as string, |
| 130 | + CacheControl: normaliseFormDataField(file?.fields?.['Cache-Control']) as string, |
| 131 | + ContentType: normaliseFormDataField(file?.fields?.['Content-Type']) as string, |
| 132 | + Expires: expiresField ? new Date(expiresField) : undefined, |
| 133 | + ContentEncoding: normaliseFormDataField(file?.fields?.['Content-Encoding']) as string, |
| 134 | + Metadata: metadata, |
| 135 | + }, |
| 136 | + { signal: ctx.signals.body, isTruncated: () => file.file.truncated } |
| 137 | + ) |
| 138 | + }) |
| 139 | + } |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +function normaliseFormDataField(value: Multipart | Multipart[] | undefined) { |
| 144 | + if (!value) { |
| 145 | + return undefined |
| 146 | + } |
| 147 | + |
| 148 | + if (Array.isArray(value)) { |
| 149 | + return (value[0] as MultipartValue).value as string |
| 150 | + } |
| 151 | + |
| 152 | + if (value.type === 'field') { |
| 153 | + return value.value |
| 154 | + } |
| 155 | + |
| 156 | + return value.file |
| 157 | +} |
0 commit comments