-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertor.js
More file actions
74 lines (66 loc) · 2.13 KB
/
convertor.js
File metadata and controls
74 lines (66 loc) · 2.13 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
var fs = require('fs');
const EventEmitter = require('events');
const path = require('path');
var glob = require("glob")
var sanitize = require("sanitize-filename");
var slug = require("slug");
const OfficeConvertor = require('./convertor/office');
const PdfConvertor = require('./convertor/pdf');
class Convertor extends EventEmitter
{
constructor() {
super();
this.timestamp = Date.now();
this.uploadDir = 'public/documents/' + this.timestamp;
}
process(document, format) {
var that = this;
fs.mkdirSync(this.uploadDir);
this.document = this.uploadDir + '/' + sanitize(document.name);
this.format = format;
document.mv(this.document, function(err){
if(!err){
var ext = path.extname(that.document);
var newPath = that.uploadDir + '/' + slug(document.name.replace(ext, '')) + ext;
fs.renameSync(that.document, newPath);
that.document = newPath;
that.emit('document.saved');
}
});
}
convert() {
var that = this;
var convertor = null;
switch (path.extname(this.document)) {
case '.pdf':
convertor = new PdfConvertor();
convertor.format = this.format;
convertor.on('pdf.convert.img', () => {
if(that.deletePdf===true){
fs.unlinkSync(this.document);
}
that.emit('converted.img');
});
break;
default:
convertor = new OfficeConvertor();
convertor.on('doc.convert.pdf', (document) => {
that.deletePdf = true;
that.document = document;
that.convert();
});
break;
}
convertor.document = this.document;
convertor.convert();
}
registerEvent() {
var that = this;
this.on('document.saved', () => {
that.convert();
});
return this;
}
}
module.exports = Convertor;