-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(File, Route): First Upload!!! Just for testing
- Loading branch information
1 parent
fd44dfa
commit c8c4a85
Showing
8 changed files
with
1,337 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"cSpell.words": [ | ||
"accesstoken", | ||
"authtoken" | ||
"authtoken", | ||
"originalname" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { InvalidMimeTypeError } from '@/domain/storage/application/use-cases/errors/invalid-mime-type-error' | ||
import { makeUploadFileUseCase } from '@/infra/use-cases/factories/make-upload-file-use-case' | ||
import { type FastifyReply, type FastifyRequest } from 'fastify' | ||
|
||
interface MulterRequest extends FastifyRequest { | ||
file?: { | ||
buffer: Buffer | ||
originalname: string | ||
mimetype: string | ||
size: number | ||
} | ||
} | ||
|
||
export async function uploadFileController(request: MulterRequest, reply: FastifyReply) { | ||
const uploadFileUseCase = makeUploadFileUseCase() | ||
|
||
if (!request.file) { | ||
return reply.status(404).send({ | ||
message: 'File required' | ||
}) | ||
} | ||
|
||
const result = await uploadFileUseCase.exec({ | ||
body: request.file.buffer, | ||
fileName: request.file.originalname, | ||
fileType: request.file.mimetype, | ||
size: request.file.size, | ||
storedAt: new Date() | ||
}) | ||
|
||
if (result.isLeft()) { | ||
const error = result.value | ||
|
||
switch (error.constructor) { | ||
case InvalidMimeTypeError: | ||
return reply.status(415).send() | ||
default: | ||
return reply.status(500).send() | ||
} | ||
} | ||
|
||
reply.status(201).send({ | ||
file: result.value.file | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { upload } from '@/infra/app' | ||
import { type FastifyInstance } from 'fastify' | ||
import { uploadFileController } from '../controllers/upload-file' | ||
import { verifyJwt } from '../middlewares/verify-jwt' | ||
|
||
export async function fileRoutes(app: FastifyInstance) { | ||
app.post('/files', { onRequest: [verifyJwt], preHandler: [upload.single('file')] }, uploadFileController) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
type UploadParams, | ||
type Uploader | ||
} from '@/domain/storage/application/upload/uploader' | ||
|
||
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3' | ||
import { randomUUID } from 'node:crypto' | ||
import { env } from '../env' | ||
|
||
export class R2Storage implements Uploader { | ||
private readonly client: S3Client | ||
|
||
constructor() { | ||
const accountId = env.CLOUDFLARE_ACCOUNT_ID | ||
|
||
this.client = new S3Client({ | ||
endpoint: `https://${accountId}.r2.cloudflarestorage.com`, | ||
region: 'auto', | ||
credentials: { | ||
accessKeyId: env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: env.AWS_SECRET_ACCESS_KEY | ||
} | ||
}) | ||
} | ||
|
||
async upload({ | ||
fileName, | ||
fileType, | ||
body | ||
}: UploadParams): Promise<{ key: string }> { | ||
const uploadId = randomUUID() | ||
const uniqueFileName = `${uploadId}-${fileName}` | ||
|
||
await this.client.send( | ||
new PutObjectCommand({ | ||
Bucket: env.AWS_BUCKET_NAME, | ||
Key: uniqueFileName, | ||
ContentType: fileType, | ||
Body: body | ||
}) | ||
) | ||
|
||
return { | ||
key: uniqueFileName | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/infra/use-cases/factories/make-upload-file-use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { UploadFileUseCase } from '@/domain/storage/application/use-cases/upload-file' | ||
import { makePrismaFilesRepository } from '@/infra/database/prisma/repositories/factories/make-prisma-files-repository' | ||
import { R2Storage } from '@/infra/storage/r2-storage' | ||
|
||
export function makeUploadFileUseCase() { | ||
const prismaFilesRepository = makePrismaFilesRepository() | ||
const R2Uploader = new R2Storage() | ||
|
||
const uploadFileUseCase = new UploadFileUseCase( | ||
prismaFilesRepository, | ||
R2Uploader | ||
) | ||
|
||
return uploadFileUseCase | ||
} |
Oops, something went wrong.