-
Notifications
You must be signed in to change notification settings - Fork 2
feat: change addTool to multipart data #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
6e5f088
7969916
60a0c18
694c216
5cac917
93b4b62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,3 +142,5 @@ dist | |
| .pnp.* | ||
|
|
||
| *.code-workspace | ||
|
|
||
| s3 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| -- Add column "description" and "cover "to "editor-tools" if it doesn't exist | ||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS(SELECT * | ||
| FROM information_schema.columns | ||
| WHERE table_name='editor_tools' AND column_name='description') | ||
| THEN | ||
| ALTER TABLE "editor_tools" ADD COLUMN "description" VARCHAR(255); -- Adjust the data type and size as needed | ||
| END IF; | ||
| IF NOT EXISTS(SELECT * | ||
| FROM information_schema.columns | ||
| WHERE table_name='editor_tools' AND column_name='cover') | ||
| THEN | ||
| ALTER TABLE "editor_tools" ADD COLUMN "cover" VARCHAR(255); -- Adjust the data type and size as needed | ||
| END IF; | ||
| END $$; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,12 @@ export enum FileType { | |
| /** | ||
| * File is a part of note | ||
| */ | ||
| NoteAttachment = 1 | ||
| NoteAttachment = 1, | ||
|
|
||
| /** | ||
| * Tool cover | ||
| */ | ||
| EditorToolCover = 2 | ||
|
Comment on lines
+17
to
+22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets store this as string enum for better readability |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -39,17 +44,25 @@ export type NoteAttachmentFileLocation = { | |
| noteId: NoteInternalId; | ||
| }; | ||
|
|
||
| /** | ||
| * Editor tool cover location | ||
| */ | ||
| export type EditorToolCoverFileLocation = { | ||
| isEditorToolCover: boolean; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe store toolId? |
||
| }; | ||
|
|
||
| /** | ||
| * Possible file location | ||
| */ | ||
| export type FileLocation = TestFileLocation | NoteAttachmentFileLocation; | ||
| export type FileLocation = TestFileLocation | NoteAttachmentFileLocation | EditorToolCoverFileLocation; | ||
|
|
||
| /** | ||
| * File location type, wich depends on file type | ||
| */ | ||
| export interface FileLocationByType { | ||
| [FileType.Test]: TestFileLocation; | ||
| [FileType.NoteAttachment]: NoteAttachmentFileLocation; | ||
| [FileType.EditorToolCover]: EditorToolCoverFileLocation; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import EditorTool from '@domain/entities/editorTools.js'; | ||
| import type { MultipartFields, MultipartFile, MultipartValue } from '@fastify/multipart'; | ||
| import { Multipart } from '@fastify/multipart'; | ||
|
|
||
| export interface AddEditorToolDto extends MultipartFields { | ||
|
neSpecc marked this conversation as resolved.
|
||
| name: MultipartValue; | ||
| title: MultipartValue; | ||
| exportName: MultipartValue; | ||
| description: MultipartValue; | ||
| isDefault?: MultipartValue; | ||
| userId: MultipartValue; | ||
| source: MultipartValue; | ||
| cover: MultipartFile; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| import type { FastifyPluginCallback } from 'fastify'; | ||
| import type EditorToolsService from '@domain/service/editorTools.js'; | ||
| import type EditorTool from '@domain/entities/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 | ||
|
|
@@ -10,6 +14,16 @@ 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; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -18,11 +32,18 @@ interface EditorToolsRouterOptions { | |
| * @param opts - empty options | ||
| * @param done - callback | ||
| */ | ||
| const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = (fastify, opts, done) => { | ||
| const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = async (fastify, opts, done) => { | ||
| /** | ||
| * Manage editor tools data | ||
| */ | ||
| const { editorToolsService } = opts; | ||
| const { editorToolsService, fileUploaderService } = opts; | ||
|
|
||
| await fastify.register(fastifyMultipart, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the |
||
| limits: { | ||
| fieldSize: opts.fileSizeLimit, | ||
| }, | ||
| attachFieldsToBody: true, | ||
| }); | ||
|
|
||
| /** | ||
| * Get all avaiable editor tools | ||
|
|
@@ -59,7 +80,7 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = (fast | |
| * Add editor tool to the library of all tools | ||
| */ | ||
| fastify.post<{ | ||
| Body: EditorTool; | ||
| Body: AddEditorToolDto; | ||
| }>('/add-tool', { | ||
| config: { | ||
| /** | ||
|
|
@@ -70,9 +91,10 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = (fast | |
| ], | ||
| }, | ||
| schema: { | ||
| body: { | ||
| $ref: 'EditorToolSchema', | ||
| }, | ||
| consumes: ['multipart/form-data'], | ||
| // body: { | ||
| // $ref: 'AddEditorToolSchema', | ||
| // }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove it, maybe add real validation? |
||
| response: { | ||
| '2xx': { | ||
| description: 'Editor tool fields', | ||
|
|
@@ -92,7 +114,31 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = (fast | |
| const editorTool = request.body; | ||
| const userId = request.userId as number; | ||
|
|
||
| const tool = await editorToolsService.addTool(editorTool, userId); | ||
| 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, | ||
| }); | ||
| } | ||
|
Comment on lines
+118
to
+130
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets move this logic into |
||
|
|
||
| const tool = await editorToolsService.addTool({ | ||
| title: String(editorTool.title?.value), | ||
| name: String(editorTool.name?.value), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsafe call |
||
| exportName: String(editorTool.exportName?.value), | ||
| description: String(editorTool.description?.value), | ||
| source: JSON.parse(String(editorTool.source?.value)), | ||
| isDefault: Boolean(editorTool.isDefault?.value ?? false), | ||
| cover: coverKey ?? '', | ||
| }, userId); | ||
|
|
||
| return reply.send({ | ||
| data: tool, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| export const AddEditorToolSchema = { | ||
| $id: 'AddEditorToolSchema', | ||
| type: 'object', | ||
| required: [ | ||
| 'name', | ||
| 'title', | ||
| 'exportName', | ||
| 'source', | ||
| ], | ||
| properties: { | ||
| id: { | ||
| type: 'string', | ||
| readOnly: true, | ||
| description: 'Unique tool id', | ||
| }, | ||
| name: { | ||
| type: 'string', | ||
| description: 'Plugin id that editor will use, e.g. "warning", "list", "linkTool"', | ||
| }, | ||
| title: { | ||
| type: 'string', | ||
| description: 'User-friendly name that will be shown in marketplace, .e.g "Warning tool 3000"', | ||
| }, | ||
| exportName: { | ||
| type: 'string', | ||
| description: 'Name of the plugin\'s class, e.g. "LinkTool", "Checklist", "Header"', | ||
| }, | ||
| description: { | ||
| type: 'string', | ||
| description: 'Plugin description that will be shown in the marketplace', | ||
| }, | ||
| cover: { | ||
| type: 'string', | ||
| description: 'Multipart data', | ||
| }, | ||
| isDefault: { | ||
| type: 'boolean', | ||
| description: 'Is plugin included by default in the editor', | ||
| default: false, | ||
| }, | ||
| userId: { | ||
| type: ['number', 'null'], | ||
| description: 'User id that added the tool to the marketplace', | ||
| }, | ||
| source: { | ||
| type: 'object', | ||
| properties: { | ||
| cdn: { | ||
| type: 'string', | ||
| description: 'Tool URL in content delivery network', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,4 +57,13 @@ export default class EditorToolsRepository { | |
|
|
||
| return editorTools; | ||
| } | ||
|
|
||
| /** | ||
| * Update tool cover | ||
| * @param editorToolId | ||
| * @param cover | ||
| */ | ||
| public async updateToolCover(editorToolId: EditorTool['id'], cover: EditorTool['cover']): Promise<void> { | ||
|
Comment on lines
+61
to
+66
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty docs |
||
| return await this.storage.updateToolCover(editorToolId, cover); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the numbers mean? Add description please