-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
447 lines (393 loc) · 15.9 KB
/
index.html
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ZIP File ALTO Cleaner</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<style>
/* Previous styles remain the same */
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
.container {
border: 2px dashed #ccc;
border-radius: 8px;
padding: 20px;
text-align: center;
margin: 20px 0;
}
#dropZone {
min-height: 200px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
#dropZone.dragover {
background-color: #e9ecef;
border-color: #666;
}
#progress {
display: none;
margin: 20px 0;
}
.progress-bar {
width: 100%;
height: 20px;
background-color: #f0f0f0;
border-radius: 10px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background-color: #4CAF50;
width: 0%;
transition: width 0.3s ease;
}
button {
background-color: #2c89b4;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin: 10px;
}
#downloadButton {
background-color: #4CAF50;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.modes-container {
text-align: left;
margin: 20px 0;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
}
.mode-option {
margin: 10px 0;
}
.mode-option label {
margin-left: 8px;
cursor: pointer;
}
.error-message {
color: #ff0000;
margin: 10px 0;
display: none;
}
.mode-group {
border-left: 3px solid #4CAF50;
padding-left: 15px;
margin: 20px 0;
}
.mode-group h4 {
margin: 0 0 10px 0;
color: #333;
}
.file-stats {
margin: 10px 0;
padding: 10px;
background-color: #f5f5f5;
border-radius: 4px;
display: none;
}
.file-stats table {
width: 100%;
border-collapse: collapse;
}
.file-stats th, .file-stats td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
.compression-info {
color: #4CAF50;
font-weight: bold;
}
.size-reduction {
color: #2196F3;
}
</style>
</head>
<body>
<h1>ZIP File ALTO Cleaner</h1>
<p>Upload a ZIP file containing XML files and select processing modes to clean up.</p>
<div class="container">
<div id="dropZone">
<div>
<p>Drag and drop a ZIP file here<br>or click to select</p>
<input type="file" id="fileInput" accept=".zip" style="display: none">
</div>
</div>
</div>
<div class="modes-container">
<h3>Select Processing Modes:</h3>
<div class="mode-group">
<h4>Structure Operations:</h4>
<div class="mode-option">
<input type="checkbox" id="flattenStructure" name="processingMode" value="flattenStructure">
<label for="flattenStructure">Move all XML files to root level</label>
</div>
<div class="mode-option">
<input type="checkbox" id="compressXML" name="processingMode" value="compressXML">
<label for="compressXML">Compress XML (remove whitespace and format)</label>
</div>
</div>
<div class="mode-group">
<h4>Content Operations:</h4>
<div class="mode-option">
<input type="checkbox" id="cleanFilenames" name="processingMode" value="cleanFilenames">
<label for="cleanFilenames">Clean filenames (remove directory paths)</label>
</div>
<div class="mode-option">
<input type="checkbox" id="removeGlyphs" name="processingMode" value="removeGlyphs">
<label for="removeGlyphs">Remove <Glyph> elements and children</label>
</div>
<div class="mode-option">
<input type="checkbox" id="removeStyles" name="processingMode" value="removeStyles">
<label for="removeStyles">Remove style-related attributes</label>
</div>
<div class="mode-option">
<input type="checkbox" id="cleanWhitespace" name="processingMode" value="cleanWhitespace">
<label for="cleanWhitespace">Clean whitespace in text content</label>
</div>
</div>
<div id="modeError" class="error-message">Please select at least one processing mode.</div>
</div>
<div id="progress">
<h3>Processing Files...</h3>
<div class="progress-bar">
<div class="progress-fill"></div>
</div>
<p id="status">0%</p>
</div>
<button id="processButton" disabled>Process ZIP File</button>
<button id="downloadButton" disabled>Download Processed ZIP</button>
<div id="fileStats" class="file-stats">
<h4>Processing Statistics:</h4>
<table id="statsTable">
<thead>
<tr>
<th>File</th>
<th>Original Size</th>
<th>Processed Size</th>
<th>Reduction</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const processButton = document.getElementById('processButton');
const downloadButton = document.getElementById('downloadButton');
const progressBar = document.querySelector('.progress-fill');
const progressStatus = document.getElementById('status');
const progressDiv = document.getElementById('progress');
const modeError = document.getElementById('modeError');
let selectedFile = null;
let processedZip = null;
// Handle checkbox changes
document.querySelectorAll('input[name="processingMode"]').forEach(checkbox => {
checkbox.addEventListener('change', validateForm);
});
function validateForm() {
const atLeastOneChecked = Array.from(document.querySelectorAll('input[name="processingMode"]'))
.some(checkbox => checkbox.checked);
processButton.disabled = !selectedFile || !atLeastOneChecked;
modeError.style.display = atLeastOneChecked ? 'none' : 'block';
}
// Drag and drop handlers remain the same
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('dragover');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (file && file.type === 'application/zip') {
handleFileSelection(file);
}
});
dropZone.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
handleFileSelection(file);
}
});
function handleFileSelection(file) {
selectedFile = file;
validateForm();
downloadButton.disabled = true;
}
function formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function compressXML(xmlString) {
// Remove comments
xmlString = xmlString.replace(/<!--[\s\S]*?-->/g, '');
// Remove white space between tags while preserving space in content
xmlString = xmlString.replace(/>\s+</g, '><');
// Remove leading and trailing whitespace from lines
xmlString = xmlString.replace(/^\s+|\s+$/gm, '');
// Remove empty lines
xmlString = xmlString.replace(/(\r\n|\n|\r)/gm, '');
return xmlString;
}
function processXMLContent(content, modes) {
let processedContent = content;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(processedContent, 'text/xml');
if (modes.includes('cleanFilenames')) {
const fileNameElements = xmlDoc.getElementsByTagName('fileName');
for (const elem of fileNameElements) {
const path = elem.textContent;
elem.textContent = path.split('/').pop();
}
}
if (modes.includes('removeGlyphs')) {
const glyphs = xmlDoc.getElementsByTagName('Glyph');
while (glyphs.length > 0) {
glyphs[0].parentNode.removeChild(glyphs[0]);
}
}
if (modes.includes('removeStyles')) {
const elements = xmlDoc.getElementsByTagName('*');
const styleAttributes = ['STYLE', 'COLOR', 'FONTSIZE', 'FONTSTYLE'];
for (const elem of elements) {
styleAttributes.forEach(attr => {
elem.removeAttribute(attr);
});
}
}
if (modes.includes('cleanWhitespace')) {
const textNodes = xmlDoc.getElementsByTagName('String');
for (const node of textNodes) {
if (node.hasAttribute('CONTENT')) {
const content = node.getAttribute('CONTENT');
node.setAttribute('CONTENT', content.trim().replace(/\s+/g, ' '));
}
}
}
const serializer = new XMLSerializer();
processedContent = serializer.serializeToString(xmlDoc);
if (modes.includes('compressXML')) {
processedContent = compressXML(processedContent);
}
return processedContent;
}
function getUniqueFilename(existingFiles, originalName) {
const baseName = originalName.split('/').pop();
if (!existingFiles.has(baseName)) {
existingFiles.add(baseName);
return baseName;
}
const [name, ext] = baseName.split('.');
let counter = 1;
let newName;
do {
newName = `${name}_${counter}.${ext}`;
counter++;
} while (existingFiles.has(newName));
existingFiles.add(newName);
return newName;
}
processButton.addEventListener('click', async () => {
if (!selectedFile) return;
const selectedModes = Array.from(document.querySelectorAll('input[name="processingMode"]:checked'))
.map(checkbox => checkbox.value);
if (selectedModes.length === 0) {
modeError.style.display = 'block';
return;
}
progressDiv.style.display = 'block';
const fileStats = document.getElementById('fileStats');
fileStats.style.display = 'block';
const statsTable = document.getElementById('statsTable').getElementsByTagName('tbody')[0];
statsTable.innerHTML = '';
processButton.disabled = true;
try {
const zip = new JSZip();
const loadedZip = await zip.loadAsync(selectedFile);
const xmlFiles = Object.keys(loadedZip.files)
.filter(name => name.toLowerCase().endsWith('.xml') && !loadedZip.files[name].dir);
let processed = 0;
const existingFiles = new Set();
for (const fileName of xmlFiles) {
const content = await loadedZip.file(fileName).async('text');
const originalSize = new Blob([content]).size;
const processedContent = processXMLContent(content, selectedModes);
const processedSize = new Blob([processedContent]).size;
// Calculate reduction percentage
const reduction = ((originalSize - processedSize) / originalSize * 100).toFixed(1);
// Add statistics row
const row = statsTable.insertRow();
row.insertCell(0).textContent = fileName.split('/').pop();
row.insertCell(1).textContent = formatBytes(originalSize);
row.insertCell(2).textContent = formatBytes(processedSize);
row.insertCell(3).textContent = `${reduction}%`;
// Determine output filename
let outputName = fileName;
if (selectedModes.includes('flattenStructure')) {
outputName = getUniqueFilename(existingFiles, fileName);
}
zip.file(outputName, processedContent);
processed++;
const progress = Math.round((processed / xmlFiles.length) * 100);
progressBar.style.width = `${progress}%`;
progressStatus.textContent = `${progress}%`;
}
// Copy non-XML files if not flattening
if (!selectedModes.includes('flattenStructure')) {
for (const fileName in loadedZip.files) {
if (!fileName.toLowerCase().endsWith('.xml') && !loadedZip.files[fileName].dir) {
const content = await loadedZip.file(fileName).async('blob');
zip.file(fileName, content);
}
}
}
processedZip = await zip.generateAsync({type: 'blob'});
downloadButton.disabled = false;
progressStatus.textContent = 'Processing complete!';
} catch (error) {
console.error('Error processing ZIP:', error);
progressStatus.textContent = 'Error processing file. Please try again.';
}
});
downloadButton.addEventListener('click', () => {
if (processedZip) {
const url = URL.createObjectURL(processedZip);
const a = document.createElement('a');
a.href = url;
a.download = 'processed_' + selectedFile.name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
});
</script>
</body>
</html>