Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limiting file size #105

Merged
merged 12 commits into from
Dec 16, 2024
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);
muskansethi1 marked this conversation as resolved.
Show resolved Hide resolved

AttachmentsSrv.registerUpdateHandlers(srv, entity, target);

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

function computeAttachment(req) {
muskansethi1 marked this conversation as resolved.
Show resolved Hide resolved
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",
};
Loading