TaskPreviewModal.tsx
9.34 KB
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
import { useState } from 'react';
import { Button, Modal } from 'antd';
import { useDownloadCenterStore } from './store';
import { getTaskPreviewUrl, hasTaskFile, saveTaskAs } from './engine';
import type { DownloadTask } from './types';
type PreviewKind = 'image' | 'pdf' | 'video' | 'other';
/** 按文件名 / 地址后缀判断预览方式。content-disposition 给的名字优先于直链末段。 */
function previewKind(task: DownloadTask): PreviewKind {
const s = `${task.fileName ?? ''} ${task.fileUrl ?? ''}`.toLowerCase();
if (/\.(png|jpe?g|gif|webp|bmp|svg)(\?|$)/.test(s)) return 'image';
if (/\.pdf(\?|$)/.test(s)) return 'pdf';
if (/\.(mp4|webm|ogg|mov|m4v)(\?|$)/.test(s)) return 'video';
return 'other';
}
/** http 开发环境(非安全上下文)下 clipboard API 不可用,降级到 textarea + execCommand。 */
function copyText(text: string): void {
const native =
typeof navigator !== 'undefined' && navigator.clipboard?.writeText
? navigator.clipboard.writeText(text)
: Promise.reject(new Error('clipboard unavailable'));
native.catch(() => {
const ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
try {
document.execCommand('copy');
} finally {
ta.remove();
}
});
}
/**
* 全系统统一的预览弹窗。
*
* 有字节(probe ok / stream / blob)时用 objectURL 渲染,同源且不受原站鉴权影响;
* 没有字节(跨域无 CORS)时用原始直链渲染——浏览器允许 <img>/<iframe>/<video>
* 显示跨域资源,只是 JS 读不到字节,用户仍可在画面上右键另存。
* 不支持内联的格式(xlsx/doc/zip 等)不自动下载:弹窗只给链接与「下载文件」按钮,
* 用户主动点击才把直链挂给隐藏 iframe 触发下载/本机打开,避免看一眼就下载一次。
* 两种情形都不开新标签,不打断用户当前页面。
*/
export function TaskPreviewModal() {
const previewTaskId = useDownloadCenterStore((s) => s.previewTaskId);
const hidePreview = useDownloadCenterStore((s) => s.hidePreview);
const task = useDownloadCenterStore((s) =>
s.previewTaskId ? s.tasks.find((t) => t.id === s.previewTaskId) : undefined,
);
const [copied, setCopied] = useState(false);
// other 格式:弹窗打开时绝不自动下载。只有用户主动点「下载文件」才挂载 iframe 触发。
// state 带 taskId:切到别的任务时旧请求自动失效;关闭弹窗时清空,重开同一任务也不会再触发。
const [downloadReq, setDownloadReq] = useState<{ taskId: string; seq: number }>({ taskId: '', seq: 0 });
if (!previewTaskId || !task) return null;
// previewUrl 仅用于渲染:有字节是 blob:,无字节是直链。
// linkUrl 是真实文件地址(出口已把明文 http 升级为 https),用于展示和复制——
// 复制 blob: 粘到浏览器打不开,必须区分开。
const previewUrl = getTaskPreviewUrl(previewTaskId);
if (!previewUrl) return null;
const linkUrl = task.fileUrl;
const kind = previewKind(task);
const hasBytes = hasTaskFile(previewTaskId);
const downloadActive = downloadReq.taskId === previewTaskId && downloadReq.seq > 0;
const handleCopy = () => {
copyText(linkUrl ?? previewUrl);
setCopied(true);
window.setTimeout(() => setCopied(false), 1600);
};
const handleClose = () => {
setDownloadReq({ taskId: '', seq: 0 });
setCopied(false);
hidePreview();
};
return (
<Modal
open
zIndex={1200}
title={`预览 · ${task.label}`}
width={kind === 'image' ? 720 : 860}
onCancel={handleClose}
footer={
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12 }}>
<span style={{ fontSize: 12, color: '#8c8c8c', textAlign: 'left' }}>
{hasBytes
? '文件已缓存,点「保存到本地」即可保存'
: kind === 'other'
? '此格式无法在线预览,需主动点「下载文件」才会触发下载'
: '请在预览画面上右键另存为'}
</span>
<span style={{ display: 'flex', gap: 8, flexShrink: 0 }}>
{/* 复制真实文件地址(出口已升级 https),不是 blob: 预览地址。 */}
{linkUrl && <Button onClick={handleCopy}>{copied ? '已复制' : '复制地址'}</Button>}
{/* 只有确实拿到字节才显示:没有字节时 a[download] 也救不了跨域直链。 */}
{hasBytes && (
<Button type="primary" onClick={() => saveTaskAs(task.id)}>
保存到本地
</Button>
)}
<Button onClick={handleClose}>关闭</Button>
</span>
</div>
}
>
{kind === 'image' && (
<div style={{ textAlign: 'center', background: '#fafafa', padding: 12 }}>
<img
src={previewUrl}
alt={task.label}
style={{ maxWidth: '100%', maxHeight: '65vh', objectFit: 'contain' }}
/>
</div>
)}
{kind === 'pdf' && (
<iframe src={previewUrl} title={task.label} style={{ width: '100%', height: '65vh', border: 0 }} />
)}
{kind === 'video' && (
<div style={{ textAlign: 'center', background: '#fafafa', padding: 12 }}>
<video src={previewUrl} controls style={{ maxWidth: '100%', maxHeight: '65vh' }} />
</div>
)}
{kind === 'other' && !hasBytes && (
<div style={{ padding: 8 }}>
<div style={{ color: '#8c8c8c', marginBottom: 8 }}>
此格式无法在页面内渲染(xlsx / doc / zip 等)。点「下载文件」后浏览器才会下载或用本机程序打开;
仅打开预览不会下载。没反应可再点「重新下载」,或复制下方地址到浏览器地址栏。
</div>
{/* 只在用户主动点下载后才挂载 iframe:src 一挂上浏览器就会触发下载/本机打开。
打开弹窗先不渲染,避免看一眼预览就莫名下载一次。0×0 移出视口不占版面。 */}
{downloadActive && (
<iframe
key={downloadReq.seq}
src={previewUrl}
title={task.label}
aria-hidden="true"
style={{ position: 'fixed', width: 0, height: 0, border: 0, left: -9999, top: -9999 }}
/>
)}
<div style={{ display: 'flex', gap: 8, marginTop: 8 }}>
<Button
type="primary"
size="small"
onClick={() => setDownloadReq({ taskId: previewTaskId, seq: downloadReq.seq + 1 })}
>
{downloadActive ? '重新下载' : '下载文件'}
</Button>
</div>
{linkUrl && (
<div
role="button"
tabIndex={0}
title="点击复制地址"
onClick={handleCopy}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') handleCopy();
}}
style={{
marginTop: 8,
padding: 10,
background: '#fafafa',
border: '1px solid #f0f0f0',
borderRadius: 4,
fontFamily: 'monospace',
fontSize: 12,
wordBreak: 'break-all',
cursor: 'pointer',
color: '#1677ff',
}}
>
{linkUrl}
</div>
)}
</div>
)}
{kind === 'other' && hasBytes && (
<div style={{ padding: 12 }}>
<div style={{ color: '#8c8c8c', lineHeight: 1.7, marginBottom: 8 }}>
此格式无法在页面内预览,但文件已完整缓存到本地。点右下角「保存到本地」即可保存,
保存位置由浏览器的下载设置决定。
</div>
{linkUrl && (
<div
role="button"
tabIndex={0}
title="点击复制地址"
onClick={handleCopy}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') handleCopy();
}}
style={{
padding: 10,
background: '#fafafa',
border: '1px solid #f0f0f0',
borderRadius: 4,
fontFamily: 'monospace',
fontSize: 12,
wordBreak: 'break-all',
cursor: 'pointer',
color: '#1677ff',
}}
>
{linkUrl}
</div>
)}
</div>
)}
{kind !== 'other' && linkUrl && (
<div
role="button"
tabIndex={0}
title="点击复制地址"
onClick={handleCopy}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') handleCopy();
}}
style={{
margin: '8px 12px 0',
padding: 10,
background: '#fafafa',
border: '1px solid #f0f0f0',
borderRadius: 4,
fontFamily: 'monospace',
fontSize: 12,
wordBreak: 'break-all',
cursor: 'pointer',
color: '#1677ff',
}}
>
{linkUrl}
</div>
)}
</Modal>
);
}