-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.js
89 lines (65 loc) · 1.88 KB
/
batch.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
var fs = require('fs')
var fsp = require('fs').promises
const util = require('util')
const stream = require('stream')
const path = require('path')
var sanitize = require('sanitize-filename')
const querystring = require('node:querystring');
const PDFSense = require("./pdfsense.js")
const pdfsense = new PDFSense()
const ROOT = 'data'
const finished = util.promisify(stream.finished);
class Batch {
constructor(config) {
this.config = config
}
async init() {
const {default: got} = await import('got')
this.got = got
}
async process(req_path, query) {
if(!query.dir) throw("You must give 'dir' query parameter.")
var files = await pdfsense.getFileList(query.dir, '', ['.pdf'])
const process_path = req_path.split('/api/batch/')[1]
var query_str = querystring.stringify(query)
for(var file of files) {
try {
const file_id = await this.copyFile(query.dir, file)
var commands = this.splitPath(process_path)
console.log(commands)
for(var command of commands) {
var url = `http://localhost:8200/api/uploads/${file_id}/${command}?${query_str}`
console.log(url)
var r = await this.got.post(url)
}
} catch (e) {
console.log(e)
throw(e)
}
}
return files
}
async copyFile(input_dir, file) {
const input_path = path.join(input_dir, file)
const filename_clean = sanitize(file)
const file_id = pdfsense.createFileID(file, true)
await fsp.mkdir(path.join(ROOT, file_id))
const target_path = path.join(ROOT, file_id, filename_clean)
await fsp.copyFile(input_path, target_path)
return file_id
}
splitPath(url) {
var tmp = []
var commands = []
var parts = url.split('/')
if(parts.length % 2 != 0) throw('invalid command path: ' + url)
for(var i=0; i<parts.length; i++) {
tmp.push(parts[i])
if(i % 2) {
commands.push(tmp.join('/'))
}
}
return commands
}
}
module.exports = Batch;