Skip to content

Commit

Permalink
Formatted plugin.js
Browse files Browse the repository at this point in the history
  • Loading branch information
SoujitD-SAP authored Dec 9, 2024
1 parent e5545dc commit 3655497
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const attachmentIDRegex = /\/\w+\(.*ID=([0-9a-fA-F-]{36})/;

cds.on("loaded", function unfoldModel(csn) {
if (!("Attachments" in csn.definitions)) return;
const csnCopy = structuredClone(csn);
const csnCopy = structuredClone(csn)
cds.linked(csnCopy).forall("Composition", (comp) => {
if (comp._target && comp._target["@_is_media_data"] && comp.parent && comp.is2many) {
const parentDefinition = comp.parent.name
Expand All @@ -31,10 +31,10 @@ cds.once("served", async function registerPluginHandlers() {
for (let srv of cds.services) {
if (srv instanceof cds.ApplicationService) {
Object.values(srv.entities).forEach((entity) => {

for (let elementName in entity.elements) {
if (elementName === "SiblingEntity") continue; // REVISIT: Why do we have this?
const element = entity.elements[elementName],
target = element._target;
const element = entity.elements[elementName], target = element._target;
if (target?.["@_is_media_data"] && target?.drafts) {
DEBUG?.("serving attachments for:", target.name);

Expand All @@ -44,15 +44,13 @@ cds.once("served", async function registerPluginHandlers() {

srv.before("PUT", target.drafts, (req) => validateAttachmentSize(req) );


AttachmentsSrv.registerUpdateHandlers(srv, entity, target);

srv.before("NEW", target.drafts, (req) => {
req.data.url = cds.utils.uuid();
req.data.ID = cds.utils.uuid();
let ext = extname(req.data.filename).toLowerCase().slice(1);
req.data.mimeType =
Ext2MimeTyes[ext] || "application/octet-stream";
req.data.mimeType = Ext2MimeTyes[ext] || "application/octet-stream";
});
}
}
Expand All @@ -61,6 +59,7 @@ cds.once("served", async function registerPluginHandlers() {
}

async function validateAttachment(req) {

/* removing case condition for mediaType annotation as in our case binary value and metadata is stored in different database */

req?.query?.SELECT?.columns?.forEach((element) => {
Expand All @@ -75,10 +74,7 @@ cds.once("served", async function registerPluginHandlers() {
const status = await AttachmentsSrv.getStatus(req.target, {ID: attachmentID,});
const scanEnabled = cds.env.requires?.attachments?.scan ?? true;
if (scanEnabled && status !== "Clean") {
req.reject(
403,
"Unable to download the attachment as scan status is not clean."
);
req.reject(403,"Unable to download the attachment as scan status is not clean.");
}
}
}
Expand Down Expand Up @@ -167,5 +163,5 @@ const Ext2MimeTyes = {
xml: "application/xml",
zip: "application/zip",
txt: "application/txt",
lst: "application/txt",
lst: "application/txt"
};

0 comments on commit 3655497

Please sign in to comment.