Skip to content

Commit

Permalink
fix: fixed validation of upload file response type, fixed policy of g…
Browse files Browse the repository at this point in the history
…etting file(added necessary preHandlers)
  • Loading branch information
slaveeks committed Jun 29, 2024
1 parent 93a0530 commit 0a16346
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export default class HttpApi implements Api {
fileUploaderService: domainServices.fileUploaderService,
noteService: domainServices.noteService,
fileSizeLimit: this.config.fileSizeLimit,
noteSettingsService: domainServices.noteSettingsService
});
}

Expand Down
37 changes: 32 additions & 5 deletions src/presentation/http/router/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ 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';
import useNoteSettingsResolver from '../middlewares/noteSettings/useNoteSettingsResolver.js';
import NoteSettingsService from '@domain/service/noteSettings.js';
import useMemberRoleResolver from '../middlewares/noteSettings/useMemberRoleResolver.js';

/**
* Interface for upload router options
Expand All @@ -20,6 +23,8 @@ interface UploadRouterOptions {
*/
noteService: NoteService;

noteSettingsService: NoteSettingsService;

/**
* Limit for uploaded files size
*/
Expand All @@ -35,6 +40,18 @@ const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify,
*/
const { noteResolver } = useNoteResolver(opts.noteService);

/**
* Prepare note settings resolver middleware
* It should be used to use note settings in middlewares
*/
const { noteSettingsResolver } = useNoteSettingsResolver(opts.noteSettingsService);

/**
* Prepare user role resolver middleware
* It should be used to use user role in middlewares
*/
const { memberRoleResolver } = useMemberRoleResolver(opts.noteSettingsService);

await fastify.register(fastifyMultipart, {
limits: {
fieldSize: opts.fileSizeLimit,
Expand Down Expand Up @@ -74,14 +91,20 @@ const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify,
'2xx': {
type: 'object',
description: 'File key to get it from the API',
key: {
$ref: 'UploadSchema#/properties/key',
},
properties: {
key: {
$ref: 'UploadSchema#/properties/key',
},
}
},
},
},
attachValidation: true,
preHandler: [ noteResolver ],
preHandler:[
noteResolver,
noteSettingsResolver,
memberRoleResolver,
],
}, async (request, reply) => {
/**
* @todo solve trouble with crashing app, when validations is not passed
Expand Down Expand Up @@ -138,7 +161,11 @@ const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify,
},
},
},
preHandler: [ noteResolver ],
preHandler:[
noteResolver,
noteSettingsResolver,
memberRoleResolver,
],
}, async (request, reply) => {
const fileLocation: NoteAttachmentFileLocation = {
noteId: request.note!.id as number,
Expand Down

0 comments on commit 0a16346

Please sign in to comment.