|
| 1 | +import { APIGatewayProxyHandler, APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; |
| 2 | +import { apiResponse } from '../utils/apiResponse'; |
| 3 | +import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'; |
| 4 | +import { accessTokenSchemaType, imageMimeSchema, imageUploadSchema } from '../utils/validators'; |
| 5 | +import { getKey } from '../utils/apiKey'; |
| 6 | +import { verify } from 'jsonwebtoken'; |
| 7 | +import { base64MimeType } from '../utils/getMimeType'; |
| 8 | + |
| 9 | +const client = new S3Client({ region: process.env['AWS_REGION'] as string }); |
| 10 | + |
| 11 | +export const handler: APIGatewayProxyHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => { |
| 12 | + try { |
| 13 | + const { body } = event; |
| 14 | + if (!body) { |
| 15 | + return apiResponse(400, 'Body is required'); |
| 16 | + } |
| 17 | + const values = JSON.parse(body); |
| 18 | + const data = imageUploadSchema.safeParse(values); |
| 19 | + if (!data.success) { |
| 20 | + return apiResponse(400, data.error); |
| 21 | + } |
| 22 | + const { file, accessToken, fileName, type } = data.data; |
| 23 | + |
| 24 | + if (!file) return apiResponse(400, 'File is required'); |
| 25 | + |
| 26 | + //Valid Image Type |
| 27 | + const mimeType = base64MimeType(file); |
| 28 | + const mimeData = imageMimeSchema.safeParse({ mimeType }); |
| 29 | + if (!mimeData.success) { |
| 30 | + return apiResponse(400, mimeData.error); |
| 31 | + } |
| 32 | + |
| 33 | + //Check Valid User Token |
| 34 | + const key = await getKey(); |
| 35 | + const decoded = verify(accessToken, key, { algorithms: ['RS256'] }); |
| 36 | + const { address } = decoded as accessTokenSchemaType; |
| 37 | + |
| 38 | + //Check File Size |
| 39 | + const sanitisedFile = file.replace(/^data:image\/\w+;base64,/, ''); |
| 40 | + const decodedFile = Buffer.from(sanitisedFile, 'base64'); |
| 41 | + const sizeInBytes = 4 * Math.ceil(sanitisedFile.length / 3) * 0.5624896334383812; |
| 42 | + const sizeInMb = +(sizeInBytes / (1000 * 1024)).toFixed(4); |
| 43 | + |
| 44 | + if (type === 'profile' && sizeInMb > 2) { |
| 45 | + return apiResponse(400, 'Profile image size should be less than 2MB'); |
| 46 | + } |
| 47 | + |
| 48 | + if (type === 'banner' && sizeInMb > 5) { |
| 49 | + return apiResponse(400, 'Banner image size should be less than 5MB'); |
| 50 | + } |
| 51 | + |
| 52 | + //Upload to s3 |
| 53 | + const date = new Date().toISOString(); |
| 54 | + const params = { |
| 55 | + Bucket: process.env['FILE_UPLOAD_BUCKET_NAME'], |
| 56 | + Key: `${address}-${fileName}-${date}.${mimeType?.split('/')[1]}`, |
| 57 | + Body: decodedFile, |
| 58 | + }; |
| 59 | + |
| 60 | + const command = new PutObjectCommand(params); |
| 61 | + await client.send(command); |
| 62 | + |
| 63 | + const hrefLink = `https://${command.input.Bucket}.s3.ap-south-1.amazonaws.com/${command.input.Key}`; |
| 64 | + return apiResponse(200, { |
| 65 | + hrefLink, |
| 66 | + message: 'Image Uploaded Successfully', |
| 67 | + }); |
| 68 | + } catch (err) { |
| 69 | + console.log(err); |
| 70 | + return apiResponse(401, err); |
| 71 | + } |
| 72 | +}; |
0 commit comments