-
Notifications
You must be signed in to change notification settings - Fork 266
/
_worker.js
981 lines (940 loc) · 36.3 KB
/
_worker.js
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
export default {
async fetch(request, env) {
const { pathname } = new URL(request.url);
const domain = env.DOMAIN;
const DATABASE = env.DATABASE;
const USERNAME = env.USERNAME;
const PASSWORD = env.PASSWORD;
const adminPath = env.ADMIN_PATH;
const enableAuth = env.ENABLE_AUTH === 'true';
const TG_BOT_TOKEN = env.TG_BOT_TOKEN;
const TG_CHAT_ID = env.TG_CHAT_ID;
switch (pathname) {
case '/':
return await handleRootRequest(request, USERNAME, PASSWORD, enableAuth);
case `/${adminPath}`:
return await handleAdminRequest(DATABASE, request, USERNAME, PASSWORD);
case '/upload':
return request.method === 'POST' ? await handleUploadRequest(request, DATABASE, enableAuth, USERNAME, PASSWORD, domain, TG_BOT_TOKEN, TG_CHAT_ID) : new Response('Method Not Allowed', { status: 405 });
case '/bing-images':
return handleBingImagesRequest();
case '/delete-images':
return handleDeleteImagesRequest(request, DATABASE);
default:
return await handleImageRequest(request, DATABASE, TG_BOT_TOKEN);
}
}
};
let isAuthenticated = false;
function authenticate(request, USERNAME, PASSWORD) {
const authHeader = request.headers.get('Authorization');
if (!authHeader) return false;
return isValidCredentials(authHeader, USERNAME, PASSWORD);
}
async function handleRootRequest(request, USERNAME, PASSWORD, enableAuth) {
const cache = caches.default;
const cacheKey = new Request(request.url);
if (enableAuth) {
if (!authenticate(request, USERNAME, PASSWORD)) {
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
}
}
const cachedResponse = await cache.match(cacheKey);
if (cachedResponse) {
return cachedResponse;
}
const response = new Response(`
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Telegraph图床-基于Workers的图床服务">
<meta name="keywords" content="Telegraph图床,Workers图床, Cloudflare, Workers,telegra.ph, 图床">
<title>Telegraph图床-基于Workers的图床服务</title>
<link rel="icon" href="https://p1.meituan.net/csc/c195ee91001e783f39f41ffffbbcbd484286.ico" type="image/x-icon">
<link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/twitter-bootstrap/4.6.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/bootstrap-fileinput/5.2.7/css/fileinput.min.css" rel="stylesheet" />
<link href="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/toastr.js/2.1.4/toastr.min.css" rel="stylesheet" />
<link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/5.15.4/css/all.min.css" type="text/css" rel="stylesheet" />
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
position: relative;
}
.background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
z-index: -1;
transition: opacity 1s ease-in-out;
opacity: 1;
}
.card {
background-color: rgba(255, 255, 255, 0.8);
border: none;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
padding: 20px;
width: 90%;
max-width: 400px;
text-align: center;
margin: 0 auto;
position: relative;
}
.uniform-height {
margin-top: 20px;
}
#viewCacheBtn {
position: absolute;
top: 10px;
right: 10px;
background: none;
border: none;
color: rgba(0, 0, 0, 0.1);
cursor: pointer;
font-size: 24px;
transition: color 0.3s ease;
}
#viewCacheBtn:hover {
color: rgba(0, 0, 0, 0.4);
}
#cacheContent {
margin-top: 20px;
max-height: 200px;
border-radius: 5px;
overflow-y: auto;
}
.cache-title {
text-align: left;
margin-bottom: 10px;
}
.cache-item {
display: block;
cursor: pointer;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
transition: background-color 0.3s ease;
text-align: left;
padding: 10px;
}
.cache-item:hover {
background-color: #e9ecef;
}
.project-link {
font-size: 14px;
text-align: center;
margin-top: 5px;
margin-bottom: 0;
}
textarea.form-control {
max-height: 200px;
overflow-y: hidden;
resize: none;
}
</style>
</head>
<body>
<div class="background" id="background"></div>
<div class="card">
<div class="title">Telegraph图床</div>
<button type="button" class="btn" id="viewCacheBtn" title="查看历史记录"><i class="fas fa-clock"></i></button>
<div class="card-body">
<form id="uploadForm" action="/upload" method="post" enctype="multipart/form-data">
<div class="file-input-container">
<input id="fileInput" name="file" type="file" class="form-control-file" data-browse-on-zone-click="true" multiple>
</div>
<div class="form-group mb-3 uniform-height" style="display: none;">
<button type="button" class="btn btn-light mr-2" id="urlBtn">URL</button>
<button type="button" class="btn btn-light mr-2" id="bbcodeBtn">BBCode</button>
<button type="button" class="btn btn-light" id="markdownBtn">Markdown</button>
</div>
<div class="form-group mb-3 uniform-height" style="display: none;">
<textarea class="form-control" id="fileLink" readonly></textarea>
</div>
<div id="cacheContent" style="display: none;"></div>
</form>
</div>
<p class="project-link">项目开源于 GitHub - <a href="https://github.com/0-RTT/telegraph" target="_blank" rel="noopener noreferrer">0-RTT/telegraph</a></p>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js" type="application/javascript"></script>
<script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/bootstrap-fileinput/5.2.7/js/fileinput.min.js" type="application/javascript"></script>
<script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/bootstrap-fileinput/5.2.7/js/locales/zh.min.js" type="application/javascript"></script>
<script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/toastr.js/2.1.4/toastr.min.js" type="application/javascript"></script>
<script>
async function fetchBingImages() {
const response = await fetch('/bing-images');
const data = await response.json();
return data.data.map(image => image.url);
}
async function setBackgroundImages() {
const images = await fetchBingImages();
const backgroundDiv = document.getElementById('background');
if (images.length > 0) {
backgroundDiv.style.backgroundImage = 'url(' + images[0] + ')';
}
let index = 0;
let currentBackgroundDiv = backgroundDiv;
setInterval(() => {
const nextIndex = (index + 1) % images.length;
const nextBackgroundDiv = document.createElement('div');
nextBackgroundDiv.className = 'background next';
nextBackgroundDiv.style.backgroundImage = 'url(' + images[nextIndex] + ')';
document.body.appendChild(nextBackgroundDiv);
nextBackgroundDiv.style.opacity = 0;
setTimeout(() => {
nextBackgroundDiv.style.opacity = 1;
}, 50);
setTimeout(() => {
document.body.removeChild(currentBackgroundDiv);
currentBackgroundDiv = nextBackgroundDiv;
index = nextIndex;
}, 1000);
}, 5000);
}
$(document).ready(function() {
let originalImageURLs = [];
let isCacheVisible = false;
initFileInput();
setBackgroundImages();
function initFileInput() {
$("#fileInput").fileinput({
theme: 'fa',
language: 'zh',
browseClass: "btn btn-primary",
removeClass: "btn btn-danger",
showUpload: false,
showPreview: false,
}).on('filebatchselected', handleFileSelection)
.on('fileclear', handleFileClear);
}
async function handleFileSelection() {
const files = $('#fileInput')[0].files;
for (let i = 0; i < files.length; i++) {
await uploadFile(files[i]);
}
}
async function uploadFile(file) {
try {
toastr.info('上传中...', '', { timeOut: 0 });
const interfaceInfo = {
acceptTypes: 'image/*,video/*',
maxFileSize: 20 * 1024 * 1024,
enableCompression: true
};
const acceptedTypes = interfaceInfo.acceptTypes.split(',');
const isAcceptedType = acceptedTypes.some(type => {
return type.includes('*') ? file.type.startsWith(type.split('/')[0]) : file.type === type;
});
if (!isAcceptedType) {
toastr.error('仅支持图片或视频格式的文件。');
return;
}
if (file.type.startsWith('video/') || file.type === 'image/gif') {
if (file.size > interfaceInfo.maxFileSize) {
toastr.error('视频或 GIF 文件必须≤20MB,上传失败。');
return;
}
const formData = new FormData($('#uploadForm')[0]);
formData.set('file', file, file.name);
const uploadResponse = await fetch('/upload', { method: 'POST', body: formData });
const responseData = await handleUploadResponse(uploadResponse);
if (responseData.error) {
toastr.error(responseData.error);
} else {
originalImageURLs.push(responseData.data);
$('#fileLink').val(originalImageURLs.join('\\n\\n'));
$('.form-group').show();
adjustTextareaHeight($('#fileLink')[0]);
toastr.success('文件上传成功!');
saveToLocalCache(responseData.data, file.name);
}
return;
}
if (interfaceInfo.enableCompression) {
toastr.info('正在压缩...', '', { timeOut: 0 });
const compressedFile = await compressImage(file);
if (compressedFile.size > interfaceInfo.maxFileSize) {
toastr.error('压缩后文件仍然超过最大限制(20MB),上传失败。');
return;
}
file = compressedFile;
} else {
if (file.size > interfaceInfo.maxFileSize) {
toastr.error('文件必须≤20MB');
return;
}
}
const formData = new FormData($('#uploadForm')[0]);
formData.set('file', file, file.name);
const uploadResponse = await fetch('/upload', { method: 'POST', body: formData });
const responseData = await handleUploadResponse(uploadResponse);
if (responseData.error) {
toastr.error(responseData.error);
} else {
originalImageURLs.push(responseData.data);
$('#fileLink').val(originalImageURLs.join('\\n\\n'));
$('.form-group').show();
adjustTextareaHeight($('#fileLink')[0]);
toastr.success('文件上传成功!');
saveToLocalCache(responseData.data, file.name);
}
} catch (error) {
console.error('处理文件时出现错误:', error);
$('#fileLink').val('文件处理失败!');
toastr.error('文件处理失败!');
} finally {
toastr.clear();
}
}
async function handleUploadResponse(response) {
if (response.ok) {
return await response.json();
} else {
const errorData = await response.json();
return { error: errorData.error };
}
}
$(document).on('paste', function(event) {
const clipboardData = event.originalEvent.clipboardData;
if (clipboardData && clipboardData.items) {
for (let i = 0; i < clipboardData.items.length; i++) {
const item = clipboardData.items[i];
if (item.kind === 'file') {
const pasteFile = item.getAsFile();
const dataTransfer = new DataTransfer();
dataTransfer.items.add(pasteFile);
$('#fileInput')[0].files = dataTransfer.files;
$('#fileInput').trigger('change');
break;
}
}
}
});
async function compressImage(file, quality = 0.5, maxResolution = 20000000) {
return new Promise((resolve) => {
const image = new Image();
image.onload = () => {
const width = image.width;
const height = image.height;
const resolution = width * height;
let scale = 1;
if (resolution > maxResolution) {
scale = Math.sqrt(maxResolution / resolution);
}
const targetWidth = Math.round(width * scale);
const targetHeight = Math.round(height * scale);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = targetWidth;
canvas.height = targetHeight;
ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
canvas.toBlob((blob) => {
const compressedFile = new File([blob], file.name, { type: 'image/jpeg' });
toastr.success('图片压缩成功!');
resolve(compressedFile);
}, 'image/jpeg', quality);
};
const reader = new FileReader();
reader.onload = (event) => {
image.src = event.target.result;
};
reader.readAsDataURL(file);
});
}
$('#urlBtn, #bbcodeBtn, #markdownBtn').on('click', function() {
const fileLinks = originalImageURLs.map(url => url.trim()).filter(url => url !== '');
if (fileLinks.length > 0) {
let formattedLinks = '';
switch ($(this).attr('id')) {
case 'urlBtn':
formattedLinks = fileLinks.join('\\n\\n');
break;
case 'bbcodeBtn':
formattedLinks = fileLinks.map(url => '[img]' + url + '[/img]').join('\\n\\n');
break;
case 'markdownBtn':
formattedLinks = fileLinks.map(url => '![image](' + url + ')').join('\\n\\n');
break;
default:
formattedLinks = fileLinks.join('\\n');
}
$('#fileLink').val(formattedLinks);
adjustTextareaHeight($('#fileLink')[0]);
copyToClipboardWithToastr(formattedLinks);
}
});
function handleFileClear(event) {
$('#fileLink').val('');
adjustTextareaHeight($('#fileLink')[0]);
hideButtonsAndTextarea();
originalImageURLs = [];
}
function adjustTextareaHeight(textarea) {
textarea.style.height = '1px';
textarea.style.height = (textarea.scrollHeight > 200 ? 200 : textarea.scrollHeight) + 'px';
if (textarea.scrollHeight > 200) {
textarea.style.overflowY = 'auto';
} else {
textarea.style.overflowY = 'hidden';
}
}
function copyToClipboardWithToastr(text) {
const input = document.createElement('textarea');
input.value = text;
document.body.appendChild(input);
input.select();
document.execCommand('copy');
document.body.removeChild(input);
toastr.success('已复制到剪贴板', '', { timeOut: 300 });
}
function hideButtonsAndTextarea() {
$('#urlBtn, #bbcodeBtn, #markdownBtn, #fileLink').parent('.form-group').hide();
}
function saveToLocalCache(url, fileName) {
const timestamp = new Date().toLocaleString('zh-CN', { hour12: false });
const cacheData = JSON.parse(localStorage.getItem('uploadCache')) || [];
cacheData.push({ url, fileName, timestamp });
localStorage.setItem('uploadCache', JSON.stringify(cacheData));
}
$('#viewCacheBtn').on('click', function() {
const cacheData = JSON.parse(localStorage.getItem('uploadCache')) || [];
const cacheContent = $('#cacheContent');
cacheContent.empty();
if (isCacheVisible) {
cacheContent.hide();
$('#fileLink').val('');
$('#fileLink').parent('.form-group').hide();
isCacheVisible = false;
} else {
if (cacheData.length > 0) {
cacheData.reverse();
cacheData.forEach((item) => {
const listItem = $('<div class="cache-item"></div>')
.text(item.timestamp + ' - ' + item.fileName)
.data('url', item.url);
cacheContent.append(listItem);
cacheContent.append('<br>');
});
cacheContent.show();
} else {
cacheContent.append('<div>还没有记录哦!</div>').show();
}
isCacheVisible = true;
}
});
$(document).on('click', '.cache-item', function() {
const url = $(this).data('url');
originalImageURLs = [];
$('#fileLink').val('');
originalImageURLs.push(url);
$('#fileLink').val(originalImageURLs.map(url => url.trim()).join('\\n\\n'));
$('.form-group').show();
adjustTextareaHeight($('#fileLink')[0]);
});
});
</script>
</body>
</html>
`, { headers: { 'Content-Type': 'text/html;charset=UTF-8' } });
await cache.put(cacheKey, response.clone());
return response;
}
async function handleAdminRequest(DATABASE, request, USERNAME, PASSWORD) {
if (!authenticate(request, USERNAME, PASSWORD)) {
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
}
return await generateAdminPage(DATABASE);
}
function isValidCredentials(authHeader, USERNAME, PASSWORD) {
const base64Credentials = authHeader.split(' ')[1];
const credentials = atob(base64Credentials).split(':');
const username = credentials[0];
const password = credentials[1];
return username === USERNAME && password === PASSWORD;
}
async function generateAdminPage(DATABASE) {
const mediaData = await fetchMediaData(DATABASE);
const mediaHtml = mediaData.map(({ url }) => {
const fileExtension = url.split('.').pop().toLowerCase();
const timestamp = url.split('/').pop().split('.')[0];
const mediaType = fileExtension === 'mp4' ? '视频' : '图片';
return `
<div class="media-container" data-key="${url}" onclick="toggleImageSelection(this)">
<div class="media-type">${mediaType}</div>
${mediaType === '视频' ? `
<video class="gallery-video" style="width: 100%; height: 100%; object-fit: contain;" data-src="${url}" controls>
<source src="" type="video/mp4">
您的浏览器不支持视频标签。
</video>
` : `
<img class="gallery-image lazy" data-src="${url}" alt="Image">
`}
<div class="upload-time">上传时间: ${new Date(parseInt(timestamp)).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })}</div>
</div>
`;
}).join('');
const html = `
<!DOCTYPE html>
<html>
<head>
<title>图库</title>
<link rel="icon" href="https://p1.meituan.net/csc/c195ee91001e783f39f41ffffbbcbd484286.ico" type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
.header {
position: sticky;
top: 0;
background-color: #ffffff;
z-index: 1000;
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding: 15px 20px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
border-radius: 8px;
flex-wrap: wrap;
}
.header-left {
flex: 1;
}
.header-right {
display: flex;
gap: 10px;
justify-content: flex-end;
flex: 1;
justify-content: flex-end;
flex-wrap: wrap;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 16px;
}
.media-container {
position: relative;
overflow: hidden;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
aspect-ratio: 1 / 1;
transition: transform 0.3s, box-shadow 0.3s;
}
.media-type {
position: absolute;
top: 10px;
left: 10px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 5px;
border-radius: 5px;
font-size: 14px;
z-index: 10;
cursor: pointer;
}
.upload-time {
position: absolute;
bottom: 10px;
left: 10px;
background-color: rgba(255, 255, 255, 0.7);
padding: 5px;
border-radius: 5px;
color: #000;
font-size: 14px;
z-index: 10;
display: none;
}
.media-container:hover {
transform: scale(1.05);
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}
.gallery-image {
width: 100%;
height: 100%;
object-fit: contain;
transition: opacity 0.3s;
opacity: 0;
}
.gallery-image.loaded {
opacity: 1;
}
.media-container.selected {
border: 2px solid #007bff;
background-color: rgba(0, 123, 255, 0.1);
}
.footer {
margin-top: 20px;
text-align: center;
font-size: 18px;
color: #555;
}
.delete-button, .copy-button {
background-color: #ff4d4d;
color: white;
border: none;
border-radius: 5px;
padding: 10px 15px;
cursor: pointer;
transition: background-color 0.3s;
width: auto;
}
.delete-button:hover, .copy-button:hover {
background-color: #ff1a1a;
}
.hidden {
display: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.dropdown-content button {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
background: none;
border: none;
width: 100%;
text-align: left;
}
.dropdown-content button:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
@media (max-width: 768px) {
.header-left, .header-right {
flex: 1 1 100%;
justify-content: flex-start;
}
.header-right {
margin-top: 10px;
}
.gallery {
grid-template-columns: repeat(2, 1fr);
}
}
</style>
<script>
let selectedCount = 0;
const selectedKeys = new Set();
let isAllSelected = false;
function toggleImageSelection(container) {
const key = container.getAttribute('data-key');
container.classList.toggle('selected');
const uploadTime = container.querySelector('.upload-time');
if (container.classList.contains('selected')) {
selectedKeys.add(key);
selectedCount++;
uploadTime.style.display = 'block';
} else {
selectedKeys.delete(key);
selectedCount--;
uploadTime.style.display = 'none';
}
updateDeleteButton();
}
function updateDeleteButton() {
const deleteButton = document.getElementById('delete-button');
const countDisplay = document.getElementById('selected-count');
countDisplay.textContent = selectedCount;
const headerRight = document.querySelector('.header-right');
if (selectedCount > 0) {
headerRight.classList.remove('hidden');
} else {
headerRight.classList.add('hidden');
}
}
async function deleteSelectedImages() {
if (selectedKeys.size === 0) return;
const confirmation = confirm('你确定要删除选中的媒体文件吗?此操作无法撤销。');
if (!confirmation) return;
const response = await fetch('/delete-images', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(Array.from(selectedKeys))
});
if (response.ok) {
alert('选中的媒体已删除');
location.reload();
} else {
alert('删除失败');
}
}
function copyFormattedLinks(format) {
const urls = Array.from(selectedKeys).map(url => url.trim()).filter(url => url !== '');
let formattedLinks = '';
switch (format) {
case 'url':
formattedLinks = urls.join('\\n\\n');
break;
case 'bbcode':
formattedLinks = urls.map(url => '[img]' + url + '[/img]').join('\\n\\n');
break;
case 'markdown':
formattedLinks = urls.map(url => '![image](' + url + ')').join('\\n\\n');
break;
}
navigator.clipboard.writeText(formattedLinks).then(() => {
alert('复制成功');
}).catch((err) => {
alert('复制失败');
});
}
function selectAllImages() {
const mediaContainers = document.querySelectorAll('.media-container');
if (isAllSelected) {
mediaContainers.forEach(container => {
container.classList.remove('selected');
const key = container.getAttribute('data-key');
selectedKeys.delete(key);
container.querySelector('.upload-time').style.display = 'none';
});
selectedCount = 0;
} else {
mediaContainers.forEach(container => {
if (!container.classList.contains('selected')) {
container.classList.add('selected');
const key = container.getAttribute('data-key');
selectedKeys.add(key);
selectedCount++;
container.querySelector('.upload-time').style.display = 'block';
}
});
}
isAllSelected = !isAllSelected;
updateDeleteButton();
}
document.addEventListener('DOMContentLoaded', () => {
const mediaContainers = document.querySelectorAll('.media-container[data-key]');
const options = {
root: null,
rootMargin: '0px',
threshold: 0.1
};
const mediaObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const container = entry.target;
const mediaType = container.querySelector('.media-type').textContent;
if (mediaType === '视频') {
const video = container.querySelector('video');
if (video && !video.src) {
video.src = video.dataset.src;
video.load();
}
} else {
const img = container.querySelector('img');
if (img && !img.src) {
img.src = img.dataset.src;
img.onload = () => img.classList.add('loaded');
}
}
observer.unobserve(container);
}
});
}, options);
mediaContainers.forEach(container => {
mediaObserver.observe(container);
});
});
</script>
</head>
<body>
<div class="header">
<div class="header-left">
<span>媒体文件 ${mediaData.length} 个</span>
<span>已选中: <span id="selected-count">0</span>个</span>
</div>
<div class="header-right hidden">
<div class="dropdown">
<button class="copy-button">复制</button>
<div class="dropdown-content">
<button onclick="copyFormattedLinks('url')">URL</button>
<button onclick="copyFormattedLinks('bbcode')">BBCode</button>
<button onclick="copyFormattedLinks('markdown')">Markdown</button>
</div>
</div>
<button id="select-all-button" class="delete-button" onclick="selectAllImages()">全选</button>
<button id="delete-button" class="delete-button" onclick="deleteSelectedImages()">删除</button>
</div>
</div>
<div class="gallery">
${mediaHtml}
</div>
<div class="footer">
到底啦
</div>
</body>
</html>
`;
return new Response(html, { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' } });
}
async function fetchMediaData(DATABASE) {
const result = await DATABASE.prepare('SELECT url, fileId FROM media').all();
const mediaData = result.results.map(row => {
const timestamp = parseInt(row.url.split('/').pop().split('.')[0]);
return { fileId: row.fileId, url: row.url, timestamp: timestamp };
});
mediaData.sort((a, b) => b.timestamp - a.timestamp);
return mediaData.map(({ fileId, url }) => ({ fileId, url }));
}
async function handleUploadRequest(request, DATABASE, enableAuth, USERNAME, PASSWORD, domain, TG_BOT_TOKEN, TG_CHAT_ID) {
try {
const formData = await request.formData();
const file = formData.get('file');
if (!file) throw new Error('缺少文件');
if (enableAuth && !authenticate(request, USERNAME, PASSWORD)) {
return new Response('Unauthorized', { status: 401, headers: { 'WWW-Authenticate': 'Basic realm="Admin"' } });
}
const uploadFormData = new FormData();
uploadFormData.append("chat_id", TG_CHAT_ID);
let fileId;
if (file.type.startsWith('image/gif')) {
const newFileName = file.name.replace(/\.gif$/, '.jpeg');
const newFile = new File([file], newFileName, { type: 'image/jpeg' });
uploadFormData.append("document", newFile);
} else {
uploadFormData.append("document", file);
}
const telegramResponse = await fetch(`https://api.telegram.org/bot${TG_BOT_TOKEN}/sendDocument`, { method: 'POST', body: uploadFormData });
if (!telegramResponse.ok) {
const errorData = await telegramResponse.json();
throw new Error(errorData.description || '上传到 Telegram 失败');
}
const responseData = await telegramResponse.json();
if (responseData.result.video) fileId = responseData.result.video.file_id;
else if (responseData.result.document) fileId = responseData.result.document.file_id;
else if (responseData.result.sticker) fileId = responseData.result.sticker.file_id;
else throw new Error('返回的数据中没有文件 ID');
const fileExtension = file.name.split('.').pop();
const timestamp = Date.now();
const imageURL = `https://${domain}/${timestamp}.${fileExtension}`;
await DATABASE.prepare('INSERT INTO media (url, fileId) VALUES (?, ?) ON CONFLICT(url) DO NOTHING').bind(imageURL, fileId).run();
return new Response(JSON.stringify({ data: imageURL }), { status: 200, headers: { 'Content-Type': 'application/json' } });
} catch (error) {
console.error('内部服务器错误:', error);
return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { 'Content-Type': 'application/json' } });
}
}
async function handleImageRequest(request, DATABASE, TG_BOT_TOKEN) {
const requestedUrl = request.url;
const cache = caches.default;
const cacheKey = new Request(requestedUrl);
const cachedResponse = await cache.match(cacheKey);
if (cachedResponse) return cachedResponse;
const result = await DATABASE.prepare('SELECT fileId FROM media WHERE url = ?').bind(requestedUrl).first();
if (!result) {
const notFoundResponse = new Response('资源不存在', { status: 404 });
await cache.put(cacheKey, notFoundResponse.clone());
return notFoundResponse;
}
const fileId = result.fileId;
let filePath;
let attempts = 0;
const maxAttempts = 3;
while (attempts < maxAttempts) {
const getFilePath = await fetch(`https://api.telegram.org/bot${TG_BOT_TOKEN}/getFile?file_id=${fileId}`);
if (!getFilePath.ok) {
return new Response('getFile请求失败', { status: 500 });
}
const fileData = await getFilePath.json();
if (fileData.ok && fileData.result.file_path) {
filePath = fileData.result.file_path;
break;
}
attempts++;
}
if (!filePath) {
const notFoundResponse = new Response('未找到FilePath', { status: 404 });
await cache.put(cacheKey, notFoundResponse.clone());
return notFoundResponse;
}
const getFileResponse = `https://api.telegram.org/file/bot${TG_BOT_TOKEN}/${filePath}`;
const response = await fetch(getFileResponse);
if (!response.ok) {
return new Response('获取文件内容失败', { status: 500 });
}
const fileExtension = requestedUrl.split('.').pop().toLowerCase();
let contentType = 'text/plain';
if (fileExtension === 'jpg' || fileExtension === 'jpeg') contentType = 'image/jpeg';
if (fileExtension === 'png') contentType = 'image/png';
if (fileExtension === 'gif') contentType = 'image/gif';
if (fileExtension === 'webp') contentType = 'image/webp';
if (fileExtension === 'mp4') contentType = 'video/mp4';
const headers = new Headers(response.headers);
headers.set('Content-Type', contentType);
headers.set('Content-Disposition', 'inline');
const responseToCache = new Response(response.body, { status: response.status, headers });
await cache.put(cacheKey, responseToCache.clone());
return responseToCache;
}
async function handleBingImagesRequest(request) {
const cache = caches.default;
const cacheKey = new Request('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=5');
const cachedResponse = await cache.match(cacheKey);
if (cachedResponse) return cachedResponse;
const res = await fetch(cacheKey);
if (!res.ok) {
return new Response('请求 Bing API 失败', { status: res.status });
}
const bingData = await res.json();
const images = bingData.images.map(image => ({ url: `https://cn.bing.com${image.url}` }));
const returnData = { status: true, message: "操作成功", data: images };
const response = new Response(JSON.stringify(returnData), { status: 200, headers: { 'Content-Type': 'application/json' } });
await cache.put(cacheKey, response.clone());
return response;
}
async function handleDeleteImagesRequest(request, DATABASE) {
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 });
}
try {
const keysToDelete = await request.json();
if (!Array.isArray(keysToDelete) || keysToDelete.length === 0) {
return new Response(JSON.stringify({ message: '没有要删除的项' }), { status: 400 });
}
const placeholders = keysToDelete.map(() => '?').join(',');
const result = await DATABASE.prepare(`DELETE FROM media WHERE url IN (${placeholders})`).bind(...keysToDelete).run();
if (result.changes === 0) {
return new Response(JSON.stringify({ message: '未找到要删除的项' }), { status: 404 });
}
const cache = caches.default;
for (const url of keysToDelete) {
const cacheKey = new Request(url);
const cachedResponse = await cache.match(cacheKey);
if (cachedResponse) {
await cache.delete(cacheKey);
}
}
return new Response(JSON.stringify({ message: '删除成功' }), { status: 200 });
} catch (error) {
return new Response(JSON.stringify({ error: '删除失败', details: error.message }), { status: 500 });
}
}