Skip to content

Commit

Permalink
ignoring test output file
Browse files Browse the repository at this point in the history
  • Loading branch information
pvrpccc committed Jan 10, 2017
1 parent 7aa5ffc commit 4baf65b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
npm-debug.log
test/watermarked.pdf
26 changes: 15 additions & 11 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const express = require('express'); //Express Web Server
const busboy = require('connect-busboy'); //middleware for form/file upload
const path = require('path'); //used for file path
const fs = require('fs-extra'); //File System - for file manipulation
var uuid = require('node-uuid');
Expand All @@ -22,35 +21,33 @@ var deleteFolderRecursive = function(path) {
};

var app = express();
app.use(busboy());

app.get('/', function (req, res) {
res.send('<h1>Instructions</h1><p>In order to watermark a pdf send two PDF files to /upload, one with fieldname watermark and one with fieldname pdf-to-watermark. The watermark pdf will get stamped on each page of the pdf-to-watermark pdf and the resulting PDF streamed back.</p><pre>curl -i -F "[email protected]" -F "[email protected]" http://localhost:'+PORT+'/watermark > watermarked.pdf</pre>\n');
});

var Busboy = require('busboy');
/* ==========================================================
Create a Route (/watermark) to handle the upload
(handle POST requests to /upload)
Express v4 Route definition
============================================================ */
app.route('/watermark')
.post(function (req, res, next) {

var busboy = new Busboy({ headers: req.headers });
var fstream;
var watermark,pdftowatermark;
req.pipe(req.busboy);
var watermark,pdftowatermark='';

let tempdir = '/tmp/' + uuid.v4()
fs.mkdir(tempdir);

req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
file.on('data', function(){
// got a chunk of file
});
let filesUploaded = 0;
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {

let localpath = tempdir+"/"+fieldname+'.pdf';
fstream = fs.createWriteStream(localpath);
file.pipe(fstream);

fstream.on('close', function () {
filesUploaded++;
console.log('Uploaded File: ' + filename);

if(fieldname == "pdf-to-watermark"){
Expand All @@ -61,6 +58,12 @@ app.route('/watermark')
watermark = localpath;
}

if(filesUploaded == 2 && (watermark === undefined || pdftowatermark === undefined)){
res.status(500);
res.send("Two files uploaded but the file upload fields did not have the right names, did you name the fields watermark and pdf-to-watermark?\n");
return;
}

if(watermark && pdftowatermark){
let output_pdf = tempdir+'/output.pdf';

Expand Down Expand Up @@ -97,6 +100,7 @@ app.route('/watermark')
}
});
});
req.pipe(busboy);
});

app.listen(PORT);
Expand Down

0 comments on commit 4baf65b

Please sign in to comment.