-
Notifications
You must be signed in to change notification settings - Fork 1
/
Demo.tsx
383 lines (353 loc) · 10.9 KB
/
Demo.tsx
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
import {
MutableRefObject,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { useNavigate } from "react-router-dom";
import styled from "styled-components";
import { Input, Switch, InputNumber } from "antd";
import createFileUploader from "../large-file-uploader";
import type {
CreateUploaderResponse,
UploaderData,
} from "../large-file-uploader/type";
import { applyUploadId, uploadChunks, uploadChunksMaybeFailure } from "../mock";
import deleteIcon from "../assets/delete.svg";
import { Loading, SphericalProgress } from "../components";
const DemoWrapper = styled.div`
height: 100vh;
`;
export const Nav = styled.div`
padding-top: 40px;
display: flex;
align-items: center;
gap: 8px;
`;
const Desc = styled.p`
margin-top: 40px;
text-align: center;
`;
const Content = styled.div`
padding: 8px 40px;
display: flex;
justify-content: space-around;
align-items: flex-start;
`;
const Left = styled.div`
padding: 20px;
display: flex;
flex-flow: column nowrap;
align-items: center;
`;
const Right = styled.div`
padding: 20px;
display: flex;
flex-flow: column nowrap;
align-items: center;
`;
const Flex = styled.div`
display: flex;
align-items: center;
`;
const ApiButton = styled(({ children, className, onClick }) => (
<div className={className}>
<button onClick={onClick}>{children}</button>
</div>
))`
margin-top: 20px;
text-align: center;
`;
const DeleteIcon = styled.div`
margin-left: 20px;
padding: 4px;
width: 20px;
height: 20px;
background: url("${deleteIcon}") center / cover no-repeat;
&:hover {
background-color: rgba(0, 0, 0, 0.1);
}
`;
const UploadItem = ({ uploader }: { uploader: UploaderData }) => {
const isSuspended = uploader.status === "suspended";
const isFailure = uploader.status === "failure";
const isComplete = uploader.status === "completed";
const isInitialization = uploader.status === "initialization";
return (
<Flex>
<div style={{ width: "200px" }}>
<p>文件名:{uploader.file.name}</p>
<Flex>
进度:
<SphericalProgress progress={uploader.progress} />
</Flex>
<p>状态: {uploader.status}</p>
</div>
<div>
{isComplete || isInitialization ? null : (
<button
style={
isFailure
? {
backgroundColor: "#f56c6c",
color: "#fff",
}
: undefined
}
onClick={
isSuspended || isFailure ? uploader.resume : uploader.pause
}
>
{isSuspended ? "开始" : isFailure ? "恢复" : "暂停"}
</button>
)}
</div>
<DeleteIcon onClick={uploader.remove} />
</Flex>
);
};
const UploadList = styled.div`
margin-top: 20px;
padding: 20px;
border: 1px solid #888;
border-radius: 16px;
width: 360px;
height: 500px;
overflow-y: auto;
&::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
border-radius: 10px;
background-color: var(--TRANSPARENT);
}
&::-webkit-scrollbar {
width: 8px;
background-color: var(--TRANSPARENT);
}
&::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(255, 255, 255, 0.3);
background-color: var(--SCROLLBAR_THUMB_COLOR);
}
`;
const Demo = () => {
const navigate = useNavigate();
const [offlineStorage, setOfflineStorage] = useState(true);
const [immediately, setImmediately] = useState(true);
const [sizeLimit, setSizeLimit] = useState(null);
const [multiple, setMultiple] = useState(true);
const [accept, setAccept] = useState("");
const [maxNumberOfRequest, setMaxNumberOfRequest] = useState(6);
const [retryCountLimit, setRetryCountLimit] = useState(3);
const [openHash, setOpenHash] = useState(true);
const [numberOfThreads, setNumberOfThreads] = useState(5);
const [checkHashText, setCheckHashText] = useState("");
const [uploadSpeed, setUploadSpeed] = useState(100);
const [uploaderList, setList] = useState<UploaderData[]>([]);
const [uploaderMaybeFailureList, setMaybeFailureList] = useState<
UploaderData[]
>([]);
const [cacheLoadedInNormal, setCacheLoadedInNormal] = useState(
!offlineStorage
);
const [cacheLoadedInMaybeFailure, setCacheLoadedInMaybeFailure] =
useState(false);
const normalUploaderRef: MutableRefObject<CreateUploaderResponse> = useRef(
null as any
);
const maybeFailureUploadRef: MutableRefObject<CreateUploaderResponse> =
useRef(null as any);
const checkHash: ((hash: string) => Promise<number | true>) | undefined =
useMemo(
() =>
checkHashText
? (new Function("hash", `return (${checkHashText})(hash)`) as (
hash: string
) => Promise<number | true>)
: undefined,
[checkHashText]
);
useEffect(() => {
const normalUploader = createFileUploader<number>({
offlineStorage,
immediately,
sizeLimit,
multiple,
accept,
maxNumberOfRequest,
retryCountLimit,
openHash,
numberOfThreads,
checkHash,
spaceName: "normal",
minSlicedFileSize: 5000,
sliceSize: 1000,
async upload({ chunks, start, end, size }, customParams) {
let id = customParams;
// 分块上传后端可能需要一个id来记住上传的文件,所以开始先申请一个id
if (customParams === undefined) {
id = await applyUploadId();
}
await uploadChunks({ id, chunks, start, end, size }, uploadSpeed);
// return的数据会作为下一次的customParams,这样这个文件每次upload都可以带上你需要的customParams
return id;
},
gotCache(cacheList, uploadDataList) {
setCacheLoadedInNormal(true);
setList(uploadDataList);
},
update(uploadDataList) {
setList(uploadDataList);
},
});
normalUploaderRef.current = normalUploader;
const maybeFailureUploader = createFileUploader<number>({
offlineStorage,
immediately,
sizeLimit,
multiple,
accept,
maxNumberOfRequest,
retryCountLimit,
openHash,
numberOfThreads,
checkHash,
spaceName: "maybeFailure",
minSlicedFileSize: 10000,
sliceSize: 1000,
async upload({ chunks, start, end, size }, customParams) {
let id: number = customParams;
// 分块上传后端可能需要一个id来记住上传的文件,所以开始先申请一个id
if (customParams === undefined) {
id = await applyUploadId();
}
await uploadChunksMaybeFailure(
{ id, chunks, start, end, size },
uploadSpeed
);
// return的数据会作为下一次的customParams,这样这个文件每次upload都可以带上你需要的customParams
return id;
},
gotCache(cacheList, uploadDataList) {
setCacheLoadedInMaybeFailure(true);
setMaybeFailureList(uploadDataList);
},
update(uploadDataList) {
setMaybeFailureList(uploadDataList);
},
});
maybeFailureUploadRef.current = maybeFailureUploader;
return () => {
clearAll();
clearAllMaybeFailure();
};
}, [
offlineStorage,
immediately,
sizeLimit,
multiple,
accept,
maxNumberOfRequest,
retryCountLimit,
openHash,
numberOfThreads,
checkHashText,
uploadSpeed,
]);
const handleUpload = useCallback(() => {
if (normalUploaderRef.current) {
normalUploaderRef.current.uploadFile();
}
}, []);
const handleUploadMaybeFailure = useCallback(() => {
if (maybeFailureUploadRef.current) {
maybeFailureUploadRef.current.uploadFile();
}
}, []);
const pauseAll = useCallback(() => {
uploaderList.forEach((uploader) => uploader.pause());
}, [uploaderList]);
const resumeAll = useCallback(() => {
uploaderList.forEach((uploader) => uploader.resume());
}, [uploaderList]);
const pauseAllMaybeFailure = useCallback(() => {
uploaderMaybeFailureList.forEach((uploader) => uploader.pause());
}, [uploaderMaybeFailureList]);
const resumeAllMaybeFailure = useCallback(() => {
uploaderMaybeFailureList.forEach((uploader) => uploader.resume());
}, [uploaderMaybeFailureList]);
const clearAll = useCallback(() => {
uploaderList.forEach((uploader) => uploader.remove());
}, [uploaderList]);
const clearAllMaybeFailure = useCallback(() => {
uploaderMaybeFailureList.forEach((uploader) => uploader.remove());
}, [uploaderMaybeFailureList]);
return (
<DemoWrapper>
<Nav>
<button
onClick={() => {
navigate("/");
}}
>
首页
</button>
<h3>></h3>
<h3>Demo</h3>
</Nav>
<Desc className="read-the-docs">
上传可一次性选多个文件,每个上传项可单独暂停/恢复/删除。请求失败自动重试,默认自动重试3次然后转为手动重试。
</Desc>
<Desc className="read-the-docs" style={{ marginTop: "-12px" }}>
demo中的上传器最小分片限制已被设置为5kb,≥5kb的小文件也可体验断点续传效果。
</Desc>
<ApiButton onClick={() => navigate("/api")}>查看API文档</ApiButton>
<Content>
<Left>
<h3>请求100%成功</h3>
<div style={{ textAlign: "center" }}>
<button onClick={handleUpload}>选择上传文件</button>
{uploaderList.length ? (
<div style={{ marginTop: "20px" }}>
<button onClick={pauseAll}>全部暂停</button>
<button onClick={resumeAll}>全部开始</button>
</div>
) : null}
</div>
<UploadList>
{uploaderList.map((uploader, index) => (
<UploadItem uploader={uploader} key={index} />
))}
{cacheLoadedInNormal ? null : <Loading type="block" />}
</UploadList>
</Left>
<Right>
<h3>请求60%几率失败</h3>
<div style={{ textAlign: "center" }}>
<button onClick={handleUploadMaybeFailure}>选择上传文件</button>
{uploaderMaybeFailureList.length ? (
<div style={{ marginTop: "20px" }}>
<button onClick={pauseAllMaybeFailure}>全部暂停</button>
<button onClick={resumeAllMaybeFailure}>全部开始</button>
</div>
) : null}
</div>
<UploadList>
{uploaderMaybeFailureList.map((uploader, index) => (
<UploadItem uploader={uploader} key={index} />
))}
{cacheLoadedInMaybeFailure ? null : <Loading type="block" />}
</UploadList>
</Right>
</Content>
{/* <div>
<Switch
checkedChildren="开启离线缓存"
unCheckedChildren="关闭离线缓存"
></Switch>
</div> */}
</DemoWrapper>
);
};
export default Demo;