Skip to content

Commit

Permalink
fix: mega storage fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbsx committed Mar 28, 2023
1 parent 12487ad commit e8a1c75
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/core/controller/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Storage {
files: File[]
serverName: string
path: string
fileName?: string
}
}

Expand All @@ -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 }))
}
}
Expand Down
30 changes: 17 additions & 13 deletions src/core/utils/upload.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> => {
const promises = files.map(async (file) => {
export const uploadFiles = async (
files: File[],
destination: string,
fileName?: string
): Promise<string[]> => {
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
}
6 changes: 3 additions & 3 deletions src/core/wrappers/fastify-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/wrappers/fastify-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ void httpInstance.register(multipart, {
void httpInstance.register(middie)
app(httpInstance)
void httpInstance.listen({
port: 3000
port: 3001
})

0 comments on commit e8a1c75

Please sign in to comment.