-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathconvert.js
27 lines (26 loc) · 1004 Bytes
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*global require, module, console, __dirname */
var path = require('path'),
fs = require('fs'),
os = require('os'),
uuid = require('uuid'),
rsvgBinaryPath = '/opt/bin/rsvg-convert',
cpPromise = require('./child-process-promise'),
s3 = require('./s3-util');
module.exports = function convert(bucket, fileKey) {
'use strict';
var targetPath, sourcePath;
console.log('converting', bucket, fileKey);
return s3.download(bucket, fileKey).then(function (downloadedPath) {
sourcePath = downloadedPath;
targetPath = path.join(os.tmpdir(), uuid.v4() + '.pdf');
return cpPromise.spawn(rsvgBinaryPath, [sourcePath, '-o', targetPath, '-f', 'pdf']);
}).then(function () {
var uploadKey = fileKey.replace(/^in/, 'out').replace(/\.[A-z0-9]+$/, '.pdf');
console.log('got to upload', targetPath, sourcePath);
return s3.upload(bucket, uploadKey, targetPath);
}).then(function () {
console.log('deleting', targetPath, sourcePath);
fs.unlinkSync(targetPath);
fs.unlinkSync(sourcePath);
});
};