coollsd commited on
Commit
a530b8d
·
verified ·
1 Parent(s): 15353fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -157
app.py CHANGED
@@ -17,6 +17,11 @@ HTML_CONTENT = """
17
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
18
  <title>Radd PRO Uploader</title>
19
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
 
 
 
 
 
20
  <style>
21
  body {
22
  font-family: 'Poppins', sans-serif;
@@ -420,117 +425,117 @@ HTML_CONTENT = """
420
  word-break: break-all;
421
  }
422
 
423
- .history-item-actions {
424
- display: flex;
425
- gap: 5px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426
  }
427
 
428
- .quick-open-modal {
429
- display: none;
430
- position: fixed;
431
- z-index: 4;
432
- left: 0;
433
- top: 0;
434
- width: 100%;
435
- height: 100%;
436
- background-color: rgba(0,0,0,0.9);
437
- overflow: auto;
438
  }
439
 
440
- .quick-open-content {
441
- margin: 5% auto;
442
- padding: 20px;
443
- width: 90%;
444
- max-width: 800px;
445
- text-align: center;
446
  }
447
 
448
- .quick-open-content img,
449
- .quick-open-content video,
450
- .quick-open-content audio {
451
- max-width: 100%;
452
- max-height: 70vh;
453
- margin-bottom: 20px;
454
  }
455
 
456
- .quick-open-content iframe {
457
- width: 100%;
458
- height: 70vh;
459
- border: none;
460
  }
461
 
462
- @media (max-width: 480px) {
463
- .container {
464
- padding: 1.5rem;
465
- }
466
-
467
- h1 {
468
- font-size: 1.5rem;
469
- }
470
-
471
- .btn, .small-btn {
472
- font-size: 0.9rem;
473
- padding: 10px 20px;
474
- margin: 0.25rem;
475
- }
476
-
477
- .file-types {
478
- font-size: 0.7rem;
479
- }
480
-
481
- .modal-content, .history-modal-content {
482
- width: 95%;
483
- margin: 5% auto;
484
- padding: 15px;
485
- }
486
-
487
- .history-item {
488
- flex-direction: column;
489
- align-items: flex-start;
490
- }
491
 
492
- .history-item-actions {
493
- margin-top: 10px;
494
- flex-wrap: wrap;
495
- }
496
 
497
- .history-item-name {
498
- font-size: 0.9rem;
499
- }
500
 
501
- .drop-zone {
502
- padding: 15px;
503
- }
504
 
505
- .drop-zone p {
506
- font-size: 0.9rem;
507
- }
508
 
509
- .result-link {
510
- font-size: 0.9rem;
511
- }
512
 
513
- .link-buttons {
514
- flex-direction: column;
515
- align-items: stretch;
516
- }
517
 
518
- .link-buttons .small-btn {
519
- margin: 0.25rem 0;
520
- }
521
 
522
- .close {
523
- top: 5px;
524
- right: 10px;
525
- font-size: 24px;
526
- }
527
 
528
- .quick-open-content {
529
- margin: 10% auto;
530
- padding: 10px;
531
- }
532
  }
533
- </style>
 
534
  </head>
535
  <body>
536
  <div class="container">
@@ -637,26 +642,18 @@ HTML_CONTENT = """
637
  }
638
  });
639
 
640
- span[0].onclick = function() {
641
- modal.style.display = "none";
642
- }
643
-
644
- span[1].onclick = function() {
645
- historyModal.style.display = "none";
646
- }
647
-
648
- span[2].onclick = function() {
649
- quickOpenModal.style.display = "none";
650
  }
651
 
652
  window.onclick = function(event) {
653
- if (event.target == modal) {
654
  modal.style.display = "none";
655
- }
656
- if (event.target == historyModal) {
657
  historyModal.style.display = "none";
658
- }
659
- if (event.target == quickOpenModal) {
660
  quickOpenModal.style.display = "none";
661
  }
662
  }
@@ -677,10 +674,6 @@ HTML_CONTENT = """
677
  const file = e.target.files[0];
678
  fileName.textContent = file.name;
679
  uploadBtn.style.display = 'inline-block';
