Skip to content
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

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

Merged
merged 5 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 35 additions & 6 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 @@ -31,10 +39,22 @@ const UploadRouter: FastifyPluginCallback<UploadRouterOptions> = async (fastify,

/**
* Prepare note id resolver middleware
* It should be used in routes that accepts note public id
* It should be used in routes that accepts note public id
*/
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
Loading