-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss-file-transfer-enhanced.html
More file actions
2159 lines (1895 loc) · 93.4 KB
/
Copy pathoss-file-transfer-enhanced.html
File metadata and controls
2159 lines (1895 loc) · 93.4 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>阿里云OSS文件中转站 - 增强版</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
<!-- 阿里云OSS SDK -->
<script src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
<!-- 媒体预览相关库 -->
<link href="https://cdn.jsdelivr.net/npm/viewerjs@1.10.5/dist/viewer.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/viewerjs@1.10.5/dist/viewer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@2.16.105/build/pdf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/plyr@3.7.2/dist/plyr.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/plyr@3.7.2/dist/plyr.css" rel="stylesheet">
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#1890ff', // 阿里云主题色
secondary: '#36b37e',
neutral: '#f5f5f5',
},
fontFamily: {
sans: ['Inter', 'system-ui', 'sans-serif'],
},
}
}
}
</script>
<style type="text/tailwindcss">
@layer utilities {
.content-auto {
content-visibility: auto;
}
.transition-custom {
transition: all 0.3s ease;
}
.shadow-hover {
@apply hover:shadow-lg transition-custom;
}
}
</style>
<style>
/* 自定义样式 */
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1000;
overflow-y: auto;
}
.modal-content {
position: relative;
background-color: #fff;
margin: 2rem auto;
padding: 1rem;
border-radius: 0.5rem;
max-width: 90%;
max-height: 90%;
overflow: auto;
}
.close {
position: absolute;
top: 0.5rem;
right: 1rem;
font-size: 1.5rem;
cursor: pointer;
}
.folder-item {
cursor: pointer;
}
.folder-item:hover {
background-color: #f0f9ff;
}
.file-checkbox {
width: 1.2rem;
height: 1.2rem;
}
/* 预览相关样式 */
.preview-container {
width: 100%;
height: 70vh;
display: flex;
align-items: center;
justify-content: center;
overflow: auto;
}
.preview-image {
max-width: 100%;
max-height: 100%;
object-fit: contain;
}
.preview-pdf {
width: 100%;
height: 100%;
border: none;
}
.preview-video, .preview-audio {
width: 100%;
max-height: 70vh;
}
.preview-text {
width: 100%;
height: 100%;
padding: 1rem;
overflow: auto;
white-space: pre-wrap;
font-family: monospace;
background-color: #f8f9fa;
border-radius: 0.25rem;
}
/* 分页控制 */
.pagination-btn {
@apply px-3 py-1 border rounded-md hover:bg-gray-100 transition-custom;
}
.pagination-btn.active {
@apply bg-primary text-white hover:bg-primary/90;
}
/* 上传历史记录样式 */
.history-item {
@apply border-l-4 pl-4 py-2 mb-2;
}
.history-success {
@apply border-green-500;
}
.history-error {
@apply border-red-500;
}
.history-pending {
@apply border-yellow-500;
}
</style>
</head>
<body class="bg-gray-50 min-h-screen font-sans">
<div class="container mx-auto px-4 py-8 max-w-5xl">
<header class="text-center mb-8">
<h1 class="text-[clamp(1.8rem,5vw,3rem)] font-bold text-gray-800 mb-2">
<i class="fa fa-cloud text-primary mr-2"></i>阿里云OSS文件中转站
</h1>
<p class="text-gray-600 text-lg">上传文件到OSS,生成可分享链接</p>
</header>
<!-- 导航栏 -->
<div class="bg-white rounded-xl shadow-md p-4 mb-8">
<div class="flex flex-wrap gap-2">
<button id="nav-files" class="px-4 py-2 rounded-lg bg-primary text-white hover:bg-primary/90 transition-custom">
<i class="fa fa-files-o mr-2"></i>文件管理
</button>
<button id="nav-upload" class="px-4 py-2 rounded-lg bg-gray-200 text-gray-700 hover:bg-gray-300 transition-custom">
<i class="fa fa-upload mr-2"></i>上传文件
</button>
<button id="nav-history" class="px-4 py-2 rounded-lg bg-gray-200 text-gray-700 hover:bg-gray-300 transition-custom">
<i class="fa fa-history mr-2"></i>上传历史
</button>
<button id="nav-config" class="px-4 py-2 rounded-lg bg-gray-200 text-gray-700 hover:bg-gray-300 transition-custom">
<i class="fa fa-cog mr-2"></i>OSS配置
</button>
</div>
</div>
<!-- 配置区域 -->
<div id="config-section" class="bg-white rounded-xl shadow-md p-6 mb-8 hidden">
<h2 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
<i class="fa fa-cog text-primary mr-2"></i>OSS配置
</h2>
<div class="grid md:grid-cols-2 gap-4">
<div>
<label class="block text-gray-700 mb-2" for="accessKeyId">AccessKey ID</label>
<input type="text" id="accessKeyId" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="输入AccessKey ID">
</div>
<div>
<label class="block text-gray-700 mb-2" for="accessKeySecret">AccessKey Secret</label>
<input type="password" id="accessKeySecret" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="输入AccessKey Secret">
</div>
<div>
<label class="block text-gray-700 mb-2" for="bucket">Bucket名称</label>
<input type="text" id="bucket" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="例如:my-bucket">
</div>
<div>
<label class="block text-gray-700 mb-2" for="region">地域</label>
<input type="text" id="region" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="例如:oss-cn-beijing">
</div>
<div class="md:col-span-2">
<label class="block text-gray-700 mb-2" for="domain">自定义域名(可选)</label>
<input type="text" id="domain" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="例如:https://files.example.com">
</div>
<div class="md:col-span-2">
<label class="block text-gray-700 mb-2" for="chunkSize">分片大小(MB)</label>
<input type="number" id="chunkSize" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="默认为5MB" value="5" min="1" max="100">
<p class="text-sm text-gray-500 mt-1">大文件会自动分片上传,这里设置每个分片的大小</p>
</div>
<div class="md:col-span-2">
<button id="save-config" class="bg-primary hover:bg-primary/90 text-white px-6 py-2 rounded-lg shadow-hover flex items-center">
<i class="fa fa-save mr-2"></i>保存配置
</button>
</div>
</div>
</div>
<!-- 上传区域 -->
<div id="upload-section" class="bg-white rounded-xl shadow-md p-6 mb-8 hidden">
<h2 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
<i class="fa fa-upload text-primary mr-2"></i>上传文件
</h2>
<!-- 当前路径导航 -->
<div id="upload-breadcrumb" class="flex flex-wrap items-center text-sm mb-4 bg-gray-50 p-2 rounded-lg">
<a href="#" data-path="" class="text-primary hover:text-primary/80 flex items-center">
<i class="fa fa-home mr-1"></i>根目录
</a>
<span id="current-path-parts"></span>
</div>
<div id="drop-area" class="border-2 border-dashed border-gray-300 hover:border-primary rounded-lg p-8 text-center cursor-pointer transition-custom bg-neutral">
<i class="fa fa-cloud-upload text-5xl text-gray-400 mb-4"></i>
<p class="text-gray-600 mb-2">拖放文件到这里,或者</p>
<label class="bg-primary hover:bg-primary/90 text-white px-6 py-2 rounded-lg shadow-hover inline-block cursor-pointer">
<i class="fa fa-file mr-2"></i>选择文件
</label>
<input type="file" id="file-input" class="hidden" multiple>
</div>
<div class="flex justify-between items-center mt-4">
<div>
<label class="block text-gray-700 mb-2" for="chunkSize">分片大小(MB)</label>
<input type="number" id="chunkSize" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="默认为5MB" value="5" min="1" max="100">
<p class="text-sm text-gray-500 mt-1">大文件会自动分片上传,这里设置每个分片的大小</p>
</div>
<div class="md:col-span-2">
<label class="block text-gray-700 mb-2">默认文件有效期</label>
<div class="flex items-center space-x-4">
<label class="inline-flex items-center">
<input type="radio" name="defaultExpiry" value="0" class="form-radio" checked>
<span class="ml-2">永久有效</span>
</label>
<label class="inline-flex items-center">
<input type="radio" name="defaultExpiry" value="1" class="form-radio">
<span class="ml-2">1天</span>
</label>
<label class="inline-flex items-center">
<input type="radio" name="defaultExpiry" value="7" class="form-radio">
<span class="ml-2">7天</span>
</label>
<label class="inline-flex items-center">
<input type="radio" name="defaultExpiry" value="30" class="form-radio">
<span class="ml-2">30天</span>
</label>
<label class="inline-flex items-center">
<input type="radio" name="defaultExpiry" value="custom" class="form-radio">
<span class="ml-2">自定义</span>
</label>
</div>
<div id="custom-expiry-container" class="mt-2 hidden">
<input type="number" id="customExpiryDays" class="w-32 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="天数" min="1" max="365">
<span class="ml-2">天</span>
</div>
<p class="text-sm text-gray-500 mt-1">设置上传文件的默认有效期,过期后文件将自动删除</p>
</div>
<div>
<button id="cancel-all-uploads" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg shadow-hover flex items-center hidden">
<i class="fa fa-times mr-2"></i>取消所有上传
</button>
</div>
</div>
<div id="upload-queue" class="mt-6 hidden">
<h3 class="font-medium text-gray-700 mb-3">上传队列</h3>
<div id="upload-items" class="space-y-3"></div>
</div>
</div>
<!-- 文件管理区域 -->
<div id="files-section" class="bg-white rounded-xl shadow-md p-6 mb-8">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-semibold text-gray-800 flex items-center">
<i class="fa fa-files-o text-primary mr-2"></i>文件管理
</h2>
<div class="flex space-x-2">
<button id="create-folder-btn" class="bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg shadow-hover flex items-center">
<i class="fa fa-plus mr-2"></i>新建文件夹
</button>
<button id="batch-delete-btn" class="bg-red-500 hover:bg-red-600 text-white px-3 py-1 rounded-lg shadow-hover flex items-center hidden">
<i class="fa fa-trash mr-1"></i>批量删除
</button>
<button id="select-all-btn" class="bg-gray-200 hover:bg-gray-300 text-gray-700 px-3 py-1 rounded-lg shadow-hover flex items-center">
<i class="fa fa-check-square-o mr-1"></i>全选
</button>
<button id="deselect-all-btn" class="bg-gray-200 hover:bg-gray-300 text-gray-700 px-3 py-1 rounded-lg shadow-hover flex items-center hidden">
<i class="fa fa-square-o mr-1"></i>取消全选
</button>
</div>
</div>
<!-- 搜索和筛选 -->
<div class="mb-4 bg-gray-50 p-4 rounded-lg">
<div class="flex flex-wrap gap-2">
<div class="flex-grow">
<div class="relative">
<input type="text" id="search-input" class="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="搜索文件名...">
<i class="fa fa-search absolute left-3 top-3 text-gray-400"></i>
</div>
</div>
<div>
<select id="filter-type" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom">
<option value="">所有类型</option>
<option value="image">图片</option>
<option value="video">视频</option>
<option value="audio">音频</option>
<option value="document">文档</option>
<option value="other">其他</option>
</select>
</div>
<div>
<select id="sort-by" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom">
<option value="date-desc">最新上传</option>
<option value="date-asc">最早上传</option>
<option value="name-asc">名称 A-Z</option>
<option value="name-desc">名称 Z-A</option>
<option value="size-desc">大小 ↓</option>
<option value="size-asc">大小 ↑</option>
</select>
</div>
</div>
</div>
<!-- 当前路径导航 -->
<div id="file-breadcrumb" class="flex flex-wrap items-center text-sm mb-4 bg-gray-50 p-2 rounded-lg">
<a href="#" data-path="" class="text-primary hover:text-primary/80 flex items-center">
<i class="fa fa-home mr-1"></i>根目录
</a>
<span id="file-path-parts"></span>
</div>
<!-- 文件列表 -->
<div id="file-list" class="space-y-2">
<div class="text-center text-gray-500 py-8 border border-dashed border-gray-300 rounded-lg">
<i class="fa fa-folder-open text-4xl mb-2"></i>
<p>暂无上传的文件</p>
</div>
</div>
<!-- 分页控制 -->
<div id="pagination" class="mt-4 flex justify-between items-center">
<div class="text-sm text-gray-500">
显示 <span id="page-info">0-0</span> 项,共 <span id="total-items">0</span> 项
</div>
<div class="flex space-x-2">
<button id="prev-page" class="pagination-btn" disabled>上一页</button>
<div id="page-numbers" class="flex space-x-2"></div>
<button id="next-page" class="pagination-btn" disabled>下一页</button>
</div>
</div>
</div>
<!-- 上传历史记录 -->
<div id="history-section" class="bg-white rounded-xl shadow-md p-6 mb-8 hidden">
<h2 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
<i class="fa fa-history text-primary mr-2"></i>上传历史记录
</h2>
<div class="mb-4">
<button id="clear-history" class="bg-gray-200 hover:bg-gray-300 text-gray-700 px-4 py-2 rounded-lg shadow-hover flex items-center">
<i class="fa fa-trash mr-2"></i>清空历史记录
</button>
</div>
<div id="history-list" class="space-y-2">
<div class="text-center text-gray-500 py-8 border border-dashed border-gray-300 rounded-lg">
<i class="fa fa-history text-4xl mb-2"></i>
<p>暂无上传历史记录</p>
</div>
</div>
</div>
</div>
<!-- 预览模态框 -->
<div id="preview-modal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2 id="preview-title" class="text-xl font-semibold mb-4 text-gray-800"></h2>
<div id="preview-container" class="preview-container"></div>
<div class="mt-4 flex justify-between">
<div>
<span id="preview-info" class="text-sm text-gray-500"></span>
</div>
<div>
<a id="preview-download" href="#" download class="bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg shadow-hover inline-block">
<i class="fa fa-download mr-2"></i>下载
</a>
</div>
</div>
</div>
</div>
<!-- 新建文件夹模态框 -->
<div id="folder-modal" class="modal">
<div class="modal-content" style="max-width: 500px;">
<span class="close">×</span>
<h2 class="text-xl font-semibold mb-4 text-gray-800">新建文件夹</h2>
<div class="mb-4">
<label class="block text-gray-700 mb-2" for="folder-name">文件夹名称</label>
<input type="text" id="folder-name" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary transition-custom" placeholder="输入文件夹名称">
</div>
<div class="flex justify-end">
<button id="create-folder-confirm" class="bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg shadow-hover">
<i class="fa fa-check mr-2"></i>创建
</button>
</div>
</div>
</div>
<script>
// 全局变量
let ossClient = null;
let config = JSON.parse(localStorage.getItem('ossConfig')) || {};
let currentPath = ''; // 当前文件夹路径
let uploadTasks = []; // 上传任务列表
let selectedFiles = new Set(); // 已选择的文件
let currentPage = 1;
let itemsPerPage = 10;
let searchTerm = '';
let fileTypeFilter = '';
let sortBy = 'date-desc';
let imageViewer = null;
let expiryCheckInterval = null; // 定期检查过期文件的定时器
// DOM元素
const dropArea = document.getElementById('drop-area');
const fileInput = document.getElementById('file-input');
const uploadQueue = document.getElementById('upload-queue');
const uploadItems = document.getElementById('upload-items');
const fileList = document.getElementById('file-list');
const saveConfigBtn = document.getElementById('save-config');
const createFolderBtn = document.getElementById('create-folder-btn');
const createFolderConfirmBtn = document.getElementById('create-folder-confirm');
const folderModal = document.getElementById('folder-modal');
const folderNameInput = document.getElementById('folder-name');
const previewModal = document.getElementById('preview-modal');
const searchInput = document.getElementById('search-input');
const filterType = document.getElementById('filter-type');
const sortBySelect = document.getElementById('sort-by');
const selectAllBtn = document.getElementById('select-all-btn');
const deselectAllBtn = document.getElementById('deselect-all-btn');
const batchDeleteBtn = document.getElementById('batch-delete-btn');
const cancelAllUploadsBtn = document.getElementById('cancel-all-uploads');
const clearHistoryBtn = document.getElementById('clear-history');
const historyList = document.getElementById('history-list');
// 导航按钮
const navFiles = document.getElementById('nav-files');
const navUpload = document.getElementById('nav-upload');
const navHistory = document.getElementById('nav-history');
const navConfig = document.getElementById('nav-config');
// 页面区域
const filesSection = document.getElementById('files-section');
const uploadSection = document.getElementById('upload-section');
const historySection = document.getElementById('history-section');
const configSection = document.getElementById('config-section');
// 初始化页面
function init() {
// 填充配置表单
if (config.accessKeyId) document.getElementById('accessKeyId').value = config.accessKeyId;
if (config.accessKeySecret) document.getElementById('accessKeySecret').value = config.accessKeySecret;
if (config.bucket) document.getElementById('bucket').value = config.bucket;
if (config.region) document.getElementById('region').value = config.region;
if (config.domain) document.getElementById('domain').value = config.domain;
if (config.chunkSize) document.getElementById('chunkSize').value = config.chunkSize;
// 设置默认有效期
if (config.defaultExpiry !== undefined) {
const expiryRadios = document.getElementsByName('defaultExpiry');
let found = false;
for (const radio of expiryRadios) {
if (radio.value === String(config.defaultExpiry)) {
radio.checked = true;
found = true;
break;
}
}
// 如果是自定义天数
if (!found && config.defaultExpiry > 0) {
const customRadio = document.querySelector('input[name="defaultExpiry"][value="custom"]');
if (customRadio) {
customRadio.checked = true;
document.getElementById('customExpiryDays').value = config.defaultExpiry;
document.getElementById('custom-expiry-container').classList.remove('hidden');
}
}
}
// 尝试初始化OSS客户端
initOSSClient();
// 检查过期文件
checkExpiredFiles();
// 加载已上传文件列表
loadFileList();
// 加载上传历史记录
loadUploadHistory();
// 事件监听
setupEventListeners();
// 初始化导航
updateBreadcrumb();
}
// 初始化OSS客户端
function initOSSClient() {
if (config.accessKeyId && config.accessKeySecret && config.bucket && config.region) {
try {
ossClient = new OSS({
accessKeyId: config.accessKeyId,
accessKeySecret: config.accessKeySecret,
bucket: config.bucket,
region: config.region,
secure: true, // 使用HTTPS
timeout: 60000, // 超时时间设置为60秒
cname: !!config.domain, // 如果设置了自定义域名,则启用cname
endpoint: config.domain ? config.domain.replace(/^https?:\/\//, '') : undefined,
cors: true, // 允许跨域
headers: {
'Cache-Control': 'no-cache' // 防止缓存问题
}
});
return true;
} catch (e) {
showNotification('初始化OSS客户端失败: ' + e.message, 'error');
return false;
}
}
return false;
}
// 设置事件监听
function setupEventListeners() {
// 保存配置
saveConfigBtn.addEventListener('click', saveConfig);
// 文件上传相关
dropArea.addEventListener('dragover', handleDragOver);
dropArea.addEventListener('dragleave', handleDragLeave);
dropArea.addEventListener('drop', handleDrop);
// 点击整个拖放区域都会触发文件选择
dropArea.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', handleFileSelect);
// 有效期自定义选项
const expiryRadios = document.getElementsByName('defaultExpiry');
expiryRadios.forEach(radio => {
radio.addEventListener('change', function() {
const customContainer = document.getElementById('custom-expiry-container');
if (this.value === 'custom') {
customContainer.classList.remove('hidden');
} else {
customContainer.classList.add('hidden');
}
});
});
// 导航按钮
navFiles.addEventListener('click', () => switchSection('files'));
navUpload.addEventListener('click', () => switchSection('upload'));
navHistory.addEventListener('click', () => switchSection('history'));
navConfig.addEventListener('click', () => switchSection('config'));
// 文件夹操作
createFolderBtn.addEventListener('click', () => showFolderModal());
createFolderConfirmBtn.addEventListener('click', createFolder);
// 关闭模态框
document.querySelectorAll('.close').forEach(closeBtn => {
closeBtn.addEventListener('click', function() {
this.closest('.modal').style.display = 'none';
});
});
// 点击模态框外部关闭
window.addEventListener('click', function(event) {
if (event.target.classList.contains('modal')) {
event.target.style.display = 'none';
}
});
// 搜索和筛选
searchInput.addEventListener('input', debounce(function() {
searchTerm = this.value.trim().toLowerCase();
currentPage = 1;
loadFileList();
}, 300));
filterType.addEventListener('change', function() {
fileTypeFilter = this.value;
currentPage = 1;
loadFileList();
});
sortBySelect.addEventListener('change', function() {
sortBy = this.value;
loadFileList();
});
// 批量操作
selectAllBtn.addEventListener('click', selectAllFiles);
deselectAllBtn.addEventListener('click', deselectAllFiles);
batchDeleteBtn.addEventListener('click', batchDeleteFiles);
// 取消所有上传
cancelAllUploadsBtn.addEventListener('click', cancelAllUploads);
// 清空历史记录
clearHistoryBtn.addEventListener('click', clearUploadHistory);
// 面包屑导航
document.getElementById('file-breadcrumb').addEventListener('click', handleBreadcrumbClick);
document.getElementById('upload-breadcrumb').addEventListener('click', handleBreadcrumbClick);
}
// 切换页面区域
function switchSection(section) {
// 隐藏所有区域
filesSection.classList.add('hidden');
uploadSection.classList.add('hidden');
historySection.classList.add('hidden');
configSection.classList.add('hidden');
// 重置导航按钮样式
navFiles.classList.remove('bg-primary', 'text-white');
navUpload.classList.remove('bg-primary', 'text-white');
navHistory.classList.remove('bg-primary', 'text-white');
navConfig.classList.remove('bg-primary', 'text-white');
navFiles.classList.add('bg-gray-200', 'text-gray-700');
navUpload.classList.add('bg-gray-200', 'text-gray-700');
navHistory.classList.add('bg-gray-200', 'text-gray-700');
navConfig.classList.add('bg-gray-200', 'text-gray-700');
// 显示选中区域
switch (section) {
case 'files':
filesSection.classList.remove('hidden');
navFiles.classList.remove('bg-gray-200', 'text-gray-700');
navFiles.classList.add('bg-primary', 'text-white');
loadFileList(); // 刷新文件列表
break;
case 'upload':
uploadSection.classList.remove('hidden');
navUpload.classList.remove('bg-gray-200', 'text-gray-700');
navUpload.classList.add('bg-primary', 'text-white');
break;
case 'history':
historySection.classList.remove('hidden');
navHistory.classList.remove('bg-gray-200', 'text-gray-700');
navHistory.classList.add('bg-primary', 'text-white');
loadUploadHistory(); // 刷新历史记录
break;
case 'config':
configSection.classList.remove('hidden');
navConfig.classList.remove('bg-gray-200', 'text-gray-700');
navConfig.classList.add('bg-primary', 'text-white');
break;
}
}
// 保存配置
function saveConfig() {
// 获取选中的有效期选项
const expiryRadios = document.getElementsByName('defaultExpiry');
let defaultExpiry = 0; // 默认永久有效
for (const radio of expiryRadios) {
if (radio.checked) {
if (radio.value === 'custom') {
defaultExpiry = parseInt(document.getElementById('customExpiryDays').value) || 0;
} else {
defaultExpiry = parseInt(radio.value);
}
break;
}
}
config = {
accessKeyId: document.getElementById('accessKeyId').value.trim(),
accessKeySecret: document.getElementById('accessKeySecret').value.trim(),
bucket: document.getElementById('bucket').value.trim(),
region: document.getElementById('region').value.trim(),
domain: document.getElementById('domain').value.trim(),
chunkSize: parseInt(document.getElementById('chunkSize').value) || 5,
defaultExpiry: defaultExpiry // 添加默认有效期配置
};
localStorage.setItem('ossConfig', JSON.stringify(config));
if (initOSSClient()) {
showNotification('配置保存成功', 'success');
} else {
showNotification('请填写完整的配置信息', 'warning');
}
}
// 处理文件拖放
function handleDragOver(e) {
e.preventDefault();
dropArea.classList.add('border-primary', 'bg-blue-50');
}
function handleDragLeave() {
dropArea.classList.remove('border-primary', 'bg-blue-50');
}
function handleDrop(e) {
e.preventDefault();
dropArea.classList.remove('border-primary', 'bg-blue-50');
if (e.dataTransfer.files.length) {
handleFiles(e.dataTransfer.files);
}
}
// 处理文件选择
function handleFileSelect(e) {
if (e.target.files.length) {
handleFiles(e.target.files);
// 清空文件输入框的值,这样即使选择相同文件也能触发change事件
e.target.value = '';
}
}
// 处理文件上传
function handleFiles(files) {
if (!initOSSClient()) {
showNotification('请先配置并保存OSS信息', 'error');
return;
}
uploadQueue.classList.remove('hidden');
cancelAllUploadsBtn.classList.remove('hidden');
Array.from(files).forEach(file => {
const fileId = 'file-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9);
addToUploadQueue(file, fileId);
// 判断是否需要分片上传
const chunkSize = (config.chunkSize || 5) * 1024 * 1024; // 默认5MB
if (file.size > chunkSize) {
uploadLargeFile(file, fileId, chunkSize);
} else {
uploadFile(file, fileId);
}
});
}
// 添加到上传队列
function addToUploadQueue(file, fileId) {
const item = document.createElement('div');
item.id = fileId;
item.className = 'p-3 border border-gray-200 rounded-lg bg-gray-50';
item.innerHTML = `
<div class="flex justify-between items-center mb-2">
<div class="flex items-center">
<i class="fa fa-file-o text-primary mr-2"></i>
<span class="font-medium text-gray-800 truncate max-w-[200px] md:max-w-[400px]">${file.name}</span>
</div>
<div class="flex items-center">
<span class="text-sm text-gray-500 mr-2">${formatFileSize(file.size)}</span>
<button class="cancel-upload text-red-500 hover:text-red-700" data-id="${fileId}">
<i class="fa fa-times"></i>
</button>
</div>
</div>
<div class="w-full bg-gray-200 rounded-full h-2.5">
<div class="upload-progress bg-primary h-2.5 rounded-full" style="width: 0%"></div>
</div>
<div class="mt-1 flex justify-between">
<div class="text-sm text-gray-600 upload-status">准备上传...</div>
<div class="text-sm text-gray-600 upload-speed"></div>
</div>
`;
uploadItems.appendChild(item);
// 添加取消上传事件
item.querySelector('.cancel-upload').addEventListener('click', function() {
const taskId = this.getAttribute('data-id');
cancelUpload(taskId);
});
}
// 上传普通文件到OSS
async function uploadFile(file, fileId) {
const item = document.getElementById(fileId);
const progressBar = item.querySelector('.upload-progress');
const statusText = item.querySelector('.upload-status');
const speedText = item.querySelector('.upload-speed');
// 创建上传任务
const task = {
id: fileId,
file: file,
status: 'uploading',
cancel: null
};
uploadTasks.push(task);
try {
// 生成唯一文件名,避免覆盖
const fileName = generateFileName(file.name);
const ossPath = currentPath + fileName;
// 记录开始时间和已上传大小,用于计算速度
let startTime = Date.now();
let lastLoaded = 0;
// 上传文件
const result = await ossClient.put(ossPath, file, {
progress: function(p, checkpoint) {
const percent = Math.round(p * 100);
progressBar.style.width = `${percent}%`;
statusText.textContent = `上传中: ${percent}%`;
// 计算上传速度
const now = Date.now();
const duration = (now - startTime) / 1000; // 秒
if (duration > 0 && checkpoint && checkpoint.loaded !== undefined) {
const loaded = checkpoint.loaded;
const speed = (loaded - lastLoaded) / duration; // 字节/秒
speedText.textContent = `${formatFileSize(speed)}/s`;
startTime = now;
lastLoaded = loaded;
}
// 保存检查点,用于断点续传
task.checkpoint = checkpoint;
},
// 允许取消上传
abortCheckpoint: function(checkpoint) {
task.cancel = () => {
return { status: 'abort', checkpoint };
};
}
});
// 上传成功
statusText.textContent = '上传成功';
statusText.classList.add('text-secondary');
speedText.textContent = '';
// 更新任务状态
task.status = 'completed';
// 获取文件类型
const fileType = getFileType(file.type);
// 计算过期时间(如果设置了有效期)
let expiryDate = null;
if (config.defaultExpiry > 0) {
expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + config.defaultExpiry);
}
// 保存文件信息到本地存储
saveFileInfo({
name: file.name,
key: result.name,
size: file.size,
url: getFileUrl(result.name),
uploadedAt: new Date().toISOString(),
type: file.type,
fileType: fileType,
folder: currentPath,
expiryDate: expiryDate ? expiryDate.toISOString() : null
});
// 添加到上传历史
addUploadHistory({
name: file.name,
key: result.name,
size: file.size,
url: getFileUrl(result.name),
uploadedAt: new Date().toISOString(),
status: 'success',
folder: currentPath
});
// 更新文件列表
loadFileList();
// 3秒后从上传队列移除
setTimeout(() => {
if (item && item.parentNode) {
item.parentNode.removeChild(item);
// 如果队列空了,隐藏队列区域和取消按钮
if (uploadItems.children.length === 0) {
uploadQueue.classList.add('hidden');
cancelAllUploadsBtn.classList.add('hidden');
}
}
}, 3000);
} catch (e) {
statusText.textContent = `上传失败: ${e.message}`;
statusText.classList.add('text-red-500');
speedText.textContent = '';
// 更新任务状态
task.status = 'failed';
// 添加到上传历史
addUploadHistory({
name: file.name,
size: file.size,
uploadedAt: new Date().toISOString(),
status: 'error',
error: e.message,
folder: currentPath
});
console.error('上传失败:', e);
}
}
// 上传大文件(分片上传)
async function uploadLargeFile(file, fileId, chunkSize) {
const item = document.getElementById(fileId);
const progressBar = item.querySelector('.upload-progress');
const statusText = item.querySelector('.upload-status');
const speedText = item.querySelector('.upload-speed');
// 创建上传任务
const task = {
id: fileId,
file: file,
status: 'uploading',
cancel: null
};
uploadTasks.push(task);
try {
// 生成唯一文件名,避免覆盖
const fileName = generateFileName(file.name);
const ossPath = currentPath + fileName;
// 记录开始时间和已上传大小,用于计算速度
let startTime = Date.now();
let lastLoaded = 0;
// 分片上传
const result = await ossClient.multipartUpload(ossPath, file, {
parallel: 3, // 并行上传分片数
partSize: chunkSize, // 分片大小
progress: function(p, checkpoint) {
const percent = Math.round(p * 100);
progressBar.style.width = `${percent}%`;
statusText.textContent = `分片上传中: ${percent}%`;
// 计算上传速度
const now = Date.now();
const duration = (now - startTime) / 1000; // 秒
if (duration > 0 && checkpoint && checkpoint.loaded !== undefined) {
const loaded = checkpoint.loaded;
const speed = (loaded - lastLoaded) / duration; // 字节/秒
speedText.textContent = `${formatFileSize(speed)}/s`;
startTime = now;
lastLoaded = loaded;
}
// 保存检查点,用于断点续传
task.checkpoint = checkpoint;