-
-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathgetObject.ts
More file actions
148 lines (135 loc) · 4.13 KB
/
getObject.ts
File metadata and controls
148 lines (135 loc) · 4.13 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { IncomingMessage, Server, ServerResponse } from 'http'
import { getConfig } from '../../../config'
import { AuthenticatedRangeRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'
import { ERRORS } from '@internal/errors'
import { Obj } from '@storage/schemas'
const { storageS3Bucket } = getConfig()
const getObjectParamsSchema = {
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 extends AuthenticatedRangeRequest {
Params: FromSchema<typeof getObjectParamsSchema>
Querystring: FromSchema<typeof getObjectQuerySchema>
}
async function requestHandler(
request: FastifyRequest<getObjectRequestInterface, Server, IncomingMessage>,
response: FastifyReply<
Server,
IncomingMessage,
ServerResponse,
getObjectRequestInterface,
unknown
>
) {
const { bucketName } = request.params
const { download } = request.query
const objectName = request.params['*']
// send the object from s3
const s3Key = request.storage.location.getKeyLocation({
tenantId: request.tenantId,
bucketId: bucketName,
objectName,
})
const bucket = await request.storage.asSuperUser().findBucket({
bucketId: bucketName,
columns: 'id,public',
filters: {
dontErrorOnEmpty: true,
},
signal: request.signals.disconnect.signal,
})
// The request is not authenticated
if (!request.isAuthenticated) {
// The bucket must be public to access its content
if (!bucket?.public) {
throw ERRORS.NoSuchBucket(bucketName)
}
}
// The request is authenticated
if (!bucket) {
throw ERRORS.NoSuchBucket(bucketName)
}
let obj: Obj | undefined
if (bucket.public) {
// request is authenticated but we still use the superUser as we don't need to check RLS
obj = await request.storage.asSuperUser().from(bucketName).findObject({
objectName,
columns: 'id, version, metadata',
signal: request.signals.disconnect.signal,
})
} else {
// request is authenticated use RLS
obj = await request.storage.from(bucketName).findObject({
objectName,
columns: 'id, version, metadata',
signal: request.signals.disconnect.signal,
})
}
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,
})
}
export default async function routes(fastify: FastifyInstance) {
const summary = 'Retrieve an object'
fastify.get<getObjectRequestInterface>(
'/authenticated/:bucketName/*',
{
exposeHeadRoute: false,
// @todo add success response schema here
schema: {
params: getObjectParamsSchema,
querystring: getObjectQuerySchema,
headers: { $ref: 'authSchema#' },
summary,
response: { '4xx': { $ref: 'errorSchema#', description: 'Error response' } },
tags: ['object'],
},
config: {
operation: { type: ROUTE_OPERATIONS.GET_AUTH_OBJECT },
},
},
async (request, response) => {
return requestHandler(request, response)
}
)
fastify.get<getObjectRequestInterface>(
'/:bucketName/*',
{
exposeHeadRoute: false,
// @todo add success response schema here
schema: {
params: getObjectParamsSchema,
summary: 'Get object',
description: 'Serve objects',
tags: ['object'],
response: { '4xx': { $ref: 'errorSchema#' } },
},
config: {
operation: { type: ROUTE_OPERATIONS.GET_AUTH_OBJECT },
allowInvalidJwt: true,
},
},
async (request, response) => {
return requestHandler(request, response)
}
)
}