From 4baf65b8b344a6ebf2819784ae823d6bb5e1a1a9 Mon Sep 17 00:00:00 2001 From: "Visco, Paul" Date: Tue, 10 Jan 2017 01:29:20 -0500 Subject: [PATCH] ignoring test output file --- .gitignore | 1 + server.js | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 1ca9571..4b2eb6d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ npm-debug.log +test/watermarked.pdf diff --git a/server.js b/server.js index 195afa7..aa060dd 100644 --- a/server.js +++ b/server.js @@ -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'); @@ -22,12 +21,11 @@ var deleteFolderRecursive = function(path) { }; var app = express(); -app.use(busboy()); app.get('/', function (req, res) { res.send('

Instructions

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.

curl -i -F "watermark=@watermark.pdf" -F "pdf-to-watermark=@my.pdf" http://localhost:'+PORT+'/watermark > watermarked.pdf
\n'); }); - +var Busboy = require('busboy'); /* ========================================================== Create a Route (/watermark) to handle the upload (handle POST requests to /upload) @@ -35,22 +33,21 @@ 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"){ @@ -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'; @@ -97,6 +100,7 @@ app.route('/watermark') } }); }); + req.pipe(busboy); }); app.listen(PORT);