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) (#259)

* fix: fixed validation of upload file response type, fixed policy of getting file(added necessary preHandlers)

* fix: removed conflict text, added doc for opt

* refactor: eslint fixes

* refactor: removed extra space from doc
  • Loading branch information
slaveeks authored Jun 29, 2024
1 parent 9615a41 commit 8fcfee0
Show file tree
Hide file tree
Showing 2 changed files with 35 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 @@ -252,6 +252,7 @@ export default class HttpApi implements Api {
fileUploaderService: domainServices.fileUploaderService,
noteService: domainServices.noteService,
fileSizeLimit: this.config.fileSizeLimit,
noteSettingsService: domainServices.noteSettingsService,
});
}

Expand Down
39 changes: 34 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 type NoteSettingsService from '@domain/service/noteSettings.js';
import useMemberRoleResolver from '../middlewares/noteSettings/useMemberRoleResolver.js';

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

/**
* Note settings service instance
*/
noteSettingsService: NoteSettingsService;

/**
* Limit for uploaded files size
*/
Expand All @@ -35,6 +43,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 +94,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 @@ -128,7 +154,6 @@ const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify,
$ref: 'UploadSchema#/properties/key',
},
},

response: {
'2xx': {
description: 'Generated buffer',
Expand All @@ -138,7 +163,11 @@ const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify,
},
},
},
preHandler: [noteResolver],
preHandler: [
noteResolver,
noteSettingsResolver,
memberRoleResolver,
],
}, async (request, reply) => {
const fileLocation: NoteAttachmentFileLocation = {
noteId: request.note!.id,
Expand Down

0 comments on commit 8fcfee0

Please sign in to comment.