-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
149 lines (130 loc) · 3.18 KB
/
server.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Create an app
const fs = require("fs")
const server = require('diet')
const exec = require('child_process').exec
const { k2pdfoptPath } = require('./k2pdfopt.js')
const TempDir = '/tmp/rayk/'
app = server()
newFiles = []
spawnedProcesses = {}
function createTemp() {
// Creates the temp directory
exec("mkdir -p " + TempDir)
}
function deleteTemp() {
// Deletes the temp directory
exec("rm -rf " + TempDir)
}
app.post('/cancel', function($) {
data = JSON.parse($.body)
fileToDelete = data.fileToDelete
console.log(data);
// for (i = 0, p = spawnedProcesses[i]; p; i ++) {
// console.log(data)
// p.kill()
// }
i = null
for (i in newFiles) {
f = newFiles[i]
if (f.path === fileToDelete.path) {
spawnedProcesses[f.path].kill()
break
}
}
if (i) {
newFiles.splice(i, 1)
}
$.end('Success')
})
app.post('/convert', function($) {
data = JSON.parse($.body)
console.log(data)
newFiles = []
for (i in data.files) {
f = data.files[i]
console.log('Converting ' + f.path)
newPath = TempDir + f.name + '_converted.pdf'
command = k2pdfoptPath + ' "' + f.path + '" -ui- -x -a- -o "' + newPath + '"'
f.newPath = newPath
spawnedProcess = exec(command)
spawnedProcesses[f.path] = spawnedProcess
console.log(spawnedProcess.pid)
f.convertData = {
bufferedOutput: '',
bufferedError: '',
status: null
}
f.sentToKindle = false
spawnedProcess.stdout.on('data', function(tempObj) {
return function(data) {
// console.log('stdout: ' + tempObj.name + data)
tempObj.convertData.bufferedOutput += '' + data;
}
}(f))
spawnedProcess.stderr.on('data', function(tempObj) {
return function(error) {
console.log('stderr: ' + tempObj.name + JSON.stringify(data))
tempObj.convertData.bufferedError += '' + JSON.stringify(data);
}
}(f))
spawnedProcess.on('close', function(tempObj) {
return function(code) {
if (code !== 0) {
tempObj.convertData.status = 'failed'
} else {
tempObj.convertData.status = 'done'
}
}
}(f))
newFiles.push(f)
}
$.end('Success')
})
app.get('/get_status', function($) {
if (newFiles)
$.end(JSON.stringify(newFiles))
})
app.get('/send_files', function($) {
// Copies files to Kindle
if (newFiles) {
for (i in newFiles) {
f = newFiles[i];
if (f.convertData.status === "done" && !f.sentToKindle) {
console.log(fs)
fs.copyFile(f.newPath, "/Volumes/Kindle/documents/" + f.name, (err) => {
if (err) {
console.log(err);
}
else {
f.sentToKindle = true
}
});
}
}
}
$.end("Success")
})
app.get("/cleanup", function($) {
// Refreshes state for new file conversion
deleteTemp()
createTemp()
for (i in newFiles) {
proc = spawnedProcess[newFiles[i].path]
if (proc) proc.kill()
}
newFiles = []
spawnedProcess = []
$.end("Success")
})
// Set exit function
process.on('message', (msg) => {
if (msg == 'exit') {
process.exit()
}
})
process.on('createTemp', createTemp)
process.on('deleteTemp', deleteTemp)
function startServer() {
app.listen('http://localhost:8000')
}
startServer();