-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
72 lines (61 loc) · 3.2 KB
/
converter.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
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
var fs = require('fs');
const express=require('express');
const mammoth = require('mammoth')
const start=require('./start')
const path = require('path')
cloudconvert = new (require('cloudconvert'))('OXBzggGenXcbU3fuZRKvxzRtLq0ZqyJkc-prJ5YPoged6TD629Fgl_tO6KwWsKJ9rTpr7CLE5lRuhzXNBlrogg');
function convert(file,new_name,format,res){
if(format=='doc'||format=='pdf'){
// create the process. see https://cloudconvert.com/apidoc#create
cloudconvert.createProcess({inputformat: format, outputformat: 'docx'}, function(err, conversionProcess) {
if(err) {
console.error('CloudConvert Process creation failed: ' + err);
} else {
// start the process. see https://cloudconvert.com/apidoc#create
conversionProcess.start({
outputformat: 'docx',
input: 'upload'
}, function (err, conversionProcess) {
if (err) {
console.error('CloudConvert Process start failed: ' + err);
} else {
// upload the input file. see https://cloudconvert.com/apidoc#upload
conversionProcess.upload(fs.createReadStream(path.join(__dirname,'./uploads/'+file)), null, function (err, conversionProcess) {
if (err) {
console.error('CloudConvert Process upload failed: ' + err);
} else {
// wait until the process is finished (or completed with an error)
conversionProcess.wait(function (err, conversionProcess) {
if (err) {
fs.unlinkSync(path.join(__dirname,'./uploads/'+file))
res.redirect('/')
console.error('CloudConvert Process failed: ' + err);
} else {
console.log('Done: ' + conversionProcess.data.message);
// download it
conversionProcess.download(fs.createWriteStream("./converted/"+new_name+".docx"), null, function (err, conversionProcess) {
if (err) {
console.error('CloudConvert Process download failed: ' + err);
} else {
console.log('Downloaded to converted');
fs.unlinkSync(path.join(__dirname,'./uploads/'+file))
start.check(new_name,res);
// fs.unlinkSync('./converted/' +new_name+'.docx')
// mammoth.convertToHtml({path: __dirname + "/converted1.docx"})
// .then(function(result){
// console.log(result.value);
// }) //end of mammoth
// .done();
}
});
}
});
}
});
}
});
}
});
}
}
module.exports={convert}