680
-
681
- const dataTransfer = new DataTransfer();
682
- dataTransfer.items.add(file);
683
- fileInput.files = dataTransfer.files;
684
  }
685
  }
686
 
@@ -698,44 +691,29 @@ HTML_CONTENT = """
698
  const formData = new FormData();
699
  formData.append('file', file);
700
 
701
- while (true) {
702
- try {
703
- const xhr = new XMLHttpRequest();
704
- xhr.open('POST', '/upload', true);
705
- xhr.upload.onprogress = (event) => updateProgress(event, progressBar.querySelector('.progress'));
706
-
707
- xhr.onload = function() {
708
- if (xhr.status === 200) {
709
- const response = JSON.parse(xhr.responseText);
710
- if (response.url) {
711
- addResultLink(response.url, file.name, response.originalExtension);
712
- saveToHistory(file.name, response.url, response.originalExtension);
713
- resetUploadState();
714
- return;
715
- } else {
716
- throw new Error('Upload failed: ' + response.error);
717
- }
718
- } else {
719
- throw new Error(`HTTP error! status: ${xhr.status}`);
720
- }
721
- };
722
-
723
- xhr.onerror = function() {
724
- throw new Error('Network error occurred');
725
- };
726
 
727
- xhr.send(formData);
 
 
728
 
729
- await new Promise((resolve, reject) => {
730
- xhr.onloadend = resolve;
731
- xhr.onerror = reject;
732
- });
733
 
734
- break;
735
- } catch (error) {
736
- console.error('Upload error:', error);
737
- await new Promise(resolve => setTimeout(resolve, 1000));
 
738
  }
 
 
 
 
 
739
  }
740
  }
741
 
@@ -755,19 +733,13 @@ HTML_CONTENT = """
755
  return container;
756
  }
757
 
758
- function updateProgress(event, progressBar) {
759
- if (event.lengthComputable) {
760
- const percentComplete = (event.loaded / event.total) * 100;
761
- progressBar.style.width = percentComplete + '%';
762
- }
763
- }
764
-
765
  function resetUploadState() {
766
  fileInput.value = '';
767
  fileName.textContent = '';
768
  uploadBtn.style.display = 'none';
769
  uploadBtn.disabled = false;
770
  loadingSpinner.style.display = 'none';
 
771
  }
772
 
773
  function addResultLink(url, fileName, originalExtension) {
@@ -889,7 +861,7 @@ HTML_CONTENT = """
889
  historyModal.style.display = "block";
890
  }
891
 
892
- function quickOpen(url, fileName, originalExtension) {
893
  quickOpenContent.innerHTML = '';
894
  const fullUrl = window.location.origin + url;
895
 
@@ -931,7 +903,17 @@ HTML_CONTENT = """
931
 
932
  quickOpenModal.style.display = "block";
933
  }
934
- </script>
 
 
 
 
 
 
 
 
 
 
935
  </body>
936
  </html>
