-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.js
367 lines (310 loc) · 11.1 KB
/
convert.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"use strict"
import * as DF from './date-format.js'
import * as TRACK from './track.js'
import * as KML from './kml.js';
import * as GPX from './gpx.js';
import * as UTILS from './utils.js';
import * as TL from './track-links.js';
// import * as WP from './waypoint.js';
window.list_all_files=list_all_files;
var g_errorCount = 0;
var g_fileCount = 0;
var g_files = []; // MyFile对象
var g_enableBeautify = false; // 是否启用美化
var g_forceConvert = false; // 是否强制转相同格式
var g_trackFileHookScript = undefined;
var g_useTrackFileHook = false;
var g_canvasWidth = TL.CanvasDefaultWidth;
var g_canvasHeight = TL.CanvasDefaultHeight;
class Converted
{
constructor(name,content,trackFile) // 转换后的数据
{
this.name=name;
this.content=content;
this.trackFile=trackFile;
}
}
class MyFile
{
constructor(name)
{
this.name = name; // 源名字
this.content = undefined; // 源数据
this.converted = []; // Converted对象
this.keepSameFormat = false; // 当目标格式与源格式相同时,保持原样,无需转换
this.trackFile = undefined;
}
parseName()
{
// 额外的处理,得出文件名、小写的扩展名
const splited = this.name.split('.');
const splitedCount = splited.length;
if(splitedCount>1){
const suffix=splited[splitedCount-1].toLowerCase();
const prefix=this.name.substring(0, this.name.length-suffix.length - 1);
return [prefix, suffix]; // prefix: 不含'.'字符
}
return undefined;
}
}
const appendFile = myFile => {
g_files.push(myFile);
setProgress(g_files.length / g_fileCount); // 始终达不到100%,最后需要压缩表示100
}
$('#convert').click(function () {
const srcFiles = $("#select-files")[0].files;
if(srcFiles.length<1)
{
appendError('请至少上传一个文件');
return;
}
g_useTrackFileHook = $('#use-trackfile-hook').prop('checked');
// Remove custom hook
if(g_trackFileHookScript){
g_trackFileHookScript.remove();
g_trackFileHookScript=undefined;
}
// Load custom hook
if(g_useTrackFileHook){
window.trackFileHook = undefined;
const waitHookToBeReady = async () => {
// https://stackoverflow.com/a/53269990/5271632
const t1 = Date.now();
while (undefined == window.trackFileHook) {
if(Date.now() - t1 > 500){
await new Promise.reject();
return;
}
await new Promise(resolve => requestAnimationFrame(resolve));
}
};
const trackFileHook=$('#trackfile-hook-func').val();
g_trackFileHookScript = $('<script>', { text: trackFileHook, type: 'module' });
g_trackFileHookScript.prop('async',true);
$('head').append(g_trackFileHookScript);
waitHookToBeReady().then(() => {
beginToExport(srcFiles);
}).catch(() => {
appendError('Load hook timeout');
});
}else
beginToExport(srcFiles);
});
const beginToExport = (srcFiles) => {
const costTimestampBegin = DF.now();
let promises = [];
const fileCount = srcFiles.length;
// reset status
g_fileCount=fileCount;
g_files=[];
setProgress(0);
clearErrors();
const exportedTrackList = $('#exportedTrackList');
exportedTrackList.empty();
const infoList=$('#infoList')
infoList.empty();
g_enableBeautify = $('#enable-export-beautify').prop('checked');
g_forceConvert = $('#force-convert-same-fmt').prop('checked');
g_canvasWidth = parseInt($('#canvas-width').val());
g_canvasHeight = parseInt($('#canvas-height').val());
const DestFileFormat = $('select#select-convert-format').find(":selected").val();
for(let i=0;i<fileCount;++i){
const f = srcFiles[i];
promises.push(promiseReadBlob(f).then(myFile => {
return promiseConvertFormat(myFile, DestFileFormat).then(myFile => {
appendFile(myFile);
})
}));
}
Promise.allSettled(promises).then(function (results) {
results.forEach(r => {
if (r.status === 'rejected') {
appendError(r.reason);
}
});
g_files = g_files.filter(myFile => { return myFile && myFile.converted.length > 0; });
g_files.sort((a, b) => a.name.localeCompare(b.name)); // 按文件名排序
let stat = {
converted: 0,
same: 0,
};
if(g_files.length > 0){
const zipHint = '转换'+DestFileFormat+'合辑_'+DF.timestampToString(costTimestampBegin / 1000, 'yyyyMMdd-hhmmss');
const zip = new JSZip();
const zipFolder = zip.folder(zipHint);
g_files.forEach(myFile => {
myFile.converted.forEach(c => { zipFolder.file(c.name, c.content); });
if (myFile.keepSameFormat)
++stat.same;
else
++stat.converted;
});
const zipBlob = zip.generate({
compression: "DEFLATE",
compressionOptions : {level:6},
type: "blob",
platform: "DOS",
mimeType: 'application/zip'
});
exportedTrackList.append(TL.newDownloadLinkDiv(zipBlob, zipHint+'.zip'));
const divPreview = $('<div>');
divPreview.append($('<button>', {
text: '预览压缩包',
onclick: 'list_all_files(this)'
}));
exportedTrackList.append(divPreview);
} else {
exportedTrackList.append($('<a>', {
text: 'Zip文件内容为空白',
href: 'javascript:void(0);'
}));
exportedTrackList.append('<br/>');
}
setProgress(1);
infoList.append('源数目: '+g_fileCount+', 已转换: '+stat.converted+ ', 无需转换: '+ stat.same + '<br/>耗时'+ UTILS.millisecondToHumanReadableString(DF.now() - costTimestampBegin));
});
}
$('#use-trackfile-hook').change(function(){
const checked = $('#use-trackfile-hook').prop('checked');
const textArea = $('#trackfile-hook-func');
if(checked)
textArea.show();
else
textArea.hide();
});
function clearErrors() {
$('#errorListHeader').css('display', 'none');
$('#errorListBody').empty();
g_errorCount = 0;
}
function appendError(s) {
if (g_errorCount <= 0) {
$('#errorListHeader').css('display', 'inline-block');
}
++g_errorCount;
$('#errorCount').html(g_errorCount);
$('#errorListBody').append(g_errorCount + ': ' + s + '<br/>');
}
// value=-1: indeterminate; value=[0,1): determinate; -2: hide div
function setProgress(value) {
let div = $('#progressBarDiv');
let pb = $('#progressBar');
//console.log('change progress=' + value);
if (value >= 0 && value <= 1) {
div.show();
pb.attr('value', value);
} else if (value == -1) {
div.show();
pb.removeAttr('value');
} else {
div.hide();
}
}
function list_all_files(btn)
{
btn.remove();
const exportedTrackList=$('#exportedTrackList');
g_files.forEach((myFile) => {
myFile.converted.forEach(c => {
const divRoot = TL.newRootDiv();
exportedTrackList.append(divRoot);
const trackFile = c.trackFile;
const paths = trackFile.lines.concat(trackFile.tracks);
const paintResult = TRACK.paint(paths, g_canvasWidth, g_canvasHeight);
divRoot.append(TL.newCanvasDiv(paintResult.points, true, g_canvasWidth, g_canvasHeight));
const divLink=TL.newDownloadLinkDiv(new Blob([c.content]), c.name);
divLink.append($('<div>', {
text: '点位' + trackFile.points.length + ',路径' + trackFile.lines.length +
',轨迹' + trackFile.tracks.length + (myFile.keepSameFormat?',未转换':'')
}));
divLink.append($('<div>', {
text: '比例尺:' + UTILS.meterToString(paintResult.horizontalDistance)+'×'+UTILS.meterToString(paintResult.verticalDistance)
}))
divRoot.append(divLink);
});
});
}
loadArchiveFormats(['zip'], function () {
let button = $('#convert');
button.html("执行转换");
button.prop('disabled', false);
});
// ---- promise 1 ----
const promiseReadBlob = blob => new Promise(function (resolve, reject) {
// API: resolve(MyFile)
const reader = new FileReader();
reader.onload = () => {
let f = new MyFile(blob.name);
f.content = reader.result;
resolve(f);
};
reader.onerror = reject;
reader.onabort = reject;
reader.readAsText(blob);
});
const promiseConvertFormat = (myFile, destFormat) => new Promise(function(resolve, reject) {
// API: resolve(MyFile)
const SrcName = myFile.name;
const SrcPrefixSuffix=myFile.parseName();
if(undefined == SrcPrefixSuffix){
reject(new Error('Invalid file extension: ' + SrcName));
return;
}
const SrcPrefix = SrcPrefixSuffix[0];
const SrcSuffix = SrcPrefixSuffix[1];
let fromFile=undefined;
switch(SrcSuffix){
case 'kml':
fromFile = c => TRACK.TrackFile.fromKMLDocument(KML.Document.fromFile(c));
break;
case 'gpx':
fromFile = c => TRACK.TrackFile.fromGPXDocument(GPX.Document.fromFile(c));
break;
default:
reject(new Error('Unsupport file extension: ' + SrcSuffix + ' of ' + SrcName));
return;
}
myFile.trackFile=fromFile(myFile.content);
if(undefined==myFile.trackFile){
reject(new Error('Failed to build TrackFile object: ' + SrcName));
return;
}
// skip if has same format
if(false == g_forceConvert && SrcSuffix == destFormat){
myFile.keepSameFormat = true;
myFile.converted=[new Converted(myFile.name, myFile.content, myFile.trackFile)];
resolve(myFile);
return; // No need for convert
}
let newTrackFiles = [myFile.trackFile];
if(g_useTrackFileHook && window.trackFileHook)
newTrackFiles = window.trackFileHook(myFile.trackFile);
if(0 == newTrackFiles.length){
reject(new Error('Removed by hook: ' + SrcName));
return;
}
let toFile = undefined;
switch(destFormat){
case 'kml':
toFile = t => t.toKMLDocument().toFile(g_enableBeautify);
break;
case 'gpx':
toFile = t => t.toGPXDocument().toFile(g_enableBeautify);
break;
default:
reject(new Error('Unsupport dest format: ' + destFormat));
return;
}
const NewContents = newTrackFiles.map(toFile);
const NewContentLength = NewContents.length;
const ZeroPadWidth = UTILS.intWidth(NewContentLength);
let newFileNameFunc = undefined;
if(1==NewContentLength)
newFileNameFunc = () => SrcPrefix+'.'+destFormat;
else
newFileNameFunc = idx => SrcPrefix + '_' + UTILS.zeroPad(idx+1, ZeroPadWidth) + '.' + destFormat;
myFile.converted=NewContents.map((c,idx) => new Converted(newFileNameFunc(idx), c, newTrackFiles[idx]));
resolve(myFile);
});
// ---- promise 2 ----