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

add file handler to write route body data to a file #139

Merged
merged 4 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const iipHandler = require('./handlers/iipHandler.js');
const proxyHandler = require('./handlers/proxyHandler.js');
const permissionHandler = require('./handlers/permssionHandler.js');
const dataHandlers = require('./handlers/dataHandlers.js');
const fileHandlers = require('./handlers/fileHandlers.js');
const sanitizeBody = require('./handlers/sanitizeHandler.js');
const DataTransformationHandler = require('./handlers/dataTransformationHandler.js');

Expand Down Expand Up @@ -88,6 +89,7 @@ var HANDLERS = {
"permissionHandler": permissionHandler,
"editHandler": auth.editHandler,
"proxyHandler": proxyHandler,
"writeFile": fileHandlers.writeFile,
"iipHandler": function() {
return iipHandler;
},
Expand Down
14 changes: 14 additions & 0 deletions handlers/fileHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs').promises;
const uuid = require("uuid");

// write a file containing the request body
function writeFile(path, prefix) {
return function(req, res, next) {
let fn = prefix + "_" + uuid.v4() + ".json";
fs.writeFile(path + fn, req.body).then(()=>next()).catch((e) => next(e));
};
};

fileHandlers = {};
fileHandlers.writeFile = writeFile;
module.exports = fileHandlers;
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"jsonwebtoken": "^8.5.1",
"jwks-rsa": "^1.12.3",
"mongodb": "^3.6.6",
"throng": "^5.0.0"
"throng": "^5.0.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"chai": "^4.3.4",
Expand Down