-
Notifications
You must be signed in to change notification settings - Fork 1
/
editorTools.ts
160 lines (144 loc) · 3.64 KB
/
editorTools.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
156
157
158
159
160
import type { FastifyPluginCallback } from 'fastify';
import type EditorToolsService from '@domain/service/editorTools.js';
import type { AddEditorToolDto } from './dto/AddEditorTool.dto.js';
import type FileUploaderService from '@domain/service/fileUploader.service.js';
import fastifyMultipart from '@fastify/multipart';
import { createFileId } from '@infrastructure/utils/id.js';
/**
* Interface for the editor tools router
*/
interface EditorToolsRouterOptions {
/**
* Editor tools service instance
*/
editorToolsService: EditorToolsService;
/**
* File uploader service instance, needed to upload tool cover
*/
fileUploaderService: FileUploaderService;
/**
* Limit for uploaded files size
*/
fileSizeLimit: number;
}
/**
* Manages user
* @param fastify - fastify instance
* @param opts - empty options
* @param done - callback
*/
const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = async (fastify, opts, done) => {
/**
* Manage editor tools data
*/
const { editorToolsService, fileUploaderService } = opts;
await fastify.register(fastifyMultipart, {
limits: {
fieldSize: opts.fileSizeLimit,
},
attachFieldsToBody: true,
});
/**
* Get all avaiable editor tools
*/
fastify.get('/all', {
schema: {
response: {
'2xx': {
description: 'Editor tool fields',
content: {
'application/json': {
schema: {
data: {
type: 'array',
items: {
$ref: 'EditorToolSchema',
},
},
},
},
},
},
},
},
}, async (_, reply) => {
const tools = await editorToolsService.getTools();
return reply.send({
data: tools,
});
});
/**
* Add editor tool to the library of all tools
*/
fastify.post<{
Body: AddEditorToolDto;
}>('/add-tool', {
config: {
/**
* @todo check if user is superadmin
*/
policy: [
'authRequired',
],
},
schema: {
consumes: ['multipart/form-data'],
body: {
$ref: 'AddEditorToolSchema',
},
response: {
'2xx': {
description: 'Editor tool fields',
content: {
'application/json': {
schema: {
data: {
$ref: 'EditorToolSchema',
},
},
},
},
},
},
},
}, async (request, reply) => {
const editorTool = request.body;
const userId = request.userId as number;
let coverKey: string | undefined = undefined;
if (editorTool.cover) {
const coverBuffer = await editorTool.cover.toBuffer();
coverKey = await fileUploaderService.uploadFile({
data: coverBuffer,
name: createFileId(),
mimetype: editorTool.cover.mimetype,
}, {
isEditorToolCover: true,
}, {
userId,
});
}
const source: {
cdn: string;
} = editorTool.source?.value !== undefined
? JSON.parse(editorTool.source.value) as {
cdn: string;
}
: {
cdn: '',
};
const tool = await editorToolsService.addTool({
title: editorTool.title?.value ?? '',
name: editorTool.name?.value ?? '',
exportName: editorTool.exportName.value ?? '',
description: editorTool.description.value ?? '',
source,
isDefault: Boolean(editorTool.isDefault?.value ?? false),
cover: coverKey ?? '',
}, userId);
return reply.send({
data: tool,
});
});
done();
};
export default EditorToolsRouter;