Skip to content

Commit

Permalink
refactor: rm mutation in scanAndRetrieveFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
LinHuiqing committed Sep 26, 2023
1 parent c77b2f3 commit f03825e
Showing 1 changed file with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
EncryptAttachmentResponse,
EncryptFormFieldResponse,
FormLoadedDto,
ParsedClearFormFieldResponse,
} from '../../../../types/api'
import { paymentConfig } from '../../../config/features/payment.config'
import formsgSdk from '../../../config/formsg-sdk'
Expand Down Expand Up @@ -229,35 +230,34 @@ export const scanAndRetrieveAttachments = async (

// Step 3 + 4: For each attachment, trigger lambda to scan and if it succeeds, retrieve attachment from clean bucket. Do this asynchronously.
const scanAndRetrieveFilesResult: Result<
boolean[], // true for attachment fields, false for non-attachment fields.
ParsedClearFormFieldResponse[], // true for attachment fields, false for non-attachment fields.
VirusScanFailedError | DownloadCleanFileFailedError
> = await ResultAsync.combine(
// If any scans or downloads error out, it will short circuit and return the first error.
req.body.responses.map((response) => {
if (isQuarantinedAttachmentResponse(response)) {
// Step 3: Trigger lambda to scan attachments.
return (
triggerVirusScanning(response.answer)
.map((lambdaOutput) => lambdaOutput.body)
// Step 4: Retrieve attachments from the clean bucket.
.andThen(
(cleanAttachment) =>
// Retrieve attachment from clean bucket.
downloadCleanFile(
cleanAttachment.cleanFileKey,
cleanAttachment.destinationVersionId,
).map((attachmentBuffer) => {
// Replace content with file retreived from clean bucket.
response.content = attachmentBuffer
// Replace answer with filename.
response.answer = response.filename
return true // Return true to indicate that the download was successful.
}), // If any downloads error out, it will short circuit and return the first error.
.andThen((cleanAttachment) =>
// Retrieve attachment from clean bucket.
downloadCleanFile(
cleanAttachment.cleanFileKey,
cleanAttachment.destinationVersionId,
).map((attachmentBuffer) => ({
...response,
// Replace content with attachmentBuffer and answer with filename.
content: attachmentBuffer,
answer: response.filename,
})),
)
)
}

// If response is not an attachment, return false.
return okAsync(false)
// If field is not an attachment, return original response.
return okAsync(response)
}),
)

Expand All @@ -276,6 +276,9 @@ export const scanAndRetrieveAttachments = async (
})
}

// Step 5: Replace req.body.responses with the new responses with populated attachments.
req.body.responses = scanAndRetrieveFilesResult.value

return next()
}

Expand Down

0 comments on commit f03825e

Please sign in to comment.