coollsd commited on
Commit
deec5f1
·
verified ·
1 Parent(s): dde5c18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -36
app.py CHANGED
@@ -425,7 +425,7 @@ HTML_CONTENT = """
425
  gap: 5px;
426
  }
427
 
428
- .quick-open-modal {
429
  display: none;
430
  position: fixed;
431
  z-index: 4;
@@ -445,7 +445,7 @@ HTML_CONTENT = """
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%;
@@ -537,7 +537,7 @@ HTML_CONTENT = """
537
  <h1>Radd PRO Uploader</h1>
538
  <form id="uploadForm">
539
  <div id="dropZone" class="drop-zone">
540
- <input type="file" name="file" id="file" class="file-input" accept="*" multiple required>
541
  <label for="file" class="btn">Choose Files</label>
542
  <p>or drag and drop files here/paste image</p>
543
  </div>
@@ -602,6 +602,8 @@ HTML_CONTENT = """
602
  const historyList = document.getElementById('historyList');
603
  const quickOpenContent = document.getElementById('quickOpenContent');
604
 
 
 
605
  fileInput.addEventListener('change', handleFileSelect);
606
 
607
  uploadForm.addEventListener('submit', (e) => {
@@ -628,15 +630,27 @@ HTML_CONTENT = """
628
 
629
  document.addEventListener('paste', (e) => {
630
  const items = e.clipboardData.items;
 
631
  for (let i = 0; i < items.length; i++) {
632
  if (items[i].kind === 'file') {
633
  const file = items[i].getAsFile();
634
- handleFileSelect({ target: { files: [file] } });
635
- break;
 
636
  }
637
  }
 
 
 
638
  });
639
 
 
 
 
 
 
 
 
640
  span[0].onclick = function() {
641
  modal.style.display = "none";
642
  }
@@ -674,12 +688,8 @@ HTML_CONTENT = """
674
 
675
  function handleFileSelect(e) {
676
  if (e.target.files && e.target.files.length > 0) {
677
- const files = e.target.files;
678
- let fileNames = [];
679
- for (let i = 0; i < files.length; i++) {
680
- fileNames.push(files[i].name);
681
- }
682
- fileName.textContent = fileNames.join(', ');
683
  uploadBtn.style.display = 'inline-block';
684
  }
685
  }
@@ -692,37 +702,50 @@ HTML_CONTENT = """
692
  resultContainer.innerHTML = '';
693
  resultContainer.style.display = 'none';
694
 
695
- for (let i = 0; i < files.length; i++) {
696
- const file = files[i];
697
  const progressBar = createProgressBar(file.name);
698
  progressContainer.appendChild(progressBar);
699
 
700
  const formData = new FormData();
701
  formData.append('file', file);
702
 
703
- try {
704
- const response = await fetch('/upload', {
705
- method: 'POST',
706
- body: formData
707
- });
708
-
709
- if (response.ok) {
710
- const result = await response.json();
711
- if (result.url) {
712
- addResultLink(result.url, file.name, result.originalExtension);
713
- saveToHistory(file.name, result.url, result.originalExtension);
714
- } else {
715
- throw new Error('Upload failed: ' + result.error);
716
- }
717
- } else {
718
- throw new Error(`HTTP error! status: ${response.status}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719
  }
720
- } catch (error) {
721
- console.error('Upload error:', error);
722
- alert(`Failed to upload ${file.name}: ${error.message}`);
723
  }
724
-
725
- updateProgress({ loaded: 1, total: 1 }, progressBar.querySelector('.progress'));
726
  }
727
 
728
  resetUploadState();
@@ -847,7 +870,7 @@ HTML_CONTENT = """
847
  copyBtn.textContent = 'Copy Link';
848
  copyBtn.className = 'small-btn';
849
  copyBtn.onclick = () => {
850
- navigator.clipboard.writeText(window.location.origin + item.url).then(() => {
851
  alert('Link copied to clipboard!');
852
  });
853
  };
@@ -887,7 +910,7 @@ HTML_CONTENT = """
887
  img.src = fullUrl;
888
  img.alt = fileName;
889
  quickOpenContent.appendChild(img);
890
- } else if (originalExtension.toLowerCase() === 'mp4') {
891
  const video = document.createElement('video');
892
  video.src = fullUrl;
893
  video.controls = true;
 
425
  gap: 5px;
426
  }
427
 
428
+ .quick-open-modal {
429
  display: none;
430
  position: fixed;
431
  z-index: 4;
 
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%;
 
537
  <h1>Radd PRO Uploader</h1>
538
  <form id="uploadForm">
539
  <div id="dropZone" class="drop-zone">
540
+ <input type="file" name="file" id="file" class="file-input" accept="*" required multiple>
541
  <label for="file" class="btn">Choose Files</label>
542
  <p>or drag and drop files here/paste image</p>
543
  </div>
 
602
  const historyList = document.getElementById('historyList');
603
  const quickOpenContent = document.getElementById('quickOpenContent');
604
 
605
+ let pastedImageCounter = 0;
606
+
607
  fileInput.addEventListener('change', handleFileSelect);
608
 
609
  uploadForm.addEventListener('submit', (e) => {
 
630
 
631
  document.addEventListener('paste', (e) => {
632
  const items = e.clipboardData.items;
633
+ const files = [];
634
  for (let i = 0; i < items.length; i++) {
635
  if (items[i].kind === 'file') {
636
  const file = items[i].getAsFile();
637
+ const newFileName = generateUniqueFileName(file.name);
638
+ const renamedFile = new File([file], newFileName, { type: file.type });
639
+ files.push(renamedFile);
640
  }
641
  }
642
+ if (files.length > 0) {
643
+ handleFileSelect({ target: { files: files } });
644
+ }
645
  });
646
 
647
+ function generateUniqueFileName(originalName) {
648
+ const extension = originalName.split('.').pop();
649
+ const baseName = originalName.split('.').slice(0, -1).join('.');
650
+ pastedImageCounter++;
651
+ return `${baseName}${pastedImageCounter}.${extension}`;
652
+ }
653
+
654
  span[0].onclick = function() {
655
  modal.style.display = "none";
656
  }
 
688
 
689
  function handleFileSelect(e) {
690
  if (e.target.files && e.target.files.length > 0) {
691
+ const fileNames = Array.from(e.target.files).map(file => file.name).join(', ');
692
+ fileName.textContent = fileNames;
 
 
 
 
693
  uploadBtn.style.display = 'inline-block';
694
  }
695
  }
 
702
  resultContainer.innerHTML = '';
703
  resultContainer.style.display = 'none';
704
 
705
+ for (let file of files) {
 
706
  const progressBar = createProgressBar(file.name);
707
  progressContainer.appendChild(progressBar);
708
 
709
  const formData = new FormData();
710
  formData.append('file', file);
711
 
712
+ while (true) {
713
+ try {
714
+ const xhr = new XMLHttpRequest();
715
+ xhr.open('POST', '/upload', true);
716
+ xhr.upload.onprogress = (event) => updateProgress(event, progressBar.querySelector('.progress'));
717
+
718
+ xhr.onload = function() {
719
+ if (xhr.status === 200) {
720
+ const response = JSON.parse(xhr.responseText);
721
+ if (response.url) {
722
+ addResultLink(response.url, file.name, response.originalExtension);
723
+ saveToHistory(file.name, response.url, response.originalExtension);
724
+ } else {
725
+ throw new Error('Upload failed: ' + response.error);
726
+ }
727
+ } else {
728
+ throw new Error(`HTTP error! status: ${xhr.status}`);
729
+ }
730
+ };
731
+
732
+ xhr.onerror = function() {
733
+ throw new Error('Network error occurred');
734
+ };
735
+
736
+ xhr.send(formData);
737
+
738
+ await new Promise((resolve, reject) => {
739
+ xhr.onloadend = resolve;
740
+ xhr.onerror = reject;
741
+ });
742
+
743
+ break;
744
+ } catch (error) {
745
+ console.error('Upload error:', error);
746
+ await new Promise(resolve => setTimeout(resolve, 1000));
747
  }
 
 
 
748
  }
 
 
749
  }
750
 
751
  resetUploadState();
 
870
  copyBtn.textContent = 'Copy Link';
871
  copyBtn.className = 'small-btn';
872
  copyBtn.onclick = () => {
873
+ navigator.clipboard.writeText(window.location.origin + item.url).then(() => {
874
  alert('Link copied to clipboard!');
875
  });
876
  };
 
910
  img.src = fullUrl;
911
  img.alt = fileName;
912
  quickOpenContent.appendChild(img);
913
+ } else if (originalExtension.toLowerCase() === 'mp4') {
914
  const video = document.createElement('video');
915
  video.src = fullUrl;
916
  video.controls = true;