feat: add file upload

This commit is contained in:
Daniel Soares
2021-12-26 01:02:23 -03:00
parent 165c26af3f
commit db0a21e4a0
23 changed files with 407 additions and 124 deletions

View File

@@ -0,0 +1,29 @@
import { UploadClient } from '@uploadcare/upload-client'
import cuid from 'cuid'
import { env } from 'constants/env'
import { ExceptionError } from 'utils/error'
export class FilesService {
async uploadFile(buffer?: Buffer, originalname?: string) {
const client = new UploadClient({ publicKey: env.UPLOADCARE_PUBLIC_KEY })
if (!buffer || !originalname) {
throw new ExceptionError('No file provided')
}
if (!originalname.toLowerCase().match(/\.(jpg|jpeg|png)$/)) {
throw new ExceptionError('Invalid file type')
}
const response = await client.uploadFile(buffer, {
fileName: `${cuid()}-${originalname}`
})
if (!response) {
throw new ExceptionError('No file uploaded')
}
return { file: response.cdnUrl }
}
}