dsh-zipfix 0.0.0-stage → 0.4.1
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/LICENSE +21 -0
- package/README.md +36 -2
- package/client.js +871 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +9 -0
- package/index.js +18 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +77 -4
- package/plugin/index.js +633 -0
- package/plugin/lib/detect.js +436 -0
- package/plugin/lib/encoding.js +369 -0
- package/plugin/lib/fix.js +1228 -0
- package/plugin/lib/report.js +135 -0
- package/plugin/lib/webapi.js +598 -0
- package/plugin/lib/zip.js +842 -0
- package/plugin/locale/en.json +6 -0
- package/plugin/locale/zh.json +6 -0
- package/ui/index.js +26 -0
package/client.js
ADDED
|
@@ -0,0 +1,871 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-zipfix 的浏览器半:左侧边栏入口 + 主面板的「上传 ZIP → 体检 → 修复」页面。
|
|
3
|
+
*
|
|
4
|
+
* 只使用页面模块表提供的 react(无 Harness Client 包依赖),样式全部走主题 token,
|
|
5
|
+
* 网络调用走同源 `/zipfix-api`(Host 半注册),与 agent 工具共用同一套修复逻辑。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
window.__ModuleLoader__.load({
|
|
9
|
+
id: 'dsh-zipfix',
|
|
10
|
+
factory(require) {
|
|
11
|
+
const React = require('react');
|
|
12
|
+
const h = React.createElement;
|
|
13
|
+
|
|
14
|
+
/** 字典命名空间。 */
|
|
15
|
+
const NS = 'zipfix.ui';
|
|
16
|
+
/** 侧边栏入口 id,同时作为 `main` 面板的 key。 */
|
|
17
|
+
const PANEL_ID = 'zipfix';
|
|
18
|
+
|
|
19
|
+
const zh = {
|
|
20
|
+
'sidebar.label': 'ZIP 修复',
|
|
21
|
+
'panel.title': 'ZIP 中文文件名修复器',
|
|
22
|
+
'panel.subtitle':
|
|
23
|
+
'上传 ZIP 后自动判定文件名编码与乱码形态,可下载「修复版 ZIP」(转发给别人也不会乱码),或按正确文件名解压到指定目录。源压缩包不会被删除或覆盖。',
|
|
24
|
+
'drop.title': '把 ZIP 拖到这里,或点击选择文件',
|
|
25
|
+
'drop.hint': '只处理 .zip;上传仅用于本次修复,任务在 24 小时后自动清理',
|
|
26
|
+
'drop.select': '选择 ZIP 文件',
|
|
27
|
+
'drop.drop': '松开鼠标即开始体检',
|
|
28
|
+
'drop.file': '当前文件:{name}({size})',
|
|
29
|
+
'drop.change': '换一个',
|
|
30
|
+
'phase.uploading': '正在上传…',
|
|
31
|
+
'phase.scanning': '正在体检编码…',
|
|
32
|
+
'phase.fixing': '正在重新压缩并逐条校验…',
|
|
33
|
+
'phase.extracting': '正在解压并逐条校验 CRC32…',
|
|
34
|
+
'status.reversible': '可无损还原:文件名是 GBK/Big5 等编码,但 ZIP 未声明 UTF-8',
|
|
35
|
+
'status.no-fix-needed': '文件名编码已合规,无需修复',
|
|
36
|
+
'status.flag-missing-utf8': '名字本身是 UTF-8,只是缺少 0x0800 标志位(重建 ZIP 即可)',
|
|
37
|
+
'status.mixed': '部分条目可还原;另有条目名含 U+FFFD,压缩包内不可逆',
|
|
38
|
+
'status.unknown': '无法识别编码,建议改用 agent 工具并显式指定 encoding',
|
|
39
|
+
'section.scan': '体检结果',
|
|
40
|
+
'field.encoding': '编码',
|
|
41
|
+
'field.confidence': '置信度',
|
|
42
|
+
'field.entries': '条目',
|
|
43
|
+
'field.entriesValue': '{total} 个(文件 {files} / 目录 {dirs})',
|
|
44
|
+
'field.stripRoot': '冗余顶层目录',
|
|
45
|
+
'field.stripRootNone': '无',
|
|
46
|
+
'field.nonAscii': '非 ASCII 条目',
|
|
47
|
+
'field.irreversible': '含 U+FFFD 条目',
|
|
48
|
+
'field.size': '文件大小',
|
|
49
|
+
'section.samples': '样例(原名字 → 新名字)',
|
|
50
|
+
'samples.empty': '没有可展示的条目(可能全部是 ASCII 名字)',
|
|
51
|
+
'section.options': '修复选项',
|
|
52
|
+
'option.stripRoot': '剥离与 ZIP 同名的冗余顶层目录',
|
|
53
|
+
'option.stripRootHint': '解压时会少一层多余的文件夹;修复版 ZIP 只影响下载包内结构',
|
|
54
|
+
'option.encoding': '文件名编码',
|
|
55
|
+
'option.encodingAuto': '自动嗅探(推荐)',
|
|
56
|
+
'action.fix': '生成修复版 ZIP',
|
|
57
|
+
'action.forceFix': '仍要生成修复版 ZIP',
|
|
58
|
+
'action.extract': '解压到目录…',
|
|
59
|
+
'action.download': '下载修复版 ZIP',
|
|
60
|
+
'action.report': '查看审计报告',
|
|
61
|
+
'action.csv': '下载明细 CSV',
|
|
62
|
+
'action.reset': '换一个 ZIP',
|
|
63
|
+
'busy.hint': '处理期间请保持本页面打开;大文件可能需要数十秒。',
|
|
64
|
+
'result.fixed': '修复版 ZIP 已生成',
|
|
65
|
+
'result.fixedDetail': '{entries} 个条目;UTF-8 标志{flag};{check}',
|
|
66
|
+
'result.flagAll': '已全部置位',
|
|
67
|
+
'result.flagMissing': '有缺失',
|
|
68
|
+
'result.checkFull': '逐条 CRC32 校验通过',
|
|
69
|
+
'result.checkStructural': '结构校验通过',
|
|
70
|
+
'result.forced': '该 ZIP 编码已合规,已按「强制处理」执行。',
|
|
71
|
+
'result.extract': '已按正确文件名解压',
|
|
72
|
+
'result.extractDetail': '{files} 个文件 / {dirs} 个目录,失败 {failures} 个;输出目录:{dir}',
|
|
73
|
+
'result.irreversible': '包内另有 {count} 个条目名含 U+FFFD(字节已销毁),未擅自改名;如有源包请用 agent 工具 zipfix_match_source。',
|
|
74
|
+
'result.willDownload': '浏览器会直接保存到下载目录。',
|
|
75
|
+
'error.title': '出错了',
|
|
76
|
+
'error.notZip': '请选择 .zip 压缩包',
|
|
77
|
+
'error.noHost': 'Host API 不可用:请重启 DeepSeek Harness 让新插件生效,或改用 agent 工具 zipfix_*。',
|
|
78
|
+
'error.retry': '重试',
|
|
79
|
+
'hint.title': '提示',
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const en = {
|
|
83
|
+
'sidebar.label': 'ZIP Fixer',
|
|
84
|
+
'panel.title': 'ZIP Chinese Filename Fixer',
|
|
85
|
+
'panel.subtitle':
|
|
86
|
+
'Upload a ZIP to detect the entry-name encoding and mojibake form. Download a repaired ZIP whose filenames are valid UTF-8 with bit 11 set, or extract it with correct filenames into a folder. The uploaded archive is never deleted or overwritten.',
|
|
87
|
+
'drop.title': 'Drop a ZIP here, or click to choose a file',
|
|
88
|
+
'drop.hint': 'Only .zip files; uploads are temporary and cleaned up after 24 hours',
|
|
89
|
+
'drop.select': 'Choose ZIP file',
|
|
90
|
+
'drop.drop': 'Release to start the inspection',
|
|
91
|
+
'drop.file': 'Selected: {name} ({size})',
|
|
92
|
+
'drop.change': 'Change',
|
|
93
|
+
'phase.uploading': 'Uploading…',
|
|
94
|
+
'phase.scanning': 'Inspecting encoding…',
|
|
95
|
+
'phase.fixing': 'Recompressing and verifying every entry…',
|
|
96
|
+
'phase.extracting': 'Extracting and verifying CRC32…',
|
|
97
|
+
'status.reversible': 'Reversible: names are GBK/Big5 but the ZIP does not declare UTF-8',
|
|
98
|
+
'status.no-fix-needed': 'Filenames are already valid; no fix needed',
|
|
99
|
+
'status.flag-missing-utf8': 'Names are UTF-8 but bit 11 is missing; rebuilding the ZIP suffices',
|
|
100
|
+
'status.mixed': 'Some entries are reversible; others contain U+FFFD and are irreversible inside the archive',
|
|
101
|
+
'status.unknown': 'Encoding not recognized; use the agent tool with an explicit encoding',
|
|
102
|
+
'section.scan': 'Inspection result',
|
|
103
|
+
'field.encoding': 'Encoding',
|
|
104
|
+
'field.confidence': 'Confidence',
|
|
105
|
+
'field.entries': 'Entries',
|
|
106
|
+
'field.entriesValue': '{total} ({files} files / {dirs} dirs)',
|
|
107
|
+
'field.stripRoot': 'Redundant root folder',
|
|
108
|
+
'field.stripRootNone': 'None',
|
|
109
|
+
'field.nonAscii': 'Non-ASCII entries',
|
|
110
|
+
'field.irreversible': 'U+FFFD entries',
|
|
111
|
+
'field.size': 'Size',
|
|
112
|
+
'section.samples': 'Samples (current → fixed)',
|
|
113
|
+
'samples.empty': 'Nothing to show (possibly all-ASCII names)',
|
|
114
|
+
'section.options': 'Options',
|
|
115
|
+
'option.stripRoot': 'Strip the redundant root folder named after the ZIP',
|
|
116
|
+
'option.stripRootHint': 'Removes one extra folder level on extract',
|
|
117
|
+
'option.encoding': 'Entry-name encoding',
|
|
118
|
+
'option.encodingAuto': 'Auto-detect (recommended)',
|
|
119
|
+
'action.fix': 'Create repaired ZIP',
|
|
120
|
+
'action.forceFix': 'Create repaired ZIP anyway',
|
|
121
|
+
'action.extract': 'Extract to folder…',
|
|
122
|
+
'action.download': 'Download repaired ZIP',
|
|
123
|
+
'action.report': 'View audit report',
|
|
124
|
+
'action.csv': 'Download CSV',
|
|
125
|
+
'action.reset': 'Choose another ZIP',
|
|
126
|
+
'busy.hint': 'Keep this page open while processing; large archives may take a while.',
|
|
127
|
+
'result.fixed': 'Repaired ZIP created',
|
|
128
|
+
'result.fixedDetail': '{entries} entries; UTF-8 flag {flag}; {check}',
|
|
129
|
+
'result.flagAll': 'set on every entry',
|
|
130
|
+
'result.flagMissing': 'missing on some entries',
|
|
131
|
+
'result.checkFull': 'per-entry CRC32 verified',
|
|
132
|
+
'result.checkStructural': 'structural check passed',
|
|
133
|
+
'result.forced': 'The archive was already compliant; forced processing was applied.',
|
|
134
|
+
'result.extract': 'Extracted with correct filenames',
|
|
135
|
+
'result.extractDetail': '{files} files / {dirs} dirs, {failures} failures; output: {dir}',
|
|
136
|
+
'result.irreversible': '{count} more entry names contain U+FFFD (bytes destroyed) and were left untouched; use the agent tool zipfix_match_source when the source archive exists.',
|
|
137
|
+
'result.willDownload': 'The browser saves it to your downloads folder.',
|
|
138
|
+
'error.title': 'Something went wrong',
|
|
139
|
+
'error.notZip': 'Please choose a .zip archive',
|
|
140
|
+
'error.noHost': 'Host API unavailable: restart DeepSeek Harness so the new plugin loads, or use the zipfix_* agent tools.',
|
|
141
|
+
'error.retry': 'Retry',
|
|
142
|
+
'hint.title': 'Note',
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/* ------------------------------------------------------------------ *
|
|
146
|
+
* 样式(只用主题 token,跟随明暗主题)
|
|
147
|
+
* ------------------------------------------------------------------ */
|
|
148
|
+
|
|
149
|
+
const S = {
|
|
150
|
+
page: {
|
|
151
|
+
height: '100%',
|
|
152
|
+
overflowY: 'auto',
|
|
153
|
+
boxSizing: 'border-box',
|
|
154
|
+
padding: '28px 30px 56px',
|
|
155
|
+
background: 'var(--dsw-alias-bg-base)',
|
|
156
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
157
|
+
},
|
|
158
|
+
inner: { maxWidth: '880px', margin: '0 auto' },
|
|
159
|
+
title: { margin: '0 0 6px', fontSize: '20px', fontWeight: 600, lineHeight: 1.3 },
|
|
160
|
+
subtitle: {
|
|
161
|
+
margin: '0 0 20px',
|
|
162
|
+
fontSize: '13px',
|
|
163
|
+
lineHeight: 1.6,
|
|
164
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
165
|
+
},
|
|
166
|
+
card: {
|
|
167
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
168
|
+
background: 'var(--dsw-alias-bg-layer-1)',
|
|
169
|
+
borderRadius: '12px',
|
|
170
|
+
padding: '18px',
|
|
171
|
+
marginBottom: '16px',
|
|
172
|
+
},
|
|
173
|
+
cardTitle: { margin: '0 0 12px', fontSize: '14px', fontWeight: 600 },
|
|
174
|
+
drop: (over, busy) => ({
|
|
175
|
+
border: `1.5px dashed ${over ? 'var(--dsw-alias-brand-primary)' : 'var(--dsw-alias-border-l2)'}`,
|
|
176
|
+
borderRadius: '12px',
|
|
177
|
+
padding: '26px 20px',
|
|
178
|
+
textAlign: 'center',
|
|
179
|
+
cursor: busy ? 'progress' : 'pointer',
|
|
180
|
+
background: over ? 'var(--dsw-alias-bg-layer-2)' : 'transparent',
|
|
181
|
+
transition: 'border-color 0.15s ease, background 0.15s ease',
|
|
182
|
+
outline: 'none',
|
|
183
|
+
}),
|
|
184
|
+
dropTitle: { fontSize: '14px', fontWeight: 500, marginBottom: '6px' },
|
|
185
|
+
dropHint: { fontSize: '12px', color: 'var(--dsw-alias-label-secondary)', lineHeight: 1.6 },
|
|
186
|
+
fileName: { fontSize: '13px', fontWeight: 500, marginTop: '10px', wordBreak: 'break-all' },
|
|
187
|
+
btn: (variant, disabled) => {
|
|
188
|
+
const primary = variant === 'primary';
|
|
189
|
+
return {
|
|
190
|
+
appearance: 'none',
|
|
191
|
+
border: primary ? '1px solid var(--dsw-alias-brand-primary)' : '1px solid var(--dsw-alias-border-l2)',
|
|
192
|
+
background: primary ? 'var(--dsw-alias-brand-primary)' : 'var(--dsw-alias-bg-layer-1)',
|
|
193
|
+
// 品牌色在深色主题下本身就是白色,文字必须取「底色」才能形成对比:
|
|
194
|
+
// 深色主题 → 深色字 + 白色按钮;浅色主题 → 白字 + 品牌色按钮
|
|
195
|
+
color: primary ? 'var(--dsw-alias-bg-base)' : 'var(--dsw-alias-label-primary)',
|
|
196
|
+
borderRadius: '8px',
|
|
197
|
+
padding: '8px 14px',
|
|
198
|
+
fontSize: '13px',
|
|
199
|
+
fontWeight: primary ? 600 : 500,
|
|
200
|
+
lineHeight: 1.3,
|
|
201
|
+
cursor: disabled ? 'not-allowed' : 'pointer',
|
|
202
|
+
opacity: disabled ? 0.55 : 1,
|
|
203
|
+
marginRight: '8px',
|
|
204
|
+
marginTop: '4px',
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
link: {
|
|
208
|
+
color: 'var(--dsw-alias-brand-primary)',
|
|
209
|
+
textDecoration: 'none',
|
|
210
|
+
fontSize: '13px',
|
|
211
|
+
fontWeight: 500,
|
|
212
|
+
marginRight: '14px',
|
|
213
|
+
},
|
|
214
|
+
row: {
|
|
215
|
+
display: 'flex',
|
|
216
|
+
justifyContent: 'space-between',
|
|
217
|
+
gap: '16px',
|
|
218
|
+
padding: '6px 0',
|
|
219
|
+
fontSize: '13px',
|
|
220
|
+
borderBottom: '1px solid var(--dsw-alias-border-l1)',
|
|
221
|
+
},
|
|
222
|
+
rowLabel: { color: 'var(--dsw-alias-label-secondary)', flex: '0 0 auto' },
|
|
223
|
+
rowValue: { textAlign: 'right', wordBreak: 'break-all', minWidth: 0 },
|
|
224
|
+
mono: { fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace', fontSize: '12px' },
|
|
225
|
+
sampleRow: {
|
|
226
|
+
display: 'flex',
|
|
227
|
+
gap: '10px',
|
|
228
|
+
alignItems: 'baseline',
|
|
229
|
+
padding: '5px 0',
|
|
230
|
+
fontSize: '12px',
|
|
231
|
+
borderBottom: '1px solid var(--dsw-alias-border-l1)',
|
|
232
|
+
},
|
|
233
|
+
sampleOld: {
|
|
234
|
+
flex: '1 1 0',
|
|
235
|
+
minWidth: 0,
|
|
236
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
237
|
+
overflow: 'hidden',
|
|
238
|
+
textOverflow: 'ellipsis',
|
|
239
|
+
whiteSpace: 'nowrap',
|
|
240
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
|
|
241
|
+
},
|
|
242
|
+
sampleArrow: { color: 'var(--dsw-alias-label-secondary)', flex: '0 0 auto' },
|
|
243
|
+
sampleNew: { flex: '1 1 0', minWidth: 0, wordBreak: 'break-all' },
|
|
244
|
+
errorCard: {
|
|
245
|
+
border: '1px solid var(--dsw-alias-state-error-primary)',
|
|
246
|
+
background: 'var(--dsw-alias-bg-layer-1)',
|
|
247
|
+
borderRadius: '12px',
|
|
248
|
+
padding: '14px 16px',
|
|
249
|
+
marginBottom: '16px',
|
|
250
|
+
fontSize: '13px',
|
|
251
|
+
lineHeight: 1.6,
|
|
252
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
253
|
+
},
|
|
254
|
+
warnCard: {
|
|
255
|
+
border: '1px solid var(--dsw-alias-state-warn-primary)',
|
|
256
|
+
background: 'var(--dsw-alias-bg-layer-1)',
|
|
257
|
+
borderRadius: '12px',
|
|
258
|
+
padding: '12px 16px',
|
|
259
|
+
marginBottom: '16px',
|
|
260
|
+
fontSize: '13px',
|
|
261
|
+
lineHeight: 1.6,
|
|
262
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
263
|
+
},
|
|
264
|
+
successCard: {
|
|
265
|
+
border: '1px solid var(--dsw-alias-state-success-primary)',
|
|
266
|
+
background: 'var(--dsw-alias-bg-layer-1)',
|
|
267
|
+
borderRadius: '12px',
|
|
268
|
+
padding: '16px',
|
|
269
|
+
marginBottom: '16px',
|
|
270
|
+
fontSize: '13px',
|
|
271
|
+
lineHeight: 1.7,
|
|
272
|
+
},
|
|
273
|
+
formRow: { display: 'flex', alignItems: 'center', gap: '10px', marginBottom: '12px', fontSize: '13px' },
|
|
274
|
+
checkbox: { accentColor: 'var(--dsw-alias-brand-primary)', width: '15px', height: '15px', margin: 0 },
|
|
275
|
+
select: {
|
|
276
|
+
background: 'var(--dsw-alias-bg-layer-2)',
|
|
277
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
278
|
+
border: '1px solid var(--dsw-alias-border-l2)',
|
|
279
|
+
borderRadius: '7px',
|
|
280
|
+
padding: '5px 8px',
|
|
281
|
+
fontSize: '13px',
|
|
282
|
+
},
|
|
283
|
+
statusLine: { display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '10px', fontSize: '14px', fontWeight: 500 },
|
|
284
|
+
dot: (color) => ({ width: '9px', height: '9px', borderRadius: '50%', background: color, flex: '0 0 auto' }),
|
|
285
|
+
footerHint: { fontSize: '12px', color: 'var(--dsw-alias-label-secondary)', marginTop: '4px' },
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const ENCODINGS = ['auto', 'utf-8', 'gbk', 'gb18030', 'big5'];
|
|
289
|
+
|
|
290
|
+
function statusColor(status) {
|
|
291
|
+
switch (status) {
|
|
292
|
+
case 'reversible':
|
|
293
|
+
case 'flag-missing-utf8':
|
|
294
|
+
return 'var(--dsw-alias-state-warn-primary)';
|
|
295
|
+
case 'no-fix-needed':
|
|
296
|
+
return 'var(--dsw-alias-state-success-primary)';
|
|
297
|
+
case 'mixed':
|
|
298
|
+
case 'unknown':
|
|
299
|
+
return 'var(--dsw-alias-state-error-primary)';
|
|
300
|
+
default:
|
|
301
|
+
return 'var(--dsw-alias-state-idle-primary)';
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function formatBytes(value) {
|
|
306
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '—';
|
|
307
|
+
if (value < 1024) return `${value} B`;
|
|
308
|
+
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KiB`;
|
|
309
|
+
if (value < 1024 * 1024 * 1024) return `${(value / 1024 / 1024).toFixed(1)} MiB`;
|
|
310
|
+
return `${(value / 1024 / 1024 / 1024).toFixed(2)} GiB`;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function truncate(value, max) {
|
|
314
|
+
const text = String(value ?? '');
|
|
315
|
+
if (text.length <= max) return text;
|
|
316
|
+
return `${text.slice(0, Math.max(0, max - 1))}…`;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function label(t, key, vars) {
|
|
320
|
+
const raw = t(key);
|
|
321
|
+
const text = typeof raw === 'string' ? raw : key;
|
|
322
|
+
if (vars === undefined) return text;
|
|
323
|
+
return text.replace(/\{(\w+)\}/g, (match, name) => (vars[name] === undefined || vars[name] === null ? match : String(vars[name])));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/* ------------------------------------------------------------------ *
|
|
327
|
+
* 侧边栏图标
|
|
328
|
+
* ------------------------------------------------------------------ */
|
|
329
|
+
|
|
330
|
+
function ZipfixIcon(props) {
|
|
331
|
+
const size = typeof props.size === 'number' ? props.size : 20;
|
|
332
|
+
const active = props.active === true;
|
|
333
|
+
return h(
|
|
334
|
+
'svg',
|
|
335
|
+
{
|
|
336
|
+
viewBox: '0 0 24 24',
|
|
337
|
+
width: size,
|
|
338
|
+
height: size,
|
|
339
|
+
fill: 'none',
|
|
340
|
+
'aria-hidden': true,
|
|
341
|
+
style: { display: 'block' },
|
|
342
|
+
},
|
|
343
|
+
h('path', {
|
|
344
|
+
d: 'M6 3.5h7.5L18 8v12.5H6z',
|
|
345
|
+
stroke: 'currentColor',
|
|
346
|
+
strokeWidth: 1.5,
|
|
347
|
+
strokeLinejoin: 'round',
|
|
348
|
+
}),
|
|
349
|
+
h('path', { d: 'M13.5 3.5V8H18', stroke: 'currentColor', strokeWidth: 1.5, strokeLinejoin: 'round' }),
|
|
350
|
+
h('path', {
|
|
351
|
+
d: 'M9 13.2l1.9 1.9 3.9-3.9',
|
|
352
|
+
stroke: active ? 'currentColor' : 'var(--dsw-alias-brand-primary)',
|
|
353
|
+
strokeWidth: 2,
|
|
354
|
+
strokeLinecap: 'round',
|
|
355
|
+
strokeLinejoin: 'round',
|
|
356
|
+
}),
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/* ------------------------------------------------------------------ *
|
|
361
|
+
* 主面板页面
|
|
362
|
+
* ------------------------------------------------------------------ */
|
|
363
|
+
|
|
364
|
+
function ZipfixPage(props) {
|
|
365
|
+
const tr = props.tr;
|
|
366
|
+
const api = props.api;
|
|
367
|
+
const [phase, setPhase] = React.useState('idle');
|
|
368
|
+
const [error, setError] = React.useState(null);
|
|
369
|
+
const [file, setFile] = React.useState(null);
|
|
370
|
+
const [task, setTask] = React.useState(null);
|
|
371
|
+
const [result, setResult] = React.useState(null);
|
|
372
|
+
const [extract, setExtract] = React.useState(null);
|
|
373
|
+
const [dragOver, setDragOver] = React.useState(false);
|
|
374
|
+
const [stripRoot, setStripRoot] = React.useState(true);
|
|
375
|
+
const [encoding, setEncoding] = React.useState('auto');
|
|
376
|
+
const [hostReady, setHostReady] = React.useState(true);
|
|
377
|
+
const inputRef = React.useRef(null);
|
|
378
|
+
|
|
379
|
+
const busy = phase === 'uploading' || phase === 'scanning' || phase === 'fixing' || phase === 'extracting';
|
|
380
|
+
|
|
381
|
+
React.useEffect(() => {
|
|
382
|
+
let alive = true;
|
|
383
|
+
api
|
|
384
|
+
.ensureToken()
|
|
385
|
+
.then(() => {
|
|
386
|
+
if (alive) setHostReady(true);
|
|
387
|
+
})
|
|
388
|
+
.catch(() => {
|
|
389
|
+
if (alive) setHostReady(false);
|
|
390
|
+
});
|
|
391
|
+
return () => {
|
|
392
|
+
alive = false;
|
|
393
|
+
};
|
|
394
|
+
}, [api]);
|
|
395
|
+
|
|
396
|
+
const chooseFile = async (selected) => {
|
|
397
|
+
if (!selected) return;
|
|
398
|
+
setError(null);
|
|
399
|
+
setResult(null);
|
|
400
|
+
setExtract(null);
|
|
401
|
+
if (!/\.zip$/i.test(selected.name)) {
|
|
402
|
+
setError(label(tr, 'error.notZip'));
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
setFile(selected);
|
|
406
|
+
setPhase('uploading');
|
|
407
|
+
try {
|
|
408
|
+
await api.ensureToken();
|
|
409
|
+
const response = await api.upload(selected, encoding === 'auto' ? undefined : encoding);
|
|
410
|
+
setTask(response);
|
|
411
|
+
const hasRoot = typeof response.scan.stripRoot === 'string' && response.scan.stripRoot.length > 0;
|
|
412
|
+
setStripRoot(hasRoot);
|
|
413
|
+
setPhase('ready');
|
|
414
|
+
} catch (uploadError) {
|
|
415
|
+
setError(String(uploadError?.message ?? uploadError));
|
|
416
|
+
setPhase('error');
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const runFix = async () => {
|
|
421
|
+
if (task === undefined || task === null) return;
|
|
422
|
+
setError(null);
|
|
423
|
+
setResult(null);
|
|
424
|
+
setPhase('fixing');
|
|
425
|
+
try {
|
|
426
|
+
const force = task.scan.status === 'no-fix-needed';
|
|
427
|
+
const response = await api.fix(task.taskId, {
|
|
428
|
+
stripRoot,
|
|
429
|
+
force,
|
|
430
|
+
encoding: encoding === 'auto' ? undefined : encoding,
|
|
431
|
+
});
|
|
432
|
+
setResult(response);
|
|
433
|
+
setPhase('ready');
|
|
434
|
+
} catch (fixError) {
|
|
435
|
+
setError(String(fixError?.message ?? fixError));
|
|
436
|
+
setPhase('ready');
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const runExtract = async () => {
|
|
441
|
+
if (task === undefined || task === null) return;
|
|
442
|
+
let outputDir = null;
|
|
443
|
+
try {
|
|
444
|
+
outputDir = await api.pickDirectory();
|
|
445
|
+
} catch (pickError) {
|
|
446
|
+
setError(String(pickError?.message ?? pickError));
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (outputDir === null || outputDir === undefined || outputDir === '') return;
|
|
450
|
+
setError(null);
|
|
451
|
+
setExtract(null);
|
|
452
|
+
setPhase('extracting');
|
|
453
|
+
try {
|
|
454
|
+
const response = await api.extract(task.taskId, outputDir, {
|
|
455
|
+
stripRoot,
|
|
456
|
+
force: task.scan.status === 'no-fix-needed',
|
|
457
|
+
encoding: encoding === 'auto' ? undefined : encoding,
|
|
458
|
+
});
|
|
459
|
+
setExtract(response.extract);
|
|
460
|
+
setPhase('ready');
|
|
461
|
+
} catch (extractError) {
|
|
462
|
+
setError(String(extractError?.message ?? extractError));
|
|
463
|
+
setPhase('ready');
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
const reset = () => {
|
|
468
|
+
setFile(null);
|
|
469
|
+
setTask(null);
|
|
470
|
+
setResult(null);
|
|
471
|
+
setExtract(null);
|
|
472
|
+
setError(null);
|
|
473
|
+
setPhase('idle');
|
|
474
|
+
setEncoding('auto');
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const scan = task?.scan ?? null;
|
|
478
|
+
const statusKey = scan ? `status.${scan.status}` : null;
|
|
479
|
+
const statusLabel = statusKey ? label(tr, statusKey) : '';
|
|
480
|
+
|
|
481
|
+
const cards = [];
|
|
482
|
+
|
|
483
|
+
// 上传卡片
|
|
484
|
+
cards.push(
|
|
485
|
+
h(
|
|
486
|
+
'div',
|
|
487
|
+
{ style: S.card, key: 'drop' },
|
|
488
|
+
h(
|
|
489
|
+
'div',
|
|
490
|
+
{
|
|
491
|
+
role: 'button',
|
|
492
|
+
tabIndex: 0,
|
|
493
|
+
'aria-label': label(tr, 'drop.select'),
|
|
494
|
+
onClick: () => {
|
|
495
|
+
if (!busy && inputRef.current) inputRef.current.click();
|
|
496
|
+
},
|
|
497
|
+
onKeyDown: (event) => {
|
|
498
|
+
if (!busy && (event.key === 'Enter' || event.key === ' ')) {
|
|
499
|
+
event.preventDefault();
|
|
500
|
+
if (inputRef.current) inputRef.current.click();
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
onDragOver: (event) => {
|
|
504
|
+
event.preventDefault();
|
|
505
|
+
if (!busy) setDragOver(true);
|
|
506
|
+
},
|
|
507
|
+
onDragLeave: () => setDragOver(false),
|
|
508
|
+
onDrop: (event) => {
|
|
509
|
+
event.preventDefault();
|
|
510
|
+
setDragOver(false);
|
|
511
|
+
if (busy) return;
|
|
512
|
+
const dropped = event.dataTransfer && event.dataTransfer.files ? event.dataTransfer.files[0] : null;
|
|
513
|
+
if (dropped) void chooseFile(dropped);
|
|
514
|
+
},
|
|
515
|
+
style: S.drop(dragOver, busy),
|
|
516
|
+
},
|
|
517
|
+
h('input', {
|
|
518
|
+
ref: inputRef,
|
|
519
|
+
type: 'file',
|
|
520
|
+
accept: '.zip,application/zip,application/x-zip-compressed',
|
|
521
|
+
style: { display: 'none' },
|
|
522
|
+
onChange: (event) => {
|
|
523
|
+
const selected = event.target.files ? event.target.files[0] : null;
|
|
524
|
+
event.target.value = '';
|
|
525
|
+
if (selected) void chooseFile(selected);
|
|
526
|
+
},
|
|
527
|
+
}),
|
|
528
|
+
file === null
|
|
529
|
+
? h('div', null,
|
|
530
|
+
h('div', { style: S.dropTitle }, dragOver ? label(tr, 'drop.drop') : label(tr, 'drop.title')),
|
|
531
|
+
h('div', { style: S.dropHint }, label(tr, 'drop.hint')),
|
|
532
|
+
)
|
|
533
|
+
: h('div', null,
|
|
534
|
+
h('div', { style: S.dropTitle }, label(tr, 'drop.file', { name: file.name, size: formatBytes(file.size) })),
|
|
535
|
+
h('div', { style: S.dropHint }, busy ? label(tr, 'busy.hint') : label(tr, 'drop.hint')),
|
|
536
|
+
),
|
|
537
|
+
),
|
|
538
|
+
busy
|
|
539
|
+
? h('div', { style: { ...S.footerHint, marginTop: '10px' } }, label(tr, `phase.${phase}`))
|
|
540
|
+
: null,
|
|
541
|
+
),
|
|
542
|
+
);
|
|
543
|
+
|
|
544
|
+
// 体检卡片
|
|
545
|
+
if (scan !== null) {
|
|
546
|
+
const rows = [];
|
|
547
|
+
rows.push(
|
|
548
|
+
h('div', { style: S.statusLine, key: 'status' },
|
|
549
|
+
h('span', { style: S.dot(statusColor(scan.status)) }),
|
|
550
|
+
h('span', null, statusLabel),
|
|
551
|
+
),
|
|
552
|
+
);
|
|
553
|
+
rows.push(
|
|
554
|
+
h('div', { style: S.row, key: 'enc' },
|
|
555
|
+
h('span', { style: S.rowLabel }, label(tr, 'field.encoding')),
|
|
556
|
+
h('span', { style: S.rowValue }, `${scan.encoding ?? '—'}(${scan.method ?? '—'},${scan.confidence ?? '—'})`),
|
|
557
|
+
),
|
|
558
|
+
);
|
|
559
|
+
if (scan.entries) {
|
|
560
|
+
rows.push(
|
|
561
|
+
h('div', { style: S.row, key: 'entries' },
|
|
562
|
+
h('span', { style: S.rowLabel }, label(tr, 'field.entries')),
|
|
563
|
+
h('span', { style: S.rowValue }, label(tr, 'field.entriesValue', {
|
|
564
|
+
total: scan.entries.total,
|
|
565
|
+
files: scan.entries.files,
|
|
566
|
+
dirs: scan.entries.directories,
|
|
567
|
+
})),
|
|
568
|
+
),
|
|
569
|
+
);
|
|
570
|
+
rows.push(
|
|
571
|
+
h('div', { style: S.row, key: 'nonascii' },
|
|
572
|
+
h('span', { style: S.rowLabel }, label(tr, 'field.nonAscii')),
|
|
573
|
+
h('span', { style: S.rowValue }, String(scan.entries.nonAscii)),
|
|
574
|
+
),
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
if (scan.stripRoot) {
|
|
578
|
+
rows.push(
|
|
579
|
+
h('div', { style: S.row, key: 'root' },
|
|
580
|
+
h('span', { style: S.rowLabel }, label(tr, 'field.stripRoot')),
|
|
581
|
+
h('span', { style: { ...S.rowValue, ...S.mono } }, scan.stripRoot),
|
|
582
|
+
),
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
if (scan.irreversible && scan.irreversible.length > 0) {
|
|
586
|
+
rows.push(
|
|
587
|
+
h('div', { style: S.row, key: 'irr' },
|
|
588
|
+
h('span', { style: S.rowLabel }, label(tr, 'field.irreversible')),
|
|
589
|
+
h('span', { style: { ...S.rowValue, color: 'var(--dsw-alias-state-error-primary)' } }, String(scan.irreversible.length)),
|
|
590
|
+
),
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
rows.push(
|
|
594
|
+
h('div', { style: S.row, key: 'size' },
|
|
595
|
+
h('span', { style: S.rowLabel }, label(tr, 'field.size')),
|
|
596
|
+
h('span', { style: S.rowValue }, formatBytes(scan.zipSize)),
|
|
597
|
+
),
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
const sampleRows = (scan.samples ?? []).slice(0, 6).map((sample, index) =>
|
|
601
|
+
h('div', { style: S.sampleRow, key: `s${index}` },
|
|
602
|
+
h('span', { style: S.sampleOld, title: sample.original }, truncate(sample.original, 46)),
|
|
603
|
+
h('span', { style: S.sampleArrow }, '→'),
|
|
604
|
+
h('span', { style: { ...S.sampleNew, ...S.mono }, title: sample.fixed }, truncate(sample.fixed, 52)),
|
|
605
|
+
),
|
|
606
|
+
);
|
|
607
|
+
|
|
608
|
+
cards.push(
|
|
609
|
+
h('div', { style: S.card, key: 'scan' },
|
|
610
|
+
h('div', { style: S.cardTitle }, label(tr, 'section.scan')),
|
|
611
|
+
rows,
|
|
612
|
+
h('div', { style: { ...S.cardTitle, marginTop: '14px' } }, label(tr, 'section.samples')),
|
|
613
|
+
sampleRows.length > 0 ? sampleRows : h('div', { style: S.dropHint }, label(tr, 'samples.empty')),
|
|
614
|
+
),
|
|
615
|
+
);
|
|
616
|
+
|
|
617
|
+
// 选项 + 操作卡片
|
|
618
|
+
const noFix = scan.status === 'no-fix-needed';
|
|
619
|
+
cards.push(
|
|
620
|
+
h('div', { style: S.card, key: 'options' },
|
|
621
|
+
h('div', { style: S.cardTitle }, label(tr, 'section.options')),
|
|
622
|
+
h('label', { style: S.formRow },
|
|
623
|
+
h('input', {
|
|
624
|
+
type: 'checkbox',
|
|
625
|
+
checked: stripRoot,
|
|
626
|
+
onChange: (event) => setStripRoot(event.target.checked),
|
|
627
|
+
style: S.checkbox,
|
|
628
|
+
disabled: busy,
|
|
629
|
+
}),
|
|
630
|
+
h('span', null, label(tr, 'option.stripRoot')),
|
|
631
|
+
h('span', { style: { ...S.dropHint, marginLeft: '4px' } }, label(tr, 'option.stripRootHint')),
|
|
632
|
+
),
|
|
633
|
+
h('label', { style: S.formRow },
|
|
634
|
+
h('span', null, label(tr, 'option.encoding')),
|
|
635
|
+
h('select', {
|
|
636
|
+
value: encoding,
|
|
637
|
+
onChange: (event) => setEncoding(event.target.value),
|
|
638
|
+
style: S.select,
|
|
639
|
+
disabled: busy,
|
|
640
|
+
}, ENCODINGS.map((value) =>
|
|
641
|
+
h('option', { key: value, value }, value === 'auto' ? label(tr, 'option.encodingAuto') : value),
|
|
642
|
+
)),
|
|
643
|
+
),
|
|
644
|
+
h('div', { style: { marginTop: '6px' } },
|
|
645
|
+
h('button', {
|
|
646
|
+
type: 'button',
|
|
647
|
+
onClick: () => void runFix(),
|
|
648
|
+
disabled: busy,
|
|
649
|
+
style: S.btn('primary', busy),
|
|
650
|
+
}, label(tr, noFix ? 'action.forceFix' : 'action.fix')),
|
|
651
|
+
h('button', {
|
|
652
|
+
type: 'button',
|
|
653
|
+
onClick: () => void runExtract(),
|
|
654
|
+
disabled: busy,
|
|
655
|
+
style: S.btn('secondary', busy),
|
|
656
|
+
}, label(tr, 'action.extract')),
|
|
657
|
+
h('button', {
|
|
658
|
+
type: 'button',
|
|
659
|
+
onClick: reset,
|
|
660
|
+
disabled: busy,
|
|
661
|
+
style: S.btn('secondary', busy),
|
|
662
|
+
}, label(tr, 'action.reset')),
|
|
663
|
+
),
|
|
664
|
+
busy ? h('div', { style: S.footerHint }, label(tr, `phase.${phase}`)) : null,
|
|
665
|
+
),
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// 修复结果卡片
|
|
670
|
+
if (result !== null && result.result) {
|
|
671
|
+
const payload = result.result;
|
|
672
|
+
const fixed = payload.fixedZip;
|
|
673
|
+
const lines = [];
|
|
674
|
+
lines.push(
|
|
675
|
+
h('div', { style: S.cardTitle, key: 't' }, label(tr, 'result.fixed')),
|
|
676
|
+
);
|
|
677
|
+
if (fixed) {
|
|
678
|
+
lines.push(
|
|
679
|
+
h('div', { key: 'detail' }, label(tr, 'result.fixedDetail', {
|
|
680
|
+
entries: fixed.entries,
|
|
681
|
+
flag: fixed.allUtf8Flag ? label(tr, 'result.flagAll') : label(tr, 'result.flagMissing'),
|
|
682
|
+
check: fixed.fullChecked ? label(tr, 'result.checkFull') : label(tr, 'result.checkStructural'),
|
|
683
|
+
})),
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
if (payload.forced) lines.push(h('div', { key: 'forced', style: { color: 'var(--dsw-alias-label-secondary)' } }, label(tr, 'result.forced')));
|
|
687
|
+
if (result.downloadUrl) {
|
|
688
|
+
lines.push(
|
|
689
|
+
h('div', { key: 'dl', style: { marginTop: '10px' } },
|
|
690
|
+
h('a', { href: result.downloadUrl, style: S.link, download: true }, label(tr, 'action.download')),
|
|
691
|
+
h('a', { href: result.downloadUrl, style: S.link, target: '_blank', rel: 'noreferrer' }, '↗'),
|
|
692
|
+
),
|
|
693
|
+
);
|
|
694
|
+
lines.push(h('div', { key: 'dlhint', style: S.footerHint }, label(tr, 'result.willDownload')));
|
|
695
|
+
}
|
|
696
|
+
if (result.reportUrl) {
|
|
697
|
+
lines.push(
|
|
698
|
+
h('div', { key: 'rep', style: { marginTop: '8px' } },
|
|
699
|
+
h('a', { href: result.reportUrl, style: S.link, target: '_blank', rel: 'noreferrer' }, label(tr, 'action.report')),
|
|
700
|
+
result.csvUrl ? h('a', { href: result.csvUrl, style: S.link, target: '_blank', rel: 'noreferrer' }, label(tr, 'action.csv')) : null,
|
|
701
|
+
),
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
if (payload.irreversible > 0) {
|
|
705
|
+
lines.push(
|
|
706
|
+
h('div', { key: 'irr', style: { marginTop: '8px', color: 'var(--dsw-alias-state-warn-primary)' } },
|
|
707
|
+
label(tr, 'result.irreversible', { count: payload.irreversible }),
|
|
708
|
+
),
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
cards.push(h('div', { style: S.successCard, key: 'result' }, lines));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// 解压结果卡片
|
|
715
|
+
if (extract !== null) {
|
|
716
|
+
cards.push(
|
|
717
|
+
h('div', { style: S.successCard, key: 'extract' },
|
|
718
|
+
h('div', { style: S.cardTitle }, label(tr, 'result.extract')),
|
|
719
|
+
h('div', null, label(tr, 'result.extractDetail', {
|
|
720
|
+
files: extract.files,
|
|
721
|
+
dirs: extract.directories,
|
|
722
|
+
failures: extract.failures,
|
|
723
|
+
dir: extract.outputDir,
|
|
724
|
+
})),
|
|
725
|
+
extract.reportMarkdown
|
|
726
|
+
? h('div', { style: { ...S.footerHint, wordBreak: 'break-all' } }, `报告:${extract.reportMarkdown}`)
|
|
727
|
+
: null,
|
|
728
|
+
),
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// 错误卡片
|
|
733
|
+
const errorCards = [];
|
|
734
|
+
if (error !== null) {
|
|
735
|
+
errorCards.push(
|
|
736
|
+
h('div', { style: S.errorCard, key: 'error' },
|
|
737
|
+
h('div', { style: { fontWeight: 600, marginBottom: '4px' } }, label(tr, 'error.title')),
|
|
738
|
+
h('div', { style: { wordBreak: 'break-word' } }, error)),
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
if (!hostReady) {
|
|
742
|
+
errorCards.push(
|
|
743
|
+
h('div', { style: S.warnCard, key: 'host' },
|
|
744
|
+
h('div', { style: { fontWeight: 600, marginBottom: '4px' } }, label(tr, 'hint.title')),
|
|
745
|
+
h('div', null, label(tr, 'error.noHost'))),
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
return h('div', { style: S.page },
|
|
750
|
+
h('div', { style: S.inner },
|
|
751
|
+
h('h1', { style: S.title }, label(tr, 'panel.title')),
|
|
752
|
+
h('p', { style: S.subtitle }, label(tr, 'panel.subtitle')),
|
|
753
|
+
errorCards,
|
|
754
|
+
cards,
|
|
755
|
+
),
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/* ------------------------------------------------------------------ *
|
|
760
|
+
* 插件入口
|
|
761
|
+
* ------------------------------------------------------------------ */
|
|
762
|
+
|
|
763
|
+
return {
|
|
764
|
+
inject: ['slots', 'locale', 'uiWorkspace'],
|
|
765
|
+
apply(ctx) {
|
|
766
|
+
// 未声明进 LocaleNamespaceMap 的动态命名空间使用单语言注册形式(官方支持的 untyped form)
|
|
767
|
+
ctx.effect(() => ctx.locale.register(NS, 'zh', zh), 'zipfix-ui: zh dictionary');
|
|
768
|
+
ctx.effect(() => ctx.locale.register(NS, 'en', en), 'zipfix-ui: en dictionary');
|
|
769
|
+
const t = ctx.locale.bind(NS);
|
|
770
|
+
const tr = (key, vars) => label(t, key, vars);
|
|
771
|
+
|
|
772
|
+
/** 当前页面的 API token(首次访问 /token 获取)。 */
|
|
773
|
+
let token = null;
|
|
774
|
+
let tokenPromise = null;
|
|
775
|
+
|
|
776
|
+
const loadToken = () => {
|
|
777
|
+
if (token !== null) return Promise.resolve(token);
|
|
778
|
+
if (tokenPromise === null) {
|
|
779
|
+
tokenPromise = fetch('/zipfix-api/token', { headers: { accept: 'application/json' } })
|
|
780
|
+
.then((response) => response.json())
|
|
781
|
+
.then((payload) => {
|
|
782
|
+
if (!payload || payload.ok !== true || typeof payload.token !== 'string') {
|
|
783
|
+
throw new Error('Host API 未返回有效令牌');
|
|
784
|
+
}
|
|
785
|
+
token = payload.token;
|
|
786
|
+
return token;
|
|
787
|
+
})
|
|
788
|
+
.catch((error) => {
|
|
789
|
+
tokenPromise = null;
|
|
790
|
+
throw error;
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
return tokenPromise;
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
const withToken = async (headers) => {
|
|
797
|
+
await loadToken();
|
|
798
|
+
return { ...headers, 'x-zipfix-token': token };
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
const requestJson = async (path, options) => {
|
|
802
|
+
const headers = await withToken(options && options.headers ? options.headers : {});
|
|
803
|
+
const response = await fetch(path, { ...(options || {}), headers });
|
|
804
|
+
const text = await response.text();
|
|
805
|
+
let payload = null;
|
|
806
|
+
try {
|
|
807
|
+
payload = text.length > 0 ? JSON.parse(text) : null;
|
|
808
|
+
} catch {
|
|
809
|
+
throw new Error(`服务响应异常(HTTP ${response.status})`);
|
|
810
|
+
}
|
|
811
|
+
if (!response.ok || (payload && payload.ok === false)) {
|
|
812
|
+
throw new Error((payload && payload.error) || `HTTP ${response.status}`);
|
|
813
|
+
}
|
|
814
|
+
return payload;
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
const api = {
|
|
818
|
+
ensureToken: () => loadToken(),
|
|
819
|
+
upload(file, encoding) {
|
|
820
|
+
const query = new URLSearchParams({ name: file.name });
|
|
821
|
+
if (typeof encoding === 'string' && encoding.length > 0) query.set('encoding', encoding);
|
|
822
|
+
return requestJson(`/zipfix-api/upload?${query.toString()}`, {
|
|
823
|
+
method: 'POST',
|
|
824
|
+
headers: { 'content-type': 'application/zip' },
|
|
825
|
+
body: file,
|
|
826
|
+
});
|
|
827
|
+
},
|
|
828
|
+
fix(taskId, options) {
|
|
829
|
+
return requestJson('/zipfix-api/fix', {
|
|
830
|
+
method: 'POST',
|
|
831
|
+
headers: { 'content-type': 'application/json' },
|
|
832
|
+
body: JSON.stringify({ taskId, ...(options || {}) }),
|
|
833
|
+
});
|
|
834
|
+
},
|
|
835
|
+
extract(taskId, outputDir, options) {
|
|
836
|
+
return requestJson('/zipfix-api/extract', {
|
|
837
|
+
method: 'POST',
|
|
838
|
+
headers: { 'content-type': 'application/json' },
|
|
839
|
+
body: JSON.stringify({ taskId, outputDir, ...(options || {}) }),
|
|
840
|
+
});
|
|
841
|
+
},
|
|
842
|
+
pickDirectory: () => ctx.uiWorkspace.pickDirectory(),
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
ctx.slots.inject('main', () =>
|
|
846
|
+
ctx.slots.register(
|
|
847
|
+
{
|
|
848
|
+
name: 'main',
|
|
849
|
+
key: PANEL_ID,
|
|
850
|
+
locale: NS,
|
|
851
|
+
inject: () => ({ tr, api }),
|
|
852
|
+
},
|
|
853
|
+
ZipfixPage,
|
|
854
|
+
),
|
|
855
|
+
);
|
|
856
|
+
ctx.slots.inject('sidebar.panellist', () =>
|
|
857
|
+
ctx.slots.register(
|
|
858
|
+
{
|
|
859
|
+
name: 'sidebar.panellist',
|
|
860
|
+
id: PANEL_ID,
|
|
861
|
+
order: 20,
|
|
862
|
+
locale: NS,
|
|
863
|
+
label: () => tr('sidebar.label'),
|
|
864
|
+
},
|
|
865
|
+
ZipfixIcon,
|
|
866
|
+
),
|
|
867
|
+
);
|
|
868
|
+
},
|
|
869
|
+
};
|
|
870
|
+
},
|
|
871
|
+
});
|