Skip to content

Commit

Permalink
rework promise logic again
Browse files Browse the repository at this point in the history
  • Loading branch information
gmrabian committed Nov 14, 2023
1 parent b5d37aa commit 999a18a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
5 changes: 4 additions & 1 deletion services/uploads/src/antivirus.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ async function downloadFileFromS3(s3ObjectKey, s3ObjectBucket) {
try {
const response = await s3.send(getObject);
const readStream = response.Body.transformToWebStream();
return await pipeline(readStream, writeStream);
await pipeline(readStream, writeStream);
utils.generateSystemMessage(
`Finished downloading new object ${s3ObjectKey}`
);
} catch (err) {
utils.generateSystemMessage(`Error downloading new object ${s3ObjectKey}`);
throw err;
Expand Down
44 changes: 23 additions & 21 deletions services/uploads/src/clamav.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,22 @@ function updateAVDefinitonsWithFreshclam() {
async function downloadAVDefinitions() {
// list all the files in that bucket
utils.generateSystemMessage("Downloading Definitions");
const allFileKeys = await listBucketFiles(constants.CLAMAV_BUCKET_NAME);

const definitionFileKeys = allFileKeys
.filter((key) => key.startsWith(constants.PATH_TO_AV_DEFINITIONS))
.map((fullPath) => path.basename(fullPath));
try {
const allFileKeys = await listBucketFiles(constants.CLAMAV_BUCKET_NAME);

// download each file in the bucket.
const downloadPromises = await definitionFileKeys.map(
(filenameToDownload) => {
return async () => {
let destinationFile = path.join("/tmp/", filenameToDownload);
const definitionFileKeys = allFileKeys
.filter((key) => key.startsWith(constants.PATH_TO_AV_DEFINITIONS))
.map((fullPath) => path.basename(fullPath));

// download each file in the bucket.
const downloadPromises = definitionFileKeys.map(
async (filenameToDownload) => {
const destinationFile = path.join("/tmp/", filenameToDownload);
utils.generateSystemMessage(
`Downloading ${filenameToDownload} from S3 to ${destinationFile}`
);

let localFileWriteStream = fs.createWriteStream(destinationFile);

let options = {
const options = {
Bucket: constants.CLAMAV_BUCKET_NAME,
Key: `${constants.PATH_TO_AV_DEFINITIONS}/${filenameToDownload}`,
};
Expand All @@ -98,19 +95,24 @@ async function downloadAVDefinitions() {
try {
const response = await S3.send(getObject);
const readStream = response.Body.transformToWebStream();
return await pipeline(readStream, localFileWriteStream);
const writeStream = fs.createWriteStream(destinationFile);
await pipeline(readStream, writeStream);
utils.generateSystemMessage(
`Finished download ${filenameToDownload}`
);
} catch (err) {
utils.generateSystemMessage(
`Error downloading definition file ${filenameToDownload}`
);
console.log(err);
throw err;
}
};
}
);

return downloadPromises;
}
);
await Promise.all(downloadPromises);
} catch (err) {
console.error(`Error in downloadAVDefinitions: ${err.message}`);
}
}

/**
Expand Down Expand Up @@ -187,7 +189,7 @@ async function uploadAVDefinitions() {
});
});

return await Promise.all(uploadPromises);
await Promise.all(uploadPromises);
}

/**
Expand All @@ -203,7 +205,7 @@ async function uploadAVDefinitions() {
*/
function scanLocalFile(pathToFile) {
try {
let avResult = child_process.spawnSync(constants.PATH_TO_CLAMAV, [
let avResult = child_process.execSync(constants.PATH_TO_CLAMAV, [
"--stdout",
"-v",
"-a",
Expand Down

0 comments on commit 999a18a

Please sign in to comment.