diff --git a/src/common/helper.js b/src/common/helper.js index 9d1a8750..8e9fd6d7 100755 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -713,6 +713,17 @@ function * downloadFile (fileURL) { return downloadedFile.Body } +/** + * Function to create s3 readstream from given S3 URL + * @param{String} fileURL S3 URL of the file to be downloaded + * @returns {Object} ReadStream of downloaded file + */ +function createS3ReadStream (fileURL) { + const { bucket, key } = AmazonS3URI(fileURL) + logger.info(`create s3 readStream(): file is on S3 ${bucket} / ${key}`) + return s3.getObject({ Bucket: bucket, Key: key }).createReadStream() +} + /** * Wrapper function to post to bus api. Ensures that every event posted to bus api * is duplicated and posted to bus api again, but to a different "aggregate" topic @@ -895,6 +906,7 @@ module.exports = { checkCreateAccess, checkGetAccess, checkReviewGetAccess, + createS3ReadStream, downloadFile, postToBusApi, cleanseReviews, diff --git a/src/controllers/SubmissionController.js b/src/controllers/SubmissionController.js index 93e199c4..94f8250b 100755 --- a/src/controllers/SubmissionController.js +++ b/src/controllers/SubmissionController.js @@ -28,7 +28,7 @@ function * downloadSubmission (req, res) { fileName = `submission-${result.submission.id}.zip` } res.attachment(fileName) - res.send(result.file) + helper.createS3ReadStream(result.submission.url).pipe(res) } /** diff --git a/src/services/SubmissionService.js b/src/services/SubmissionService.js index 48a65631..5cc2e786 100755 --- a/src/services/SubmissionService.js +++ b/src/services/SubmissionService.js @@ -164,15 +164,14 @@ getSubmission.schema = { } /** - * Function to download submission from S3 + * Function to get submission * @param {Object} authUser Authenticated User * @param {String} submissionId ID of the Submission which need to be retrieved - * @return {Object} Submission retrieved from S3 + * @return {Object} Submission retrieved */ function * downloadSubmission (authUser, submissionId) { const record = yield getSubmission(authUser, submissionId) - const downloadedFile = yield helper.downloadFile(record.url) - return { submission: record, file: downloadedFile } + return { submission: record } } /**