937
  """
@@ -947,7 +929,7 @@ async def handle_upload(file: UploadFile = File(...)):
947
 
948
  cookies = await get_cookies()
949
  if 'csrftoken' not in cookies or 'sessionid' not in cookies:
950
- return JSONResponse(content={"error": "Failed"}, status_code=500)
951
 
952
  original_extension = os.path.splitext(file.filename)[1][1:]
953
  supported_types = ['mp4', 'png', 'jpg', 'jpeg', 'gif', 'mp3', 'pdf', 'txt']
@@ -961,12 +943,12 @@ async def handle_upload(file: UploadFile = File(...)):
961
 
962
  upload_result = await initiate_upload(cookies, temp_filename, content_type)
963
  if not upload_result or 'upload_url' not in upload_result:
964
- return JSONResponse(content={"error": "Failed to upload"}, status_code=500)
965
 
966
  file_content = await file.read()
967
  upload_success = await retry_upload(upload_result['upload_url'], file_content, content_type)
968
  if not upload_success:
969
- return JSONResponse(content={"error": "FAILED GOD MAN AFTER alot of attempts"}, status_code=500)
970
 
971
  original_url = upload_result['serving_url']
972
  mirrored_url = f"/rbxg/{original_url.split('/pbxt/')[1]}"
@@ -1087,7 +1069,7 @@ async def upload_file(upload_url: str, file_content: bytes, content_type: str) -
1087
  return False
1088
 
1089
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
1090
- while True:
1091
  try:
1092
  success = await upload_file(upload_url, file_content, content_type)
1093
  if success:
 
17
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
18
  <title>Radd PRO Uploader</title>
19
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
20
+ <link rel="manifest" href="/manifest.json">
21
+ <meta name="apple-mobile-web-app-capable" content="yes">
22
+ <meta name="apple-mobile-web-app-status-bar-style" content="black">
23
+ <meta name="apple-mobile-web-app-title" content="Radd PRO">
24
+ <link rel="apple-touch-icon" href="/icon-192x192.png">
25
  <style>
26
  body {
27
  font-family: 'Poppins', sans-serif;
 
425
  word-break: break-all;
426
  }
427
 
428
+ .history-item-actions {
429
+ display: flex;
430
+ gap: 5px;
431
+ }
432
+
433
+ .quick-open-modal {
434
+ display: none;
435
+ position: fixed;
436
+ z-index: 4;
437
+ left: 0;
438
+ top: 0;
439
+ width: 100%;
440
+ height: 100%;
441
+ background-color: rgba(0,0,0,0.9);
442
+ overflow: auto;
443
+ }
444
+
445
+ .quick-open-content {
446
+ margin: 5% auto;
447
+ padding: 20px;
448
+ width: 90%;
449
+ max-width: 800px;
450
+ text-align: center;
451
+ }
452
+
453
+ .quick-open-content img,
454
+ .quick-open-content video,
455
+ .quick-open-content audio {
456
+ max-width: 100%;
457
+ max-height: 70vh;
458
+ margin-bottom: 20px;
459
+ }
460
+
461
+ .quick-open-content iframe {
462
+ width: 100%;
463
+ height: 70vh;
464
+ border: none;
465
+ }
466
+
467
+ @media (max-width: 480px) {
468
+ .container {
469
+ padding: 1.5rem;
470
  }
471
 
472
+ h1 {
473
+ font-size: 1.5rem;
 
 
 
 
 
 
 
 
474
  }
475
 
476
+ .btn, .small-btn {
477
+ font-size: 0.9rem;
478
+ padding: 10px 20px;
479
+ margin: 0.25rem;
 
 
480
  }
481
 
482
+ .file-types {
483
+ font-size: 0.7rem;
 
 
 
 
484
  }
485
 
486
+ .modal-content, .history-modal-content {
487
+ width: 95%;
488
+ margin: 5% auto;
489
+ padding: 15px;
490
  }
491
 
492
+ .history-item {
493
+ flex-direction: column;
494
+ align-items: flex-start;
495
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
 
497
+ .history-item-actions {
498
+ margin-top: 10px;
499
+ flex-wrap: wrap;
500
+ }
501
 
502
+ .history-item-name {
503
+ font-size: 0.9rem;
504
+ }
505
 
506
+ .drop-zone {
507
+ padding: 15px;
508
+ }
509
 
510
+ .drop-zone p {
511
+ font-size: 0.9rem;
512
+ }
513
 
514
+ .result-link {
515
+ font-size: 0.9rem;
516
+ }
517
 
518
+ .link-buttons {
519
+ flex-direction: column;
520
+ align-items: stretch;
521
+ }
522
 
523
+ .link-buttons .small-btn {
524
+ margin: 0.25rem 0;
525
+ }
526
 
527
+ .close {
528
+ top: 5px;
529
+ right: 10px;
530
+ font-size: 24px;
531
+ }
532
 
533
+ .quick-open-content {
534
+ margin: 10% auto;
535
+ padding: 10px;
 
536
  }
537
+ }
538
+ </style>
539
  </head>
540
  <body>
541
  <div class="container">
 
642
  }
643
  });
644
 
645
+ for (let i = 0; i < span.length; i++) {
646
+ span[i].onclick = function() {
647
+ modal.style.display = "none";
648
+ historyModal.style.display = "none";
649
+ quickOpenModal.style.display = "none";
650
+ }
 
 
 
 
651
  }
652
 
653
  window.onclick = function(event) {
654
+ if (event.target == modal || event.target == historyModal || event.target == quickOpenModal) {
655
  modal.style.display = "none";
 
 
656
  historyModal.style.display = "none";
 
 
657
  quickOpenModal.style.display = "none";
658
  }
659
  }
 
674
  const file = e.target.files[0];
675
  fileName.textContent = file.name;
676
  uploadBtn.style.display = 'inline-block';
 
 
 
 
677
  }
678
  }
679
 
 
691
  const formData = new FormData();
692
  formData.append('file', file);
693
 
694
+ try {
695
+ const response = await fetch('/upload', {
696
+ method: 'POST',
697
+ body: formData
698
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699
 
700
+ if (!response.ok) {
701
+ throw new Error(`HTTP error! status: ${response.status}`);
702
+ }
703
 
704
+ const result = await response.json();
 
 
 
705
 
706
+ if (result.url) {
707
+ addResultLink(result.url, file.name, result.originalExtension);
708
+ saveToHistory(file.name, result.url, result.originalExtension);
709
+ } else {
710
+ throw new Error('Upload failed: ' + result.error);
711
  }
712
+ } catch (error) {
713
+ console.error('Upload error:', error);
714
+ alert('Upload failed. Please try again.');
715
+ } finally {
716
+ resetUploadState();
717
  }
718
  }
719
 
 
733
  return container;
734
  }
735
 
 
 
 
 
 
 
 
736
  function resetUploadState() {
737
  fileInput.value = '';
738
  fileName.textContent = '';
739
  uploadBtn.style.display = 'none';
740
  uploadBtn.disabled = false;
741
  loadingSpinner.style.display = 'none';
742
+ progressContainer.style.display = 'none';
743
  }
744
 
745
  function addResultLink(url, fileName, originalExtension) {
 
861
  historyModal.style.display = "block";
862
  }
863
 
864
+ function quickOpen(url, fileName, originalExtension) {
865
  quickOpenContent.innerHTML = '';
866
  const fullUrl = window.location.origin + url;
867
 
 
903
 
904
  quickOpenModal.style.display = "block";
905
  }
906
+
907
+ if ('serviceWorker' in navigator) {
908
+ window.addEventListener('load', () => {
909
+ navigator.serviceWorker.register('/sw.js').then(registration => {
910
+ console.log('ServiceWorker registration successful with scope: ', registration.scope);
911
+ }, err => {
912
+ console.log('ServiceWorker registration failed: ', err);
913
+ });
914
+ });
915
+ }
916
+ </script>
917
  </body>
918
  </html>
919
  """
 
