-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
109 lines (93 loc) · 3.51 KB
/
test.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
const path = require('node:path');
const fs = require('node:fs');
const getFormatId = function (ext) {
// Sheets
if (ext === 'xlsx') { return 257; }
if (ext === 'xls') { return 258; }
if (ext === 'ods') { return 259; }
if (ext === 'csv') { return 260; }
if (ext === 'pdf') { return 513; }
// Docs
if (ext === 'docx') { return 65; }
if (ext === 'doc') { return 66; }
if (ext === 'odt') { return 67; }
if (ext === 'txt') { return 69; }
if (ext === 'html') { return 70; }
// Slides
if (ext === 'pptx') { return 129; }
if (ext === 'ppt') { return 130; }
if (ext === 'odp') { return 131; }
return;
};
const getFromId = function (ext) {
var id = getFormatId(ext);
if (!id) { return ''; }
return '<m_nFormatFrom>'+id+'</m_nFormatFrom>';
};
const getToId = function (ext) {
var id = getFormatId(ext);
if (!id) { return ''; }
return '<m_nFormatTo>'+id+'</m_nFormatTo>';
};
const x2t = require('./x2t');
function copyToWasm(nodePath, wasmPath) {
const data = fs.readFileSync(nodePath);
const stream = x2t.FS.open(wasmPath, 'w');
x2t.FS.write(stream, data, 0, data.length, 0);
x2t.FS.close(stream);
}
function copyDirToWasm(nodePath, wasmPath) {
const dir = fs.readdirSync(nodePath);
for (const f of dir) {
copyToWasm(path.join(nodePath, f), path.join(wasmPath, f));
}
}
function copyFromWasm(wasmPath, nodePath) {
const data = x2t.FS.readFile(wasmPath, {encoding: 'binary'});
fs.writeFileSync(nodePath, data);
}
function convert(inputPath, outputPath) {
const inputName = path.basename(inputPath);
const outputName = path.basename(outputPath);
const inputFormat = path.extname(inputPath).substring(1);
const outputFormat = path.extname(outputPath).substring(1);
const pdfData = "";
console.log({inputPath, outputPath, inputName, outputName, inputFormat, outputFormat});
const params = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<TaskQueueDataConvert xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
+ "<m_sFontDir>/working/fonts/</m_sFontDir>"
+ "<m_sThemeDir>/working/themes</m_sThemeDir>"
+ "<m_sFileFrom>/working/" + inputName + "</m_sFileFrom>"
+ "<m_sFileTo>/working/" + outputName + "</m_sFileTo>"
+ pdfData
// + getFromId(inputFormat)
// + getToId(outputFormat)
+ "<m_bIsNoBase64>false</m_bIsNoBase64>"
+ "<m_nCsvTxtEncoding>46</m_nCsvTxtEncoding>"
+ "<m_nCsvDelimiter>4</m_nCsvDelimiter>"
// + "<m_sCsvDelimiterChar>,</m_sCsvDelimiterChar>"
+ "</TaskQueueDataConvert>";
console.log(params);
x2t.FS.writeFile('/working/params.xml', params);
copyToWasm(inputPath, '/working/' + inputName);
console.log(x2t.FS.readdir('/working/'));
const result = x2t.ccall("main1", "number", ["string"], ["/working/params.xml"]);
console.log(x2t.FS.readdir('/working/'));
console.log(result);
copyFromWasm('/working/' + outputName, outputPath);
}
x2t.onRuntimeInitialized = function() {
console.log("on init");
x2t.FS.mkdir('/working');
x2t.FS.mkdir('/working/media');
x2t.FS.mkdir('/working/fonts');
x2t.FS.mkdir('/working/themes');
copyDirToWasm('/tests/fonts', '/working/fonts');
// convert('/tests/test1.xlsx', '/results/out1.pdf');
convert('/tests/test1.xlsx', '/results/out1.bin');
// convert('/results/out1.bin', '/results/out1.xlsx');
// convert('/results/out1.bin', '/results/out1.ods');
// convert('/tests/test1.xlsx', '/results/out1.csv');
// convert('/results/out1.bin', '/results/out1.csv');
// convert('/results/out1.bin', '/results/out1.pdf');
};