From dbd9786a07e9c10cfe07db58f660fa186243b9a3 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 16 Nov 2017 00:33:41 +0100 Subject: Add pdfrotate module --- modules/pdfrotate/editor.html | 17 +++++++ modules/pdfrotate/index.html | 65 +++++++++++++++++++++++++ modules/pdfrotate/pdfrotate.js | 106 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 modules/pdfrotate/editor.html create mode 100644 modules/pdfrotate/index.html create mode 100644 modules/pdfrotate/pdfrotate.js (limited to 'modules/pdfrotate') diff --git a/modules/pdfrotate/editor.html b/modules/pdfrotate/editor.html new file mode 100644 index 0000000..6be03a8 --- /dev/null +++ b/modules/pdfrotate/editor.html @@ -0,0 +1,17 @@ + + + + +PDF rotate + + + +Editor... + + + diff --git a/modules/pdfrotate/index.html b/modules/pdfrotate/index.html new file mode 100644 index 0000000..ae63865 --- /dev/null +++ b/modules/pdfrotate/index.html @@ -0,0 +1,65 @@ + + + + +PDF rotate + + + + +

PDF rotate

+

Select a PDF file below.

+
+
+

Click the button that looks most like the document you have. We will turn it upright.

+ + + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+

Now click the button below to finish the process!

+ +
+
+
+This uses pdfjam to perform the actual hard work. Credits to me only for the slick rotated A's above. + + diff --git a/modules/pdfrotate/pdfrotate.js b/modules/pdfrotate/pdfrotate.js new file mode 100644 index 0000000..a41241d --- /dev/null +++ b/modules/pdfrotate/pdfrotate.js @@ -0,0 +1,106 @@ +var cmn=require("../$common.js"), + fs=require("fs"), + child_process=require("child_process"), + multer=require("multer"), + mkdirp=require("mkdirp"); + +var moddir; + +var uniqid=(function(){ + var id=0; + return function uniqid(){return id++;}; +})(); + +function clearDirectory(path){ + var entries=fs.readdirSync(path); + for(var i=0;i30*60*1000){ + fs.unlink(path+"/"+entries[i],function(err){}); + } + } +} + +// Clear files older than 30 mins every 30 mins +setInterval(function(){ + process.nextTick(cleanUpDir(moddir+"/uploads")); + process.nextTick(cleanUpDir(moddir+"/converted")); +},30*60*1000); + +module.exports=function(app,io,_moddir){ + moddir=_moddir; + + mkdirp.sync(moddir+"/uploads"); + mkdirp.sync(moddir+"/converted"); + clearDirectory(moddir+"/uploads"); + + var multerupload=multer({ + storage: multer.diskStorage({ + destination: function(req,file,cb){ + cb(null,moddir+"/uploads/"); + }, + filename: function(req,file,cb){ + cb(null,uniqid()+".pdf"); + } + }), + limits: { + files: 1, + fileSize: 100*1024*1024 + } + }); + + app.get("/pdfrotate",function(req,res){ + res.sendFile(moddir+"/index.html"); + }); + app.post("/pdfrotate/upload",multerupload.single("file"),function(req,res){ + if(!req.file){ + res.sendFile(moddir+"/index.html"); + return; + } + var origname=req.file.originalname; + var path=req.file.path; + var rot=req.body.rotgroup+""; + if(rot=="rot0"){ + fs.unlink(path,function(err){}); + res.redirect("/pdfrotate?err=rot0"); + return; + } + + var cmd; + switch(rot){ + case "rot90": cmd="pdf90"; break; + case "rot180": cmd="pdf180"; break; + case "rot270": cmd="pdf270"; break; + default: + fs.unlink(path,function(err){}); + res.redirect("/pdfrotate?err=invrot"); + return; + } + + var outfile=moddir+"/converted/"+uniqid()+".pdf"; + + child_process.execFile(cmd,[path,"-o",outfile],function(err,stdout,stderr){ + if(err){ + console.log(err); + res.redirect("/pdfrotate?err=pdfjamerr"); + return; + } + var newname=origname.replace(/\.pdf$/,"_rotated.pdf"); + res.download(outfile,newname,function(err){ + fs.unlink(path,function(err){}); + fs.unlink(outfile,function(err){}); + }); + }); + }); + app.get("/pdfrotate/upload",function(req,res){ + res.redirect("/pdfrotate"); + }); +}; -- cgit v1.2.3-70-g09d2