Skip to content

fix: fix issue when download large file, topcoder-platform/community-… #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -895,6 +906,7 @@ module.exports = {
checkCreateAccess,
checkGetAccess,
checkReviewGetAccess,
createS3ReadStream,
downloadFile,
postToBusApi,
cleanseReviews,
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/SubmissionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/services/SubmissionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

/**
Expand Down