diff --git a/src/core/controller/storage.ts b/src/core/controller/storage.ts index b0c3395..5e5044b 100644 --- a/src/core/controller/storage.ts +++ b/src/core/controller/storage.ts @@ -14,6 +14,7 @@ interface Storage { files: File[] serverName: string path: string + fileName?: string } } @@ -33,7 +34,7 @@ class StorageController implements Controller { if (!isValidPathFolder || !isValidServerNameFolder) { return left(badRequest(new Error('Invalid folder name'))) } - const filesUploadedPaths = await uploadFiles(body.files, join('uploads', body.serverName, body.path)) + const filesUploadedPaths = await uploadFiles(body.files, join('uploads', body.serverName, body.path), body.fileName) return right(created({ files: filesUploadedPaths })) } } diff --git a/src/core/utils/upload.ts b/src/core/utils/upload.ts index 34a96b9..508172d 100644 --- a/src/core/utils/upload.ts +++ b/src/core/utils/upload.ts @@ -1,22 +1,26 @@ import { randomUUID } from 'node:crypto' import { type File } from 'core/contracts' -import { mkdir } from 'node:fs/promises' -import { existsSync, writeFileSync } from 'node:fs' +import { existsSync, writeFileSync, mkdirSync } from 'node:fs' import { join } from 'node:path' -export const uploadFiles = async (files: File[], destination: string): Promise => { - const promises = files.map(async (file) => { +export const uploadFiles = async ( + files: File[], + destination: string, + fileName?: string +): Promise => { + const paths: string[] = [] + for (const file of files) { const publicFolder = join(__dirname, '..', '..', '..', 'public') - const extension = file.mimetype.split('/')[1] - const fileName = `${randomUUID()}.${extension}` - const path = join(publicFolder, destination, fileName) - const isFolderExist = existsSync(join(publicFolder, destination)) - if (!isFolderExist) { - await mkdir(join(publicFolder, destination), { recursive: true }) + const extension = file.mimetype.split('/')?.[1] + const fileNameGeneratedOrProvided = fileName ?? `${randomUUID()}.${extension}` + const path = join(publicFolder, destination, fileNameGeneratedOrProvided) + const isFolderExists = existsSync(join(publicFolder, destination)) + if (!isFolderExists) { + mkdirSync(join(publicFolder, destination), { recursive: true }) } const buffer = file.data writeFileSync(path, buffer) - return `${destination}/${fileName}` - }) - return await Promise.all(promises) + paths.push(`${destination}/${fileNameGeneratedOrProvided}`) + } + return paths } diff --git a/src/core/wrappers/fastify-controller.ts b/src/core/wrappers/fastify-controller.ts index 41964ea..413ea09 100644 --- a/src/core/wrappers/fastify-controller.ts +++ b/src/core/wrappers/fastify-controller.ts @@ -12,12 +12,12 @@ export const controllerWrapper = (controller: Controller) => { } const httpResponse = await controller.handle(httpRequest) if (isLeft(httpResponse)) { - await reply.code(httpResponse.left.statusCode).send(httpResponse.left.body) + return await reply.code(httpResponse.left.statusCode).send(httpResponse.left.body) } if (isRight(httpResponse)) { - await reply.code(200).send(httpResponse.right.body) + return await reply.code(200).send(httpResponse.right.body) } - await reply.code(500).send({ + return await reply.code(500).send({ message: 'Internal Server Error' }) } diff --git a/src/core/wrappers/fastify-middleware.ts b/src/core/wrappers/fastify-middleware.ts index 63469d8..1729f67 100644 --- a/src/core/wrappers/fastify-middleware.ts +++ b/src/core/wrappers/fastify-middleware.ts @@ -12,13 +12,13 @@ export const middlewareWrapper = (middleware: Middleware) => { } const httpResponse = await middleware.handle(httpRequest) if (isLeft(httpResponse)) { - await reply.code(httpResponse.left.statusCode).send(httpResponse.left.body) + return await reply.code(httpResponse.left.statusCode).send(httpResponse.left.body) } if (isRight(httpResponse)) { Object.assign(request, httpResponse.right.body) next(); return } - await reply.code(500).send({ + return await reply.code(500).send({ message: 'Internal Server Error' }) } diff --git a/src/main/app.ts b/src/main/app.ts index c83c1b4..c829857 100644 --- a/src/main/app.ts +++ b/src/main/app.ts @@ -29,5 +29,5 @@ void httpInstance.register(multipart, { void httpInstance.register(middie) app(httpInstance) void httpInstance.listen({ - port: 3000 + port: 3001 })