Skip to content

Commit

Permalink
Refactor(Video, Route): Upload video route - Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 26, 2024
1 parent 68aa73e commit 61d4d62
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
60 changes: 53 additions & 7 deletions src/infra/http/controllers/upload-file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { InvalidMimeTypeError } from '@/core/errors/errors/invalid-mime-type-error'
import { makeFileMapper } from '@/infra/database/prisma/mappers/factories/make-file-mapper'
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error'
import { ImageMapper } from '@/infra/database/prisma/mappers/image-mapper'
import { VideoMapper } from '@/infra/database/prisma/mappers/video-mapper'
import { makeGetImageDetailsUseCase } from '@/infra/use-cases/factories/make-get-image-details-use-case'
import { makeGetVideoDetailsUseCase } from '@/infra/use-cases/factories/make-get-video-details-use-case'
import { makeUploadFileUseCase } from '@/infra/use-cases/factories/make-upload-file-use-case'
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { FilePresenter } from '../presenters/file-presenter'
import { ImagePresenter } from '../presenters/image-presenter'
import { VideoPresenter } from '../presenters/video-presenter'

interface MulterRequest extends FastifyRequest {
file?: {
Expand All @@ -21,6 +26,8 @@ export async function uploadFileController(request: MulterRequest, reply: Fastif
}

const uploadFileUseCase = makeUploadFileUseCase()
const getVideoDetailsUseCase = makeGetVideoDetailsUseCase()
const getImageDetailsUseCase = makeGetImageDetailsUseCase()

const result = await uploadFileUseCase.exec({
body: request.file.buffer,
Expand All @@ -40,10 +47,49 @@ export async function uploadFileController(request: MulterRequest, reply: Fastif
}
}

const fileMapper = makeFileMapper()
const file = await fileMapper.toPrisma(result.value.file)
const { file } = result.value

return await reply.status(201).send({
file: FilePresenter.toHTTP(file)
})
if (file.fileType === 'video/mp4' || file.fileType === 'video/avi') {
const videoResult = await getVideoDetailsUseCase.exec({
fileKey: file.fileKey
})

if (videoResult.isLeft()) {
const error = videoResult.value

switch (error.constructor) {
case ResourceNotFoundError:
return await reply.status(404).send({ message: error.message })
default:
return await reply.status(500).send({ message: error.message })
}
}

const video = VideoMapper.toPrisma(videoResult.value.video)

return await reply.status(201).send({
file: VideoPresenter.toHTTP(video)
})
} else if (file.fileType === 'image/jpeg' || file.fileType === 'image/png') {
const imageResult = await getImageDetailsUseCase.exec({
fileKey: result.value.file.fileKey
})

if (imageResult.isLeft()) {
const error = imageResult.value

switch (error.constructor) {
case ResourceNotFoundError:
return await reply.status(404).send({ message: error.message })
default:
return await reply.status(500).send({ message: error.message })
}
}

const image = ImageMapper.toPrisma(imageResult.value.image)

return await reply.status(201).send({
file: ImagePresenter.toHTTP(image)
})
}
}
3 changes: 2 additions & 1 deletion src/infra/storage/r2-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export class R2Storage implements Uploader {
body
}: UploadParams): Promise<{ key: string }> {
const uploadId = randomUUID()
const uniqueFileName = `${uploadId}-${fileName}`
const uniqueFileName = `${uploadId}-${fileName}`.trim().replace(/ /g, '-').replace(/\(|\)/g, '')
.toLowerCase()

await this.client.send(
new PutObjectCommand({
Expand Down

0 comments on commit 61d4d62

Please sign in to comment.