929
 
930
  cookies = await get_cookies()
931
  if 'csrftoken' not in cookies or 'sessionid' not in cookies:
932
+ return JSONResponse(content={"error": "Failed to obtain necessary cookies."}, status_code=500)
933
 
934
  original_extension = os.path.splitext(file.filename)[1][1:]
935
  supported_types = ['mp4', 'png', 'jpg', 'jpeg', 'gif', 'mp3', 'pdf', 'txt']
 
943
 
944
  upload_result = await initiate_upload(cookies, temp_filename, content_type)
945
  if not upload_result or 'upload_url' not in upload_result:
946
+ return JSONResponse(content={"error": "Failed to initiate upload."}, status_code=500)
947
 
948
  file_content = await file.read()
949
  upload_success = await retry_upload(upload_result['upload_url'], file_content, content_type)
950
  if not upload_success:
951
+ return JSONResponse(content={"error": "Failed to upload after multiple attempts."}, status_code=500)
952
 
953
  original_url = upload_result['serving_url']
954
  mirrored_url = f"/rbxg/{original_url.split('/pbxt/')[1]}"
 
1069
  return False
1070
 
1071
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
1072
+ for _ in range(max_retries):
1073
  try:
1074
  success = await upload_file(upload_url, file_content, content_type)
1075
  if success: