-
-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathgetPublicObject.ts
More file actions
89 lines (81 loc) · 2.56 KB
/
getPublicObject.ts
File metadata and controls
89 lines (81 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { FastifyInstance } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { getConfig } from '../../../config'
import { ROUTE_OPERATIONS } from '../operations'
const { storageS3Bucket } = getConfig()
const getPublicObjectParamsSchema = {
type: 'object',
properties: {
bucketName: { type: 'string', examples: ['avatars'] },
'*': { type: 'string', examples: ['folder/cat.png'] },
},
required: ['bucketName', '*'],
} as const
const getObjectQuerySchema = {
type: 'object',
properties: {
download: { type: 'string', examples: ['filename.jpg', null] },
},
} as const
interface getObjectRequestInterface {
Params: FromSchema<typeof getPublicObjectParamsSchema>
Headers: {
range?: string
}
Querystring: FromSchema<typeof getObjectQuerySchema>
}
export default async function routes(fastify: FastifyInstance) {
const summary = 'Retrieve an object from a public bucket'
fastify.get<getObjectRequestInterface>(
'/public/:bucketName/*',
{
// @todo add success response schema here
exposeHeadRoute: false,
schema: {
params: getPublicObjectParamsSchema,
querystring: getObjectQuerySchema,
summary,
response: { '4xx': { $ref: 'errorSchema#', description: 'Error response' } },
tags: ['object'],
},
config: {
operation: { type: ROUTE_OPERATIONS.GET_PUBLIC_OBJECT },
},
},
async (request, response) => {
const { bucketName } = request.params
const objectName = request.params['*']
const { download } = request.query
const bucketRef = request.storage.asSuperUser().from(bucketName)
const [, obj] = await Promise.all([
request.storage.asSuperUser().findBucket({
bucketId: bucketName,
columns: 'id,public',
filters: {
isPublic: true,
},
signal: request.signals.disconnect.signal,
}),
bucketRef.findObject({
objectName,
columns: 'id,version,metadata',
signal: request.signals.disconnect.signal,
}),
])
// send the object from s3
const s3Key = request.storage.location.getKeyLocation({
tenantId: request.tenantId,
bucketId: bucketName,
objectName,
})
return request.storage.renderer('asset').render(request, response, {
bucket: storageS3Bucket,
key: s3Key,
version: obj.version,
download,
xRobotsTag: obj.metadata?.['xRobotsTag'] as string | undefined,
signal: request.signals.disconnect.signal,
})
}
)
}