Skip to content

Commit

Permalink
Merge pull request #534 from IshavSohal/issue-482
Browse files Browse the repository at this point in the history
Delete a product's zip file upon cancelling load (#534)
  • Loading branch information
szczz authored Feb 17, 2025
2 parents 8e4e74d + 92768f6 commit be1b434
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,13 @@ app.route(ROUTE_PREFIX + '/retrieve/:id/:hash').get(function (req, res, next) {
const PRODUCT_PATH = `${TARGET_PATH}/${req.params.id}`;
const uploadLocation = `${UPLOAD_PATH}/${req.params.id}-outgoing.zip`;
const commitHash = req.params.hash;
let isRequestAborted = false;

req.on('error', (err) => {
if (err.message === 'aborted') {
isRequestAborted = true;
}
});
// Check if the product exists.
if (
fs.access(PRODUCT_PATH, async (error) => {
Expand Down Expand Up @@ -212,29 +218,35 @@ app.route(ROUTE_PREFIX + '/retrieve/:id/:hash').get(function (req, res, next) {
}

const output = fs.createWriteStream(uploadLocation);

// This event listener is fired when the write stream has finished. This means that the
// ZIP file should be correctly populated. Now, we can set the correct headers and send the
// ZIP file to the client.
output.on('close', () => {
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': `attachment; filename=${req.params.id}.zip`,
'Content-Length': archive.pointer()
});
// Delete the zip file if the product load was cancelled by the user
if (isRequestAborted) {
fs.rm(uploadLocation);
} else {
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': `attachment; filename=${req.params.id}.zip`,
'Content-Length': archive.pointer()
});

const result = fs.createReadStream(uploadLocation).pipe(res);
const result = fs.createReadStream(uploadLocation).pipe(res);

// When the piping is finished, delete the stream and perform any git cleanup.
result.on('finish', async () => {
fs.rm(uploadLocation);
// When the piping is finished, delete the stream and perform any git cleanup.
result.on('finish', async () => {
fs.rm(uploadLocation);

if (commitHash !== 'latest') {
// Since the user has not asked for the latest commit, we need to clean up.
// Go back to the main branch and delete the newly created branch.
await git.checkout(currBranch);
await git.deleteLocalBranch(`version-${commitHash}`);
}
});
if (commitHash !== 'latest') {
// Since the user has not asked for the latest commit, we need to clean up.
// Go back to the main branch and delete the newly created branch.
await git.checkout(currBranch);
await git.deleteLocalBranch(`version-${commitHash}`);
}
});
}
});

// Write the product data to the ZIP file.
Expand Down

0 comments on commit be1b434

Please sign in to comment.