vgapp 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +19 -16
- package/app/langs/en/buttons.json +17 -2
- package/app/langs/en/messages.json +36 -1
- package/app/langs/ru/buttons.json +17 -2
- package/app/langs/ru/messages.json +69 -34
- package/app/modules/module-fn.js +15 -9
- package/app/modules/vgfilepreview/index.js +3 -0
- package/app/modules/vgfilepreview/js/i18n.js +56 -0
- package/app/modules/vgfilepreview/js/renderers/image-modal.js +145 -0
- package/app/modules/vgfilepreview/js/renderers/image.js +92 -0
- package/app/modules/vgfilepreview/js/renderers/index.js +19 -0
- package/app/modules/vgfilepreview/js/renderers/office-modal.js +168 -0
- package/app/modules/vgfilepreview/js/renderers/office.js +79 -0
- package/app/modules/vgfilepreview/js/renderers/pdf-modal.js +260 -0
- package/app/modules/vgfilepreview/js/renderers/pdf.js +76 -0
- package/app/modules/vgfilepreview/js/renderers/playlist.js +71 -0
- package/app/modules/vgfilepreview/js/renderers/text-modal.js +343 -0
- package/app/modules/vgfilepreview/js/renderers/text.js +83 -0
- package/app/modules/vgfilepreview/js/renderers/video-modal.js +272 -0
- package/app/modules/vgfilepreview/js/renderers/video.js +80 -0
- package/app/modules/vgfilepreview/js/renderers/zip-modal.js +522 -0
- package/app/modules/vgfilepreview/js/renderers/zip.js +89 -0
- package/app/modules/vgfilepreview/js/vgfilepreview.js +532 -0
- package/app/modules/vgfilepreview/readme.md +68 -0
- package/app/modules/vgfilepreview/scss/_variables.scss +113 -0
- package/app/modules/vgfilepreview/scss/vgfilepreview.scss +460 -0
- package/app/modules/vgfiles/js/base.js +463 -175
- package/app/modules/vgfiles/js/droppable.js +260 -260
- package/app/modules/vgfiles/js/render.js +153 -153
- package/app/modules/vgfiles/js/vgfiles.js +41 -29
- package/app/modules/vgfiles/readme.md +116 -217
- package/app/modules/vgfiles/scss/_variables.scss +18 -10
- package/app/modules/vgfiles/scss/vgfiles.scss +153 -59
- package/app/modules/vgformsender/js/vgformsender.js +7 -2
- package/app/modules/vgmodal/js/vgmodal.js +12 -0
- package/app/modules/vgnav/js/vgnav.js +135 -135
- package/app/modules/vgnav/readme.md +67 -67
- package/app/modules/vgnestable/README.md +307 -307
- package/app/modules/vgnestable/scss/_variables.scss +60 -60
- package/app/modules/vgnestable/scss/vgnestable.scss +163 -163
- package/app/modules/vgselect/js/vgselect.js +62 -59
- package/app/modules/vgselect/scss/vgselect.scss +22 -22
- package/app/modules/vgspy/readme.md +28 -28
- package/app/utils/js/components/audio-metadata.js +240 -0
- package/app/utils/js/components/file-icon.js +109 -0
- package/app/utils/js/components/file-preview.js +304 -0
- package/app/utils/js/components/sanitize.js +150 -150
- package/build/vgapp.css +1 -1
- package/build/vgapp.css.map +1 -1
- package/index.js +1 -0
- package/index.scss +9 -6
- package/package.json +1 -1
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import VGModal from "../../../vgmodal";
|
|
2
|
+
|
|
3
|
+
class ZipModal {
|
|
4
|
+
constructor() {
|
|
5
|
+
this._modalId = 'vg-filepreview-zip-modal';
|
|
6
|
+
this._abortController = null;
|
|
7
|
+
this._labels = {};
|
|
8
|
+
this._entries = [];
|
|
9
|
+
this._arrayBuffer = null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static getInstance() {
|
|
13
|
+
if (!ZipModal._instance) {
|
|
14
|
+
ZipModal._instance = new ZipModal();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return ZipModal._instance;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
open(payload = {}) {
|
|
21
|
+
const src = String(payload.src || '').trim();
|
|
22
|
+
if (!src) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this._ensureModal();
|
|
27
|
+
if (!this._modal || !this._content) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this._labels = payload?.labels && typeof payload.labels === 'object' ? payload.labels : {};
|
|
32
|
+
const defaultTitle = String(payload.defaultTitle || '').trim();
|
|
33
|
+
const title = String(payload.title || '').trim();
|
|
34
|
+
this._title.textContent = title || defaultTitle;
|
|
35
|
+
this._content.innerHTML = `<div class="vg-filepreview-zip-modal__loading">${this._escapeHtml(this._label('loadingArchive'))}</div>`;
|
|
36
|
+
this._modal.show();
|
|
37
|
+
|
|
38
|
+
this._loadZipEntries(src);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
close() {
|
|
42
|
+
if (!this._modal) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this._modal.hide();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_ensureModal() {
|
|
50
|
+
if (this._modal && this._root) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this._initModal();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_initModal() {
|
|
58
|
+
const params = {
|
|
59
|
+
centered: true,
|
|
60
|
+
dismiss: true,
|
|
61
|
+
backdrop: true,
|
|
62
|
+
keyboard: true,
|
|
63
|
+
sizes: {
|
|
64
|
+
width: '800px',
|
|
65
|
+
},
|
|
66
|
+
animation: {
|
|
67
|
+
enable: false
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const existed = document.getElementById(this._modalId);
|
|
72
|
+
if (existed) {
|
|
73
|
+
this._root = existed;
|
|
74
|
+
this._modal = VGModal.getOrCreateInstance(existed, params);
|
|
75
|
+
this._bindElements(existed);
|
|
76
|
+
this._bindLifecycle(existed);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this._modal = VGModal.build(this._modalId, params, (modalInstance) => {
|
|
81
|
+
const element = modalInstance._element;
|
|
82
|
+
this._root = element;
|
|
83
|
+
element.classList.add('vg-filepreview-zip-modal');
|
|
84
|
+
|
|
85
|
+
const body = element.querySelector('.vg-modal-body');
|
|
86
|
+
const content = element.querySelector('.vg-modal-content');
|
|
87
|
+
if (!body || !content) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
body.classList.add('vg-filepreview-image-modal__body');
|
|
92
|
+
body.innerHTML = '';
|
|
93
|
+
|
|
94
|
+
let header = element.querySelector('.vg-modal-header');
|
|
95
|
+
if (!header) {
|
|
96
|
+
header = document.createElement('div');
|
|
97
|
+
header.className = 'vg-modal-header';
|
|
98
|
+
content.prepend(header);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this._title = document.createElement('div');
|
|
102
|
+
this._title.className = 'vg-modal-title';
|
|
103
|
+
this._title.textContent = '';
|
|
104
|
+
|
|
105
|
+
this._content = document.createElement('div');
|
|
106
|
+
this._content.className = 'vg-filepreview-zip-modal__content';
|
|
107
|
+
|
|
108
|
+
header.appendChild(this._title);
|
|
109
|
+
body.appendChild(this._content);
|
|
110
|
+
|
|
111
|
+
this._bindLifecycle(element);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_bindElements(root) {
|
|
116
|
+
this._title = root.querySelector('.vg-filepreview-zip-modal__title');
|
|
117
|
+
this._content = root.querySelector('.vg-filepreview-zip-modal__content');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
_bindLifecycle(root) {
|
|
121
|
+
if (!root || root.hasAttribute('data-vg-filepreview-zip-lifecycle-bind')) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
root.setAttribute('data-vg-filepreview-zip-lifecycle-bind', 'true');
|
|
126
|
+
root.addEventListener('vg.modal.hidden', () => {
|
|
127
|
+
this._destroyModal();
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
_loadZipEntries(src) {
|
|
132
|
+
if (this._abortController) {
|
|
133
|
+
this._abortController.abort();
|
|
134
|
+
}
|
|
135
|
+
this._abortController = new AbortController();
|
|
136
|
+
|
|
137
|
+
const cached = ZipModal._cache.get(src);
|
|
138
|
+
if (cached?.entries && cached?.arrayBuffer) {
|
|
139
|
+
this._entries = cached.entries;
|
|
140
|
+
this._arrayBuffer = cached.arrayBuffer;
|
|
141
|
+
this._renderEntries(this._entries);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
fetch(src, {
|
|
146
|
+
method: 'GET',
|
|
147
|
+
signal: this._abortController.signal
|
|
148
|
+
})
|
|
149
|
+
.then((response) => {
|
|
150
|
+
if (!response.ok) {
|
|
151
|
+
throw new Error(`HTTP ${response.status}`);
|
|
152
|
+
}
|
|
153
|
+
return response.arrayBuffer();
|
|
154
|
+
})
|
|
155
|
+
.then((buffer) => {
|
|
156
|
+
const entries = this._parseZipEntries(buffer);
|
|
157
|
+
this._entries = entries;
|
|
158
|
+
this._arrayBuffer = buffer;
|
|
159
|
+
ZipModal._cache.set(src, {
|
|
160
|
+
entries,
|
|
161
|
+
arrayBuffer: buffer
|
|
162
|
+
});
|
|
163
|
+
this._renderEntries(entries);
|
|
164
|
+
})
|
|
165
|
+
.catch((error) => {
|
|
166
|
+
if (error?.name === 'AbortError') {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (this._content) {
|
|
171
|
+
const errorText = this._resolveErrorText(error);
|
|
172
|
+
this._content.innerHTML = `<div class="vg-filepreview-zip-modal__error">${this._escapeHtml(this._label('cannotOpenArchive'))}: ${this._escapeHtml(errorText)}</div>`;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
_destroyModal() {
|
|
178
|
+
if (this._abortController) {
|
|
179
|
+
this._abortController.abort();
|
|
180
|
+
this._abortController = null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (this._modal && typeof this._modal.dispose === 'function') {
|
|
184
|
+
this._modal.dispose();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (this._root && this._root.parentNode) {
|
|
188
|
+
this._root.parentNode.removeChild(this._root);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
this._root = null;
|
|
192
|
+
this._modal = null;
|
|
193
|
+
this._title = null;
|
|
194
|
+
this._content = null;
|
|
195
|
+
this._labels = null;
|
|
196
|
+
this._entries = [];
|
|
197
|
+
this._arrayBuffer = null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
_parseZipEntries(arrayBuffer) {
|
|
201
|
+
const view = new DataView(arrayBuffer);
|
|
202
|
+
const length = view.byteLength;
|
|
203
|
+
|
|
204
|
+
if (length < 22) {
|
|
205
|
+
throw new Error('invalid_zip');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const eocdOffset = this._findEndOfCentralDirectory(view);
|
|
209
|
+
if (eocdOffset < 0) {
|
|
210
|
+
throw new Error('central_directory_not_found');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const totalEntries = view.getUint16(eocdOffset + 10, true);
|
|
214
|
+
const centralDirOffset = view.getUint32(eocdOffset + 16, true);
|
|
215
|
+
|
|
216
|
+
const decoder = new TextDecoder('utf-8', { fatal: false });
|
|
217
|
+
const entries = [];
|
|
218
|
+
let ptr = centralDirOffset;
|
|
219
|
+
|
|
220
|
+
for (let i = 0; i < totalEntries; i += 1) {
|
|
221
|
+
if (ptr + 46 > length) {
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const signature = view.getUint32(ptr, true);
|
|
226
|
+
if (signature !== 0x02014b50) {
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const compressedSize = view.getUint32(ptr + 20, true);
|
|
231
|
+
const uncompressedSize = view.getUint32(ptr + 24, true);
|
|
232
|
+
const compressionMethod = view.getUint16(ptr + 10, true);
|
|
233
|
+
const fileNameLen = view.getUint16(ptr + 28, true);
|
|
234
|
+
const extraLen = view.getUint16(ptr + 30, true);
|
|
235
|
+
const commentLen = view.getUint16(ptr + 32, true);
|
|
236
|
+
const localHeaderOffset = view.getUint32(ptr + 42, true);
|
|
237
|
+
|
|
238
|
+
const nameStart = ptr + 46;
|
|
239
|
+
const nameEnd = nameStart + fileNameLen;
|
|
240
|
+
if (nameEnd > length) {
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const fileNameBytes = new Uint8Array(arrayBuffer, nameStart, fileNameLen);
|
|
245
|
+
const fileName = decoder.decode(fileNameBytes);
|
|
246
|
+
|
|
247
|
+
entries.push({
|
|
248
|
+
name: fileName,
|
|
249
|
+
compressedSize,
|
|
250
|
+
uncompressedSize,
|
|
251
|
+
compressionMethod,
|
|
252
|
+
localHeaderOffset,
|
|
253
|
+
isDirectory: fileName.endsWith('/')
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
ptr = nameEnd + extraLen + commentLen;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return entries;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
_findEndOfCentralDirectory(view) {
|
|
263
|
+
const minOffset = Math.max(0, view.byteLength - 65557);
|
|
264
|
+
for (let i = view.byteLength - 22; i >= minOffset; i -= 1) {
|
|
265
|
+
if (view.getUint32(i, true) === 0x06054b50) {
|
|
266
|
+
return i;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return -1;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
_renderEntries(entries) {
|
|
273
|
+
if (!this._content) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (!entries.length) {
|
|
278
|
+
this._content.innerHTML = `<div class="vg-filepreview-zip-modal__empty">${this._escapeHtml(this._label('archiveEmptyUnsupported'))}</div>`;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const rows = entries.map((entry, index) => {
|
|
283
|
+
const type = entry.isDirectory
|
|
284
|
+
? this._label('typeDir')
|
|
285
|
+
: this._label('typeFile');
|
|
286
|
+
const compressed = entry.isDirectory ? '-' : this._formatSize(entry.compressedSize);
|
|
287
|
+
const original = entry.isDirectory ? '-' : this._formatSize(entry.uncompressedSize);
|
|
288
|
+
const previewAction = (!entry.isDirectory && this._canPreviewEntry(entry))
|
|
289
|
+
? `<button type="button" class="vg-filepreview-zip-modal__entry-btn" data-entry-index="${index}">${this._escapeHtml(this._label('previewEntry'))}</button>`
|
|
290
|
+
: '-';
|
|
291
|
+
|
|
292
|
+
return `
|
|
293
|
+
<tr>
|
|
294
|
+
<td>${index + 1}</td>
|
|
295
|
+
<td class="name">${this._escapeHtml(entry.name)}</td>
|
|
296
|
+
<td>${type}</td>
|
|
297
|
+
<td>${compressed}</td>
|
|
298
|
+
<td>${original}</td>
|
|
299
|
+
<td>${previewAction}</td>
|
|
300
|
+
</tr>`;
|
|
301
|
+
}).join('');
|
|
302
|
+
|
|
303
|
+
this._content.innerHTML = `
|
|
304
|
+
<table class="vg-filepreview-zip-modal__table">
|
|
305
|
+
<thead>
|
|
306
|
+
<tr>
|
|
307
|
+
<th>#</th>
|
|
308
|
+
<th>${this._escapeHtml(this._label('tableName'))}</th>
|
|
309
|
+
<th>${this._escapeHtml(this._label('tableType'))}</th>
|
|
310
|
+
<th>${this._escapeHtml(this._label('tablePacked'))}</th>
|
|
311
|
+
<th>${this._escapeHtml(this._label('tableSize'))}</th>
|
|
312
|
+
<th>${this._escapeHtml(this._label('tablePreview'))}</th>
|
|
313
|
+
</tr>
|
|
314
|
+
</thead>
|
|
315
|
+
<tbody>
|
|
316
|
+
${rows}
|
|
317
|
+
</tbody>
|
|
318
|
+
</table>`;
|
|
319
|
+
this._bindEntriesPreview();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
_label(key, fallback = '') {
|
|
323
|
+
const value = this._labels?.[key];
|
|
324
|
+
return typeof value === 'string' && value.trim() ? value : fallback;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
_resolveErrorText(error) {
|
|
328
|
+
const message = String(error?.message || '').trim();
|
|
329
|
+
if (!message) {
|
|
330
|
+
return this._label('unknownError');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (message === 'invalid_zip') {
|
|
334
|
+
return this._label('invalidZip');
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (message === 'central_directory_not_found') {
|
|
338
|
+
return this._label('centralDirectoryNotFound');
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return message;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
_formatSize(bytes) {
|
|
345
|
+
if (!Number.isFinite(bytes) || bytes < 0) {
|
|
346
|
+
return '-';
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const units = ['B', 'KB', 'MB', 'GB'];
|
|
350
|
+
let value = bytes;
|
|
351
|
+
let unitIndex = 0;
|
|
352
|
+
while (value >= 1024 && unitIndex < units.length - 1) {
|
|
353
|
+
value /= 1024;
|
|
354
|
+
unitIndex += 1;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const digits = value >= 100 || unitIndex === 0 ? 0 : 2;
|
|
358
|
+
return `${value.toFixed(digits)} ${units[unitIndex]}`;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
_escapeHtml(value) {
|
|
362
|
+
return String(value)
|
|
363
|
+
.replace(/&/g, '&')
|
|
364
|
+
.replace(/</g, '<')
|
|
365
|
+
.replace(/>/g, '>')
|
|
366
|
+
.replace(/"/g, '"')
|
|
367
|
+
.replace(/'/g, ''');
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
_bindEntriesPreview() {
|
|
371
|
+
if (!this._content || this._content.hasAttribute('data-vg-filepreview-zip-preview-bind')) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
this._content.setAttribute('data-vg-filepreview-zip-preview-bind', 'true');
|
|
376
|
+
this._content.addEventListener('click', (event) => {
|
|
377
|
+
const button = event.target.closest('[data-entry-index]');
|
|
378
|
+
if (!button) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
event.preventDefault();
|
|
383
|
+
const index = Number(button.getAttribute('data-entry-index'));
|
|
384
|
+
if (!Number.isFinite(index) || index < 0 || index >= this._entries.length) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
this._previewEntry(index);
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
_canPreviewEntry(entry) {
|
|
392
|
+
if (!entry || entry.isDirectory || !entry.name) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const ext = this._extractExtension(entry.name);
|
|
397
|
+
const allowed = new Set([
|
|
398
|
+
'.txt', '.md', '.json', '.xml', '.yml', '.yaml', '.csv', '.log',
|
|
399
|
+
'.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.bmp'
|
|
400
|
+
]);
|
|
401
|
+
return allowed.has(ext);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async _previewEntry(index) {
|
|
405
|
+
if (!this._content) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const entry = this._entries[index];
|
|
410
|
+
if (!entry || !this._canPreviewEntry(entry)) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
let panel = this._content.querySelector('.vg-filepreview-zip-modal__entry-preview');
|
|
415
|
+
if (!panel) {
|
|
416
|
+
panel = document.createElement('div');
|
|
417
|
+
panel.className = 'vg-filepreview-zip-modal__entry-preview';
|
|
418
|
+
this._content.appendChild(panel);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
panel.innerHTML = `<div class="vg-filepreview-zip-modal__entry-preview-loading">${this._escapeHtml(this._label('loadingEntry'))}</div>`;
|
|
422
|
+
|
|
423
|
+
try {
|
|
424
|
+
const uint8 = await this._extractEntryBytes(entry);
|
|
425
|
+
const ext = this._extractExtension(entry.name);
|
|
426
|
+
|
|
427
|
+
if (this._isImageExtension(ext)) {
|
|
428
|
+
const blob = new Blob([uint8.buffer], { type: this._mimeByExtension(ext) });
|
|
429
|
+
const url = URL.createObjectURL(blob);
|
|
430
|
+
panel.innerHTML = `
|
|
431
|
+
<div class="vg-filepreview-zip-modal__entry-preview-title">${this._escapeHtml(entry.name)}</div>
|
|
432
|
+
<img class="vg-filepreview-zip-modal__entry-preview-image" src="${url}" alt="${this._escapeHtml(entry.name)}" />`;
|
|
433
|
+
setTimeout(() => URL.revokeObjectURL(url), 2000);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const decoder = new TextDecoder('utf-8', { fatal: false });
|
|
438
|
+
const text = decoder.decode(uint8);
|
|
439
|
+
const safeText = this._escapeHtml(text || this._label('emptyFileInArchive'));
|
|
440
|
+
panel.innerHTML = `
|
|
441
|
+
<div class="vg-filepreview-zip-modal__entry-preview-title">${this._escapeHtml(entry.name)}</div>
|
|
442
|
+
<pre class="vg-filepreview-zip-modal__entry-preview-text">${safeText}</pre>`;
|
|
443
|
+
} catch (error) {
|
|
444
|
+
panel.innerHTML = `<div class="vg-filepreview-zip-modal__entry-preview-error">${this._escapeHtml(this._label('cannotPreviewEntry'))}: ${this._escapeHtml(error?.message || this._label('unknownError'))}</div>`;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
async _extractEntryBytes(entry) {
|
|
449
|
+
if (!this._arrayBuffer) {
|
|
450
|
+
throw new Error(this._label('archiveBufferMissing'));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const view = new DataView(this._arrayBuffer);
|
|
454
|
+
const offset = Number(entry.localHeaderOffset || 0);
|
|
455
|
+
if (offset + 30 > view.byteLength) {
|
|
456
|
+
throw new Error(this._label('entryCorrupted'));
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const signature = view.getUint32(offset, true);
|
|
460
|
+
if (signature !== 0x04034b50) {
|
|
461
|
+
throw new Error(this._label('entryCorrupted'));
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const nameLen = view.getUint16(offset + 26, true);
|
|
465
|
+
const extraLen = view.getUint16(offset + 28, true);
|
|
466
|
+
const dataStart = offset + 30 + nameLen + extraLen;
|
|
467
|
+
const dataEnd = dataStart + Number(entry.compressedSize || 0);
|
|
468
|
+
if (dataEnd > view.byteLength) {
|
|
469
|
+
throw new Error(this._label('entryCorrupted'));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const compressed = new Uint8Array(this._arrayBuffer.slice(dataStart, dataEnd));
|
|
473
|
+
const method = Number(entry.compressionMethod || 0);
|
|
474
|
+
|
|
475
|
+
if (method === 0) {
|
|
476
|
+
return compressed;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (method === 8) {
|
|
480
|
+
if (typeof DecompressionStream === 'undefined') {
|
|
481
|
+
throw new Error(this._label('deflateNotSupported'));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const stream = new Blob([compressed]).stream().pipeThrough(new DecompressionStream('deflate-raw'));
|
|
485
|
+
const decompressed = await new Response(stream).arrayBuffer();
|
|
486
|
+
return new Uint8Array(decompressed);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
throw new Error(this._label('compressionNotSupported'));
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
_extractExtension(name) {
|
|
493
|
+
const value = String(name || '').toLowerCase();
|
|
494
|
+
const clean = value.split('/').pop() || '';
|
|
495
|
+
if (!clean.includes('.')) {
|
|
496
|
+
return '';
|
|
497
|
+
}
|
|
498
|
+
return `.${clean.split('.').pop() || ''}`;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
_isImageExtension(ext) {
|
|
502
|
+
return new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.bmp']).has(ext);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
_mimeByExtension(ext) {
|
|
506
|
+
const map = {
|
|
507
|
+
'.png': 'image/png',
|
|
508
|
+
'.jpg': 'image/jpeg',
|
|
509
|
+
'.jpeg': 'image/jpeg',
|
|
510
|
+
'.gif': 'image/gif',
|
|
511
|
+
'.webp': 'image/webp',
|
|
512
|
+
'.svg': 'image/svg+xml',
|
|
513
|
+
'.bmp': 'image/bmp'
|
|
514
|
+
};
|
|
515
|
+
return map[ext] || 'application/octet-stream';
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
ZipModal._cache = new Map();
|
|
521
|
+
|
|
522
|
+
export default ZipModal;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import ZipModal from "./zip-modal";
|
|
2
|
+
|
|
3
|
+
const ZIP_EXTENSIONS = new Set([
|
|
4
|
+
'.zip'
|
|
5
|
+
]);
|
|
6
|
+
|
|
7
|
+
class ZipFilePreviewRenderer {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.name = 'zip';
|
|
10
|
+
this._modal = ZipModal.getInstance();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
canRender(context = {}) {
|
|
14
|
+
const ext = String(context?.fileMeta?.ext || '').toLowerCase();
|
|
15
|
+
return ZIP_EXTENSIONS.has(ext);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
render(context = {}) {
|
|
19
|
+
const container = context?.previewContainer;
|
|
20
|
+
const nameOnly = Boolean(context?.ui?.nameOnly);
|
|
21
|
+
const i18n = context?.i18n;
|
|
22
|
+
|
|
23
|
+
const src = context?.fileUrl?.href || context?.filePath || '';
|
|
24
|
+
if (!src) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const openArchive = (event) => {
|
|
29
|
+
if (event) {
|
|
30
|
+
event.preventDefault();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this._modal.open({
|
|
34
|
+
src,
|
|
35
|
+
title: context?.fileMeta?.name || i18n?.message('archive_title') || '',
|
|
36
|
+
defaultTitle: i18n?.message('archive_title') || '',
|
|
37
|
+
labels: {
|
|
38
|
+
loadingArchive: i18n?.message('loading_archive') || '',
|
|
39
|
+
cannotOpenArchive: i18n?.message('cannot_open_archive') || '',
|
|
40
|
+
unknownError: i18n?.message('unknown_error') || '',
|
|
41
|
+
invalidZip: i18n?.message('invalid_zip') || '',
|
|
42
|
+
centralDirectoryNotFound: i18n?.message('central_directory_not_found') || '',
|
|
43
|
+
archiveEmptyUnsupported: i18n?.message('archive_empty_unsupported') || '',
|
|
44
|
+
typeDir: i18n?.message('archive_type_dir') || '',
|
|
45
|
+
typeFile: i18n?.message('archive_type_file') || '',
|
|
46
|
+
tableName: i18n?.message('archive_table_name') || '',
|
|
47
|
+
tableType: i18n?.message('archive_table_type') || '',
|
|
48
|
+
tablePacked: i18n?.message('archive_table_packed') || '',
|
|
49
|
+
tableSize: i18n?.message('archive_table_size') || '',
|
|
50
|
+
tablePreview: i18n?.message('archive_table_preview') || '',
|
|
51
|
+
previewEntry: i18n?.button('preview') || '',
|
|
52
|
+
loadingEntry: i18n?.message('loading_entry') || '',
|
|
53
|
+
cannotPreviewEntry: i18n?.message('cannot_preview_entry') || '',
|
|
54
|
+
deflateNotSupported: i18n?.message('deflate_not_supported') || '',
|
|
55
|
+
compressionNotSupported: i18n?.message('compression_not_supported') || '',
|
|
56
|
+
entryCorrupted: i18n?.message('entry_corrupted') || '',
|
|
57
|
+
archiveBufferMissing: i18n?.message('archive_buffer_missing') || '',
|
|
58
|
+
emptyFileInArchive: i18n?.message('empty_file') || ''
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const titleLink = context?.element?.querySelector('.name');
|
|
64
|
+
if (titleLink && !titleLink.hasAttribute('data-vg-filepreview-zip-bind')) {
|
|
65
|
+
titleLink.setAttribute('data-vg-filepreview-zip-bind', 'true');
|
|
66
|
+
titleLink.classList.add('is-preview-action');
|
|
67
|
+
titleLink.addEventListener('click', openArchive);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (nameOnly) {
|
|
71
|
+
return Boolean(titleLink);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!container) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const trigger = document.createElement('button');
|
|
79
|
+
trigger.type = 'button';
|
|
80
|
+
trigger.className = 'vg-filepreview-zip-trigger';
|
|
81
|
+
trigger.textContent = i18n?.button('open_archive') || '';
|
|
82
|
+
trigger.addEventListener('click', openArchive);
|
|
83
|
+
container.appendChild(trigger);
|
|
84
|
+
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export default ZipFilePreviewRenderer;
|