Skip to content

Commit

Permalink
Making the inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SoujitD-SAP committed Oct 8, 2024
1 parent ca23699 commit 3264397
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ cds.once("served", async function registerPluginHandlers() {
srv.before("READ", [target, target.drafts], validateAttachment);

srv.after("READ", [target, target.drafts], readAttachment);
srv.before("PUT", [target, target.drafts], computeAttachment);

AttachmentsSrv.registerUpdateHandlers(srv, entity, target);

Expand All @@ -56,8 +57,36 @@ cds.once("served", async function registerPluginHandlers() {
}
}

function computeAttachment(req) {
const contentLengthHeader = req.headers["content-length"];
let fileSizeInBytes;

if (contentLengthHeader) {
fileSizeInBytes = Number(contentLengthHeader);

if (!isNaN(fileSizeInBytes)) {
console.log(`File size: ${fileSizeInBytes} bytes`);
const MAX_FILE_SIZE = 400 * 1024 * 1024; // 400 MB in bytes


if (fileSizeInBytes > MAX_FILE_SIZE) {
console.error(
"File size exceeds 400 MB limit. File will not be stored."
);
// Reject the request or prevent file storage

return req.reject(400, "File size exceeds the 400 MB limit.");
}
} else {
console.warn("Content-Length is not a valid number.");
return req.reject(400, "Invalid Content-Length header.");
}
} else {
return req.reject(400, "Missing Content-Length header.");
}
}

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 Down Expand Up @@ -144,5 +173,5 @@ const Ext2MimeTyes = {
xml: "application/xml",
zip: "application/zip",
txt: "application/txt",
lst: "application/txt"
lst: "application/txt",
};

0 comments on commit 3264397

Please sign in to comment.