-
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathcopyObject.ts
More file actions
89 lines (81 loc) · 2.93 KB
/
copyObject.ts
File metadata and controls
89 lines (81 loc) · 2.93 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, FastifyRequest } from 'fastify'
import { FromSchema } from 'json-schema-to-ts'
import { createDefaultSchema } from '../../routes-helper'
import { AuthenticatedRequest } from '../../types'
import { ROUTE_OPERATIONS } from '../operations'
import { parseUserMetadata } from '@storage/uploader'
import { objectSchema } from '@storage/schemas'
const copyRequestBodySchema = {
type: 'object',
properties: {
bucketId: { type: 'string', examples: ['avatars'] },
sourceKey: { type: 'string', examples: ['folder/source.png'] },
destinationBucket: { type: 'string', examples: ['users'] },
destinationKey: { type: 'string', examples: ['folder/destination.png'] },
metadata: {
type: 'object',
properties: {
cacheControl: { type: 'string' },
mimetype: { type: 'string' },
},
},
copyMetadata: { type: 'boolean', examples: [true] },
},
required: ['sourceKey', 'bucketId', 'destinationKey'],
} as const
const successResponseSchema = {
type: 'object',
properties: {
Id: { type: 'string' },
Key: { type: 'string', examples: ['folder/destination.png'] },
...objectSchema.properties,
},
required: ['Key'],
}
interface copyRequestInterface extends AuthenticatedRequest {
Body: FromSchema<typeof copyRequestBodySchema>
}
export default async function routes(fastify: FastifyInstance) {
const summary = 'Copies an object'
const schema = createDefaultSchema(successResponseSchema, {
body: copyRequestBodySchema,
summary,
tags: ['object'],
})
fastify.post<copyRequestInterface>(
'/copy',
{
schema,
config: {
operation: { type: ROUTE_OPERATIONS.COPY_OBJECT },
resources: (req: FastifyRequest<copyRequestInterface>) => {
const { sourceKey, destinationKey, bucketId, destinationBucket } = req.body
return [`${bucketId}/${sourceKey}`, `${destinationBucket || bucketId}/${destinationKey}`]
},
},
},
async (request, response) => {
const { sourceKey, destinationKey, bucketId, destinationBucket, metadata } = request.body
const destinationBucketId = destinationBucket || bucketId
const userMetadata = request.headers['x-metadata']
const result = await request.storage.from(bucketId).copyObject({
sourceKey,
destinationBucket: destinationBucketId,
destinationKey,
owner: request.owner,
userMetadata:
typeof userMetadata === 'string' ? parseUserMetadata(userMetadata) : undefined,
metadata: metadata,
copyMetadata: request.body.copyMetadata ?? true,
upsert: request.headers['x-upsert'] === 'true',
signal: request.signals.disconnect.signal,
})
return response.status(result.httpStatusCode ?? 200).send({
// Deprecated, remove in next major
Id: result.destObject.id,
Key: `${destinationBucketId}/${destinationKey}`,
...result.destObject,
})
}
)
}