-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.ts
156 lines (139 loc) · 3.56 KB
/
upload.ts
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
149
150
151
152
153
154
155
import type FileUploaderService from '@domain/service/fileUploader.service.js';
import { fastifyMultipart, type MultipartFile } from '@fastify/multipart';
import type { FastifyPluginCallback } from 'fastify';
import type NoteService from '@domain/service/note.js';
import useNoteResolver from '../middlewares/note/useNoteResolver.js';
import type { NoteAttachmentFileLocation } from '@domain/entities/file.js';
import { StatusCodes } from 'http-status-codes';
/**
* Interface for upload router options
*/
interface UploadRouterOptions {
/**
* File uploader service
*/
fileUploaderService: FileUploaderService;
/**
* Note service instance
*/
noteService: NoteService;
/**
* Limit for uploaded files size
*/
fileSizeLimit: number;
}
const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify, opts, done) => {
const { fileUploaderService } = opts;
/**
* Prepare note id resolver middleware
* It should be used in routes that accepts note public id
*/
const { noteResolver } = useNoteResolver(opts.noteService);
await fastify.register(fastifyMultipart, {
limits: {
fieldSize: opts.fileSizeLimit,
},
attachFieldsToBody: true,
});
fastify.post<{
Body: {
/**
* File to upload
*/
file: MultipartFile;
}
}>('/:notePublicId', {
config: {
policy: [
'authRequired',
'userCanEdit',
],
},
schema: {
consumes: [ 'multipart/form-data' ],
params: {
notePublicId: {
$ref: 'NoteSchema#/properties/id',
},
},
body: {
type: 'object',
required: [ 'file' ],
properties: {
file: { isFile: true },
},
},
response: {
'2xx': {
type: 'object',
description: 'File key to get it from the API',
key: {
$ref: 'UploadSchema#/properties/key',
},
},
},
},
attachValidation: true,
preHandler: [ noteResolver ],
}, async (request, reply) => {
/**
* @todo solve trouble with crashing app, when validations is not passed
*/
if (request.validationError) {
return reply.code(StatusCodes.BAD_REQUEST).send(request.validationError);
}
const { userId } = request;
const location: NoteAttachmentFileLocation = {
noteId: request.note!.id as number,
};
const uploadedFileKey = await fileUploaderService.uploadFile(
{
data: await request.body.file.toBuffer(),
mimetype: request.body.file.mimetype,
name: request.body.file.filename,
},
location,
{
userId: userId!,
}
);
return reply.send({
key: uploadedFileKey,
});
});
fastify.get<{
Params: {
key: string;
}
}>('/:notePublicId/:key', {
config: {
policy: [
'notePublicOrUserInTeam',
],
},
schema: {
params: {
key: {
$ref: 'UploadSchema#/properties/key',
},
},
response: {
'2xx': {
description: 'Generated buffer',
properties: {
fileData: { type: 'string' },
},
},
},
},
preHandler: [ noteResolver ],
}, async (request, reply) => {
const fileLocation: NoteAttachmentFileLocation = {
noteId: request.note!.id as number,
};
const fileData = await fileUploaderService.getFileData(request.params.key, fileLocation);
return reply.send(fileData);
});
done();
};
export default UploadRouter;