summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/pdfrotate/editor.html17
-rw-r--r--modules/pdfrotate/index.html65
-rw-r--r--modules/pdfrotate/pdfrotate.js106
3 files changed, 188 insertions, 0 deletions
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 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>PDF rotate</title>
+<script>
+var sessionid=(function(){
+ var spl=location.href.split("/");
+ return spl[spl.length-1];
+})();
+</script>
+</head>
+<body>
+Editor...
+</body>
+<script>alert(sessionid);</script>
+</html>
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 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>PDF rotate</title>
+<script>
+window.addEventListener("load",function(){
+ var errc=location.href.split("?err=")[1];
+ if(errc){
+ if(errc=="nopdf")alert("That doesn't seem to be a PDF document.");
+ else if(errc=="rot0")alert("Haha your document was already upright. Maybe just use that instead?");
+ else if(errc=="invrot")alert("Don't bug my form, that rotation was invalid!");
+ else if(errc=="pdfjamerr")alert("An error occurred trying to convert that document. Maybe it isn't a PDF document?");
+ else alert("Unknown error occurred ('"+errc+"')");
+ }
+});
+</script>
+<style>
+.pagediagram {
+ display: inline-block;
+ border: 1px black solid;
+ width: 40px;
+ height: 56px;
+ font-family: sans-serif;
+ font-size: 40px;
+ text-align: center;
+ margin-left: 9px;
+ margin-right: 9px;
+ line-height: 62px;
+}
+</style>
+</head>
+<body>
+<h1>PDF rotate</h1>
+<p>Select a PDF file below.</p>
+<form method="POST" action="/pdfrotate/upload" enctype="multipart/form-data">
+ <input type="file" id="file" name="file" required><br>
+ <p>Click the button that looks most like the document you have. We will turn it upright.</p>
+ <table><tbody><tr>
+ <!-- CSS and pdfjam rotation are conveniently each other's inverses. -->
+ <td style="text-align: center;">
+ <label for="rot0"><div class="pagediagram" style="transform:rotate(0deg);">A</div></label><br>
+ <input type="radio" id="rot0" name="rotgroup" value="rot0" required>
+ </td>
+ <td style="text-align: center;">
+ <label for="rot90"><div class="pagediagram" style="transform:rotate(90deg);">A</div></label><br>
+ <input type="radio" id="rot90" name="rotgroup" value="rot90" required>
+ </td>
+ <td style="text-align: center;">
+ <label for="rot180"><div class="pagediagram" style="transform:rotate(180deg);">A</div></label><br>
+ <input type="radio" id="rot180" name="rotgroup" value="rot180" required>
+ </td>
+ <td style="text-align: center;">
+ <label for="rot270"><div class="pagediagram" style="transform:rotate(270deg);">A</div></label><br>
+ <input type="radio" id="rot270" name="rotgroup" value="rot270" required>
+ </td>
+ </tr></tbody></table>
+ <p>Now click the button below to finish the process!</p>
+ <input type="submit" value="Upload, turn and download result!">
+</form>
+<br>
+<br>
+<i><small>This uses <a href="https://warwick.ac.uk/fac/sci/statistics/staff/academic-research/firth/software/pdfjam/">pdfjam</a> to perform the actual hard work. Credits to me only for the slick rotated A's above.</small></i>
+</body>
+</html>
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;i<entries.length;i++){
+ fs.unlinkSync(path+"/"+entries[i]);
+ }
+}
+
+function cleanUpDir(path){
+ var nowtime=new Date().getTime();
+ var entries=fs.readdirSync(path);
+ for(var i=0;i<entries.length;i++){
+ var stat=fs.statSync(path+"/"+entries[i]);
+ if(stat.mtime-nowtime>30*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");
+ });
+};