Skip to content

Commit

Permalink
feat: add endpoint in backend to upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
KishenKumarrrrr committed Feb 8, 2024
1 parent 1c9bcee commit fd6e0a6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
35 changes: 35 additions & 0 deletions backend/src/core/middlewares/file-attachment.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { configureEndpoint } from '@core/utils/aws-endpoint'
import { CommonAttachment } from '@email/models/common-attachment'
import { v4 as uuidv4 } from 'uuid'
import { Readable } from 'stream'
import axios from 'axios'
import { UploadService } from '@core/services'

const TOTAL_ATTACHMENT_SIZE_LIMIT = config.get(
'file.maxCumulativeAttachmentsSize'
Expand Down Expand Up @@ -156,11 +158,44 @@ async function streamCampaignEmbed(
return res
}

async function uploadFileToPresignedUrl(
req: Request,
res: Response
): Promise<Response> {
// 1. Get uploaded file from request
const uploadedFile = req.files?.file as fileUpload.UploadedFile
if (!uploadedFile) {
return res
}
// 2. Get presigned URL for file upload
const { presignedUrl, signedKey } = await UploadService.getUploadParameters(
uploadedFile.mimetype
)
try {
// 3. Upload file to presigned URL
const response = await axios.put(presignedUrl, uploadedFile, {
headers: {
'Content-Type': uploadedFile.mimetype,
},
withCredentials: false,
timeout: 30 * 1000, // 30 Seconds
})
// 4. Return the etag and transactionId to the FE
return res.json({
etag: response.headers.etag,
transactionId: signedKey,
})
} catch (err) {
return res.status(500).json({ error: err })
}
}

export const FileAttachmentMiddleware = {
checkAttachmentValidity,
getFileUploadHandler,
preprocessPotentialIncomingFile,
transformAttachmentsFieldToArray,
storeCampaignEmbed,
streamCampaignEmbed,
uploadFileToPresignedUrl,
}
8 changes: 8 additions & 0 deletions backend/src/core/routes/common-attachment.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@core/middlewares'
import { Joi, Segments, celebrate } from 'celebrate'
import { Router } from 'express'
import fileUpload from 'express-fileupload'

export const InitCommonAttachmentRoute = (
authMiddleware: AuthMiddleware
Expand Down Expand Up @@ -35,6 +36,13 @@ export const InitCommonAttachmentRoute = (
FileAttachmentMiddleware.storeCampaignEmbed
)

router.post(
'/csv-upload',
authMiddleware.getAuthMiddleware([AuthType.Cookie]),
fileUpload(),
FileAttachmentMiddleware.uploadFileToPresignedUrl
)

router.get(
'/:attachmentId/:fileName',
celebrate({
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/services/upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,17 @@ async function getMd5(blob: Blob): Promise<string> {

export async function uploadFileWithPresignedUrl(
uploadedFile: File,
presignedUrl: string
_presignedUrl: string // Making this unused because the endpoint below generates its own presignedUrl and uploads the file
): Promise<string> {
try {
const md5 = await getMd5(uploadedFile)
const response = await axios.put(presignedUrl, uploadedFile, {
const formData = new FormData()
formData.append('file', uploadedFile)
const response = await axios.post(`/attachments/csv-upload`, formData, {
headers: {
'Content-Type': uploadedFile.type,
'Content-MD5': md5,
'Content-Type': 'multipart/form-data',
},
withCredentials: false,
timeout: 0,
})
return response.headers.etag
return response.data.etag
} catch (e) {
errorHandler(
e,
Expand Down

0 comments on commit fd6e0a6

Please sign in to comment.