diff --git a/lib/aws-s3.js b/lib/aws-s3.js index f150314a..aca61472 100644 --- a/lib/aws-s3.js +++ b/lib/aws-s3.js @@ -102,7 +102,7 @@ module.exports = class AWSAttachmentsService extends require("./basic") { const stored = super.put(attachments, metadata); await Promise.all([stored, multipartUpload.done()]); - if (this.kind === 's3') scanRequest(attachments, { ID: metadata.ID }) + if (this.kind === 's3') scanRequest(attachments, { ID: metadata.ID },req) } catch (err) { console.error(err); // eslint-disable-line no-console } @@ -128,21 +128,21 @@ module.exports = class AWSAttachmentsService extends require("./basic") { } } - async deleteAttachment(key) { + async deleteAttachment(key,req) { if (!key) return; - return await this.delete(key); + return await this.delete(key,req); } async deleteAttachmentsWithKeys(records, req) { if (req?.attachmentsToDelete?.length > 0) { req.attachmentsToDelete.forEach((attachment) => { - this.deleteAttachment(attachment.url); + this.deleteAttachment(attachment.url,req); }); } } async attachDeletionData(req) { - const attachments = cds.model.definitions[req?.query?.target?.name + ".attachments"]; + const attachments = cds.model.definitions[req?.target?.name + ".attachments"]; if (attachments) { const diffData = await req.diff(); let deletedAttachments = []; @@ -186,7 +186,7 @@ module.exports = class AWSAttachmentsService extends require("./basic") { await Promise.all([multipartUpload.done()]); const keys = { ID: req.data.ID } - scanRequest(req.target, keys) + scanRequest(req.target, keys,req) } } else if (req?.data?.note) { const key = { ID: req.data.ID }; @@ -209,8 +209,8 @@ module.exports = class AWSAttachmentsService extends require("./basic") { } async attachDraftDeletionData(req) { - const draftEntity = cds.model.definitions[req?.query?.target?.name]; - const name = req?.query?.target?.name; + const draftEntity = cds.model.definitions[req?.target?.name]; + const name = req?.target?.name; const activeEntity = name ? cds.model.definitions?.[name.split(".").slice(0, -1).join(".")] : undefined; if (!draftEntity || !activeEntity) return; @@ -295,8 +295,8 @@ module.exports = class AWSAttachmentsService extends require("./basic") { return response.DeleteMarker; } - async deleteInfectedAttachment(Attachments, key) { + async deleteInfectedAttachment(Attachments, key,req) { const response = await SELECT.from(Attachments, key).columns('url') - return await this.delete(response.url); + return await this.delete(response.url,req); } }; diff --git a/lib/malwareScanner.js b/lib/malwareScanner.js index 07ee5cd5..cd74858a 100644 --- a/lib/malwareScanner.js +++ b/lib/malwareScanner.js @@ -2,7 +2,7 @@ const cds = require('@sap/cds') const DEBUG = cds.debug('attachments') const { SELECT } = cds.ql; -async function scanRequest(Attachments, key) { +async function scanRequest(Attachments, key,req) { const scanEnabled = cds.env.requires?.attachments?.scan ?? true const AttachmentsSrv = await cds.connect.to("attachments") @@ -64,7 +64,7 @@ async function scanRequest(Attachments, key) { const status = responseText.malwareDetected ? "Infected" : "Clean" if (status === "Infected") { DEBUG?.("Malware detected in the file, deleting attachment content from db", key) - await AttachmentsSrv.deleteInfectedAttachment(currEntity, key) + await AttachmentsSrv.deleteInfectedAttachment(currEntity, key,req) } await updateStatus(AttachmentsSrv, key, status, currEntity, draftEntity, activeEntity) } catch (err) {