Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit c385d76

Browse files
committed
Stream downloads instead of saving to a buffer
1 parent c1a95e6 commit c385d76

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/services/fetchArtifactsService.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,32 @@ async function downloadArtifacts (submissionId, artifacts, savePath) {
3838
)
3939
try {
4040
// Download the artifact
41-
const req = await submissionApiClient.downloadArtifact(
41+
let req = await submissionApiClient.downloadArtifact(
4242
submissionId,
43-
artifactId
43+
artifactId,
44+
null,
45+
true
4446
)
47+
// Get the temporary path
48+
const temporaryFilePath = path.join(savePath, `artifact-${artifactId}.tcdownload`)
49+
// Save the file
50+
const fStream = fs.createWriteStream(temporaryFilePath)
51+
const writeStream = req.pipe(fStream)
52+
// Wait for write to complete
53+
await new Promise((resolve, reject) => {
54+
req.on('response', (_req) => {
55+
req = _req
56+
})
57+
writeStream.on('finish', resolve)
58+
writeStream.on('error', reject)
59+
})
4560
// Get file name from headers
4661
const disposition = _.get(req, 'headers.content-disposition')
4762
const fileName = contentDisposition.parse(disposition).parameters.filename
48-
// Get file path
49-
const filePath = path.join(savePath, `${fileName}`)
63+
// Get the final file path
64+
const filePath = path.join(savePath, fileName)
5065
// Save the file
51-
await fs.writeFile(filePath, req.body)
66+
await fs.move(temporaryFilePath, filePath)
5267
// Log the result
5368
logger.info(
5469
`[${idx + 1}/${artifacts.length}] ` +

src/services/fetchSubmissionService.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,27 @@ async function downloadSubmissions (submissions, savePath) {
4343
)
4444
try {
4545
// Download the submission
46-
const req = await submissionApiClient.downloadSubmission(submission.id)
46+
let req = await submissionApiClient.downloadSubmission(submission.id, null, true)
47+
// Get the temporary path
48+
const temporaryFilePath = path.join(savePath, `submission-${submission.id}.tcdownload`)
49+
// Save the file
50+
const fStream = fs.createWriteStream(temporaryFilePath)
51+
const writeStream = req.pipe(fStream)
52+
// Wait for write to complete
53+
await new Promise((resolve, reject) => {
54+
req.on('response', (_req) => {
55+
req = _req
56+
})
57+
writeStream.on('finish', resolve)
58+
writeStream.on('error', reject)
59+
})
4760
// Get file name from headers
4861
const disposition = _.get(req, 'headers.content-disposition')
4962
const fileName = contentDisposition.parse(disposition).parameters.filename
50-
// Get file path
51-
const filePath = path.join(savePath, `${fileName}`)
63+
// Get the final file path
64+
const filePath = path.join(savePath, fileName)
5265
// Save the file
53-
await fs.writeFile(filePath, req.body)
66+
await fs.move(temporaryFilePath, filePath)
5467
// Log the result
5568
logger.info(
5669
`[${idx + 1}/${submissions.length}] ` +

0 commit comments

Comments
 (0)