11import type { FastifyPluginCallback } from 'fastify' ;
22import type EditorToolsService from '@domain/service/editorTools.js' ;
33import type EditorTool from '@domain/entities/editorTools.js' ;
4+ import type { AddEditorToolDto } from './dto/AddEditorTool.dto.js' ;
5+ import type FileUploaderService from '@domain/service/fileUploader.service.js' ;
6+ import fastifyMultipart from '@fastify/multipart' ;
7+ import { createFileId } from '@infrastructure/utils/id.js' ;
48
59/**
610 * Interface for the editor tools router
@@ -10,6 +14,16 @@ interface EditorToolsRouterOptions {
1014 * Editor tools service instance
1115 */
1216 editorToolsService : EditorToolsService ;
17+
18+ /**
19+ * File uploader service instance, needed to upload tool cover
20+ */
21+ fileUploaderService : FileUploaderService ;
22+
23+ /**
24+ * Limit for uploaded files size
25+ */
26+ fileSizeLimit : number ;
1327}
1428
1529/**
@@ -18,11 +32,18 @@ interface EditorToolsRouterOptions {
1832 * @param opts - empty options
1933 * @param done - callback
2034 */
21- const EditorToolsRouter : FastifyPluginCallback < EditorToolsRouterOptions > = ( fastify , opts , done ) => {
35+ const EditorToolsRouter : FastifyPluginCallback < EditorToolsRouterOptions > = async ( fastify , opts , done ) => {
2236 /**
2337 * Manage editor tools data
2438 */
25- const { editorToolsService } = opts ;
39+ const { editorToolsService, fileUploaderService } = opts ;
40+
41+ await fastify . register ( fastifyMultipart , {
42+ limits : {
43+ fieldSize : opts . fileSizeLimit ,
44+ } ,
45+ attachFieldsToBody : true ,
46+ } ) ;
2647
2748 /**
2849 * Get all avaiable editor tools
@@ -59,7 +80,7 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = (fast
5980 * Add editor tool to the library of all tools
6081 */
6182 fastify . post < {
62- Body : EditorTool ;
83+ Body : AddEditorToolDto ;
6384 } > ( '/add-tool' , {
6485 config : {
6586 /**
@@ -70,9 +91,10 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = (fast
7091 ] ,
7192 } ,
7293 schema : {
73- body : {
74- $ref : 'EditorToolSchema' ,
75- } ,
94+ consumes : [ 'multipart/form-data' ] ,
95+ // body: {
96+ // $ref: 'AddEditorToolSchema',
97+ // },
7698 response : {
7799 '2xx' : {
78100 description : 'Editor tool fields' ,
@@ -92,7 +114,31 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = (fast
92114 const editorTool = request . body ;
93115 const userId = request . userId as number ;
94116
95- const tool = await editorToolsService . addTool ( editorTool , userId ) ;
117+ let coverKey : string | undefined = undefined ;
118+
119+ if ( editorTool . cover ) {
120+ const coverBuffer = await editorTool . cover . toBuffer ( ) ;
121+
122+ coverKey = await fileUploaderService . uploadFile ( {
123+ data : coverBuffer ,
124+ name : createFileId ( ) ,
125+ mimetype : editorTool . cover . mimetype ,
126+ } , {
127+ isEditorToolCover : true ,
128+ } , {
129+ userId,
130+ } ) ;
131+ }
132+
133+ const tool = await editorToolsService . addTool ( {
134+ title : String ( editorTool . title ?. value ) ,
135+ name : String ( editorTool . name ?. value ) ,
136+ exportName : String ( editorTool . exportName ?. value ) ,
137+ description : String ( editorTool . description ?. value ) ,
138+ source : JSON . parse ( String ( editorTool . source ?. value ) ) ,
139+ isDefault : Boolean ( editorTool . isDefault ?. value ?? false ) ,
140+ cover : coverKey ?? '' ,
141+ } , userId ) ;
96142
97143 return reply . send ( {
98144 data : tool ,
0 commit comments