3 var _ = require('lodash');
4 var fs = require('fs');
5 var path = require('path');
6 var uploadFile = require('upload-file');
7 var uuidLib = require('node-uuid');
8 var pdf = require('html-pdf');
9 var spindrift = require('spindrift');
10 var formidable = require('formidable');
11 var exec = require('child_process').exec;
12 var config = require('../../config/environment');
14 var FaxMessage = require('../../models').FaxMessage;
15 var FaxRoom = require('../../models').FaxRoom;
16 var FaxAccount = require('../../models').FaxAccount;
17 var Contact = require('../../models').Contact;
19 // Get list of fax_messages
20 exports.index = function(req, res) {
24 order: 'createdAt DESC',
38 .then(function(fax_messages) {
39 return res.status(200).send(fax_messages);
41 .catch(function(err) {
42 return handleError(res, err);
46 // Get a single fax_message
47 exports.show = function(req, res) {
61 .then(function(fax_message) {
63 return res.sendStatus(404);
65 return res.send(fax_message);
67 .catch(function(err) {
68 return handleError(res, err);
72 // Creates a new fax_message in the DB.
73 exports.create = function(req, res, next) {
76 // .then(function(fax_message) {
77 // return res.status(201).send(fax_message);
79 // .catch(function(err) {
80 // return handleError(res, err);
83 var form = new formidable.IncomingForm();
84 // form.encoding = 'utf-8';
85 form.uploadDir = path.join(config.root, 'server/files/fax/outbound/original');
86 form.keepExtensions = true;
87 form.multiples = false;
89 if (req.body.onlyText) {
90 //There is not file, only text
91 // console.log(req.body);
93 //There is the html message -> convert
94 var uuid = uuidLib.v4()
95 var dest = path.join(config.root, 'server/files/fax/outbound/original', uuid + '.pdf');
96 pdf.create(req.body.html, {
98 }).toFile(dest, function(err, result) {
99 console.log('Fax: HTML Converted');
100 convertPdfAndSendFax(req, res, uuid, req.body);
104 form.parse(req, function(err, fields, files) {
106 return handleError(res, err);
110 //There is HTML with PDF
111 var uuid = uuidLib.v4();
112 var dest = path.join(config.root, 'server/files/fax/outbound/original', uuid + '.pdf');
113 pdf.create(fields.html, {
115 }).toFile(dest, function(err, result) {
116 console.log('Fax: HTML Converted');
117 var pdfHTML = spindrift(result.filename);
118 var pdfUploaded = spindrift(files.file.path);
119 var uuid = uuidLib.v4();
120 spindrift.join(pdfHTML, pdfUploaded).pdfStream().pipe(fs.createWriteStream(path.join(config.root, 'server/files/fax/outbound/original', uuid + '.pdf')));
121 console.log('Fax: HTML joined with PDF uploaded');
122 convertPdfAndSendFax(req, res, uuid, fields);
126 var uuid = uuidLib.v4();
127 var dest = path.join(config.root, 'server/files/fax/outbound/original', uuid + '.pdf');
128 fs.rename(files.file.path, dest, function(err) {
131 return handleError(res, req);
134 console.log('Fax: PDF Rename');
135 convertPdfAndSendFax(req, res, uuid, fields);
143 function convertPdfAndSendFax(req, res, filenameInput, fields) {
144 var inputPath = path.join(config.root, 'server', 'files', 'fax', 'outbound', 'original', filenameInput + '.pdf');
145 var resultPath = path.join(config.root, 'server', 'files', 'fax', 'outbound', 'converted', filenameInput + '.tif');
146 var command = 'gs' + ' -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 ' + '-sPAPERSIZE=letter -sOutputFile=' + resultPath + ' ' + inputPath;
147 // console.log(inputPath, resultPath, command);
149 var uuidFax = uuidLib.v4();
151 .findById(fields.accountId)
152 .then(function(faxAccount) {
153 // console.log('FaxAccount', faxAccount);
159 from: faxAccount.phone,
160 fax_file: resultPath,
161 fax_header: faxAccount.faxheader,
162 fax_localid: faxAccount.localid,
163 maxrate: faxAccount.maxrate,
164 minrate: faxAccount.minrate,
168 id: faxAccount.TrunkId
172 fs.exists(inputPath, function(exists) {
174 // console.log('File not exist');
175 handleError(res, new Error('File not exists'));
177 exec(command, function(err) {
180 handleError(res, err);
182 console.log('Fax: PDF converted to TIFF');
183 fs.chmod(resultPath, 511, function(err) {
185 // Room already exists -> create new message and associate
193 .spread(function(faxRoom, created) {
194 // TODO Verify if we have to manage also the created
199 filenamePDF: filenameInput + '.pdf',
200 filename: filenameInput + '.tif',
201 body: JSON.stringify(faxBody),
203 FaxRoomId: faxRoom.id
205 .then(function(faxMessage) {
206 // Fax message created, now association
214 fullname: faxBody.from
217 .spread(function(contact, created) {
218 // console.log('Set relationship with from', created);
219 faxMessage.setFrom(contact);
232 .spread(function(contact, created) {
233 // console.log('Set relationship with to', created);
234 faxMessage.setTo(contact);
237 return res.status(200).send(faxMessage);
239 .catch(function(err) {
240 handleError(res, err);
241 // console.error(err);
246 // Room not exists -> create room, message and associate
251 FaxAccountId: fields.accountId,
252 UserId: fields.userId
254 .then(function(faxRoom) {
255 // Room created, now message
260 filenamePDF: filenameInput + '.pdf',
261 filename: filenameInput + '.tif',
262 body: JSON.stringify(faxBody),
264 FaxRoomId: faxRoom.id
266 .then(function(faxMessage) {
267 // Fax message created, now association
276 fullname: faxBody.from
279 .spread(function(contact, created) {
280 faxMessage.setFrom(contact);
293 .spread(function(contact, created) {
294 faxMessage.setTo(contact);
297 return res.status(200).send(
300 .catch(function(err) {
301 handleError(res, err);
304 .catch(function(err) {
305 handleError(res, err);
317 // Updates an existing fax_message in the DB.
318 exports.update = function(req, res) {
335 .then(function(fax_message) {
337 return res.sendStatus(404);
339 var updated = _.merge(fax_message, req.body);
342 return res.status(200).send(fax_message);
344 .catch(function(err) {
345 return handleError(res, err);
348 .catch(function(err) {
349 return handleError(res, err);
353 // Deletes a fax_message from the DB.
354 exports.destroy = function(req, res) {
361 .then(function(fax_message) {
363 return res.sendStatus(404);
365 fax_message.destroy()
367 return res.sendStatus(204);
369 .catch(function(err) {
370 return handleError(res, err);
373 .catch(function(err) {
374 return handleError(res, err);
378 exports.getContentFromMessage = function(req, res) {
381 .findById(req.params.id)
382 .then(function(fax_message) {
384 // console.log(fax_message);
387 return res.sendStatus(404);
390 if (fax_message.filenamePDF) {
392 if (fax_message.status === 'RECEIVED') {
393 var pdfPath = path.join(config.root, 'server', 'files', 'fax', 'inbound', fax_message.filenamePDF);
394 fs.exists(pdfPath, function(exists) {
396 return res.sendFile(pdfPath);
398 return res.sendStatus(404);
400 } else if (fax_message.status === 'NOT SENT' || fax_message.directory ===
401 'SENT' || fax_message.status === 'FAILED') {
402 var pdfPath = path.join(config.root, 'server', 'files', 'fax', 'outbound', 'original', fax_message.filenamePDF);
403 fs.exists(pdfPath, function(exists) {
405 return res.sendFile(pdfPath);
407 return res.sendStatus(404);
415 .catch(function(err) {
416 return handleError(res, err);
420 function handleError(res, err) {
421 return res.status(500).send(err);