coollsd commited on
Commit
1f26f5f
·
verified ·
1 Parent(s): 6b98612

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -60
app.py CHANGED
@@ -6,6 +6,7 @@ import asyncio
6
  from typing import Dict
7
  import os
8
  import mimetypes
 
9
 
10
  app = FastAPI()
11
 
@@ -634,7 +635,7 @@ HTML_CONTENT = """
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, pastedFiles);
638
  const renamedFile = new File([file], newFileName, { type: file.type });
639
  pastedFiles.push(renamedFile);
640
  }
@@ -645,20 +646,6 @@ HTML_CONTENT = """
645
  }
646
  });
647
 
648
- function generateUniqueFileName(originalName, fileList) {
649
- const extension = originalName.split('.').pop();
650
- const baseName = originalName.split('.').slice(0, -1).join('.');
651
- let counter = 1;
652
- let newName = originalName;
653
-
654
- while (fileList.some(file => file.name === newName)) {
655
- newName = `${baseName}${counter}.${extension}`;
656
- counter++;
657
- }
658
-
659
- return newName;
660
- }
661
-
662
  function updateFileNameDisplay() {
663
  const fileNames = pastedFiles.map(file => file.name).join(', ');
664
  fileName.textContent = fileNames;
@@ -723,42 +710,26 @@ HTML_CONTENT = """
723
  const formData = new FormData();
724
  formData.append('file', file);
725
 
726
- while (true) {
727
- try {
728
- const xhr = new XMLHttpRequest();
729
- xhr.open('POST', '/upload', true);
730
- xhr.upload.onprogress = (event) => updateProgress(event, progressBar.querySelector('.progress'));
731
-
732
- xhr.onload = function() {
733
- if (xhr.status === 200) {
734
- const response = JSON.parse(xhr.responseText);
735
- if (response.url) {
736
- addResultLink(response.url, file.name, response.originalExtension);
737
- saveToHistory(file.name, response.url, response.originalExtension);
738
- } else {
739
- throw new Error('Upload failed: ' + response.error);
740
- }
741
- } else {
742
- throw new Error(`HTTP error! status: ${xhr.status}`);
743
- }
744
- };
745
-
746
- xhr.onerror = function() {
747
- throw new Error('Network error occurred');
748
- };
749
-
750
- xhr.send(formData);
751
-
752
- await new Promise((resolve, reject) => {
753
- xhr.onloadend = resolve;
754
- xhr.onerror = reject;
755
- });
756
-
757
- break;
758
- } catch (error) {
759
- console.error('Upload error:', error);
760
- await new Promise(resolve => setTimeout(resolve, 1000));
761
  }
 
 
 
762
  }
763
  }
764
 
@@ -781,13 +752,6 @@ HTML_CONTENT = """
781
  return container;
782
  }
783
 
784
- function updateProgress(event, progressBar) {
785
- if (event.lengthComputable) {
786
- const percentComplete = (event.loaded / event.total) * 100;
787
- progressBar.style.width = percentComplete + '%';
788
- }
789
- }
790
-
791
  function resetUploadState() {
792
  fileInput.value = '';
793
  fileName.textContent = '';
@@ -873,7 +837,7 @@ HTML_CONTENT = """
873
  const historyItem = document.createElement('div');
874
  historyItem.className = 'history-item';
875
 
876
- const itemName = document.createElement('span');
877
  itemName.className = 'history-item-name';
878
  itemName.textContent = item.fileName;
879
  historyItem.appendChild(itemName);
@@ -899,7 +863,7 @@ HTML_CONTENT = """
899
  };
900
  actionsContainer.appendChild(openBtn);
901
 
902
- if (item.originalExtension.toLowerCase() === 'mp4') {
903
  const embedBtn = document.createElement('button');
904
  embedBtn.textContent = 'Embed';
905
  embedBtn.className = 'small-btn';
@@ -956,7 +920,7 @@ HTML_CONTENT = """
956
  quickOpenContent.appendChild(link);
957
  }
958
 
959
- quickOpenModal.style.display = "block";
960
  }
961
  </script>
962
  </body>
 
6
  from typing import Dict
7
  import os
8
  import mimetypes
9
+ import uuid
10
 
11
  app = FastAPI()
12
 
 
635
  for (let i = 0; i < items.length; i++) {
636
  if (items[i].kind === 'file') {
637
  const file = items[i].getAsFile();
638
+ const newFileName = `pasted_image_${Date.now()}.png`;
639
  const renamedFile = new File([file], newFileName, { type: file.type });
640
  pastedFiles.push(renamedFile);
641
  }
 
646
  }
647
  });
648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649
  function updateFileNameDisplay() {
650
  const fileNames = pastedFiles.map(file => file.name).join(', ');
651
  fileName.textContent = fileNames;
 
710
  const formData = new FormData();
711
  formData.append('file', file);
712
 
713
+ try {
714
+ const response = await fetch('/upload', {
715
+ method: 'POST',
716
+ body: formData
717
+ });
718
+
719
+ if (response.ok) {
720
+ const result = await response.json();
721
+ if (result.url) {
722
+ addResultLink(result.url, file.name, result.originalExtension);
723
+ saveToHistory(file.name, result.url, result.originalExtension);
724
+ } else {
725
+ throw new Error('Upload failed: ' + result.error);
726
+ }
727
+ } else {
728
+ throw new Error(`HTTP error! status: ${response.status}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
729
  }
730
+ } catch (error) {
731
+ console.error('Upload error:', error);
732
+ alert(`Failed to upload ${file.name}: ${error.message}`);
733
  }
734
  }
735
 
 
752
  return container;
753
  }
754
 
 
 
 
 
 
 
 
755
  function resetUploadState() {
756
  fileInput.value = '';
757
  fileName.textContent = '';
 
837
  const historyItem = document.createElement('div');
838
  historyItem.className = 'history-item';
839
 
840
+ const itemName = document.createElement('span');
841
  itemName.className = 'history-item-name';
842
  itemName.textContent = item.fileName;
843
  historyItem.appendChild(itemName);
 
863
  };
864
  actionsContainer.appendChild(openBtn);
865
 
866
+ if (item.originalExtension.toLowerCase() === 'mp4') {
867
  const embedBtn = document.createElement('button');
868
  embedBtn.textContent = 'Embed';
869
  embedBtn.className = 'small-btn';
 
920
  quickOpenContent.appendChild(link);
921
  }
922
 
923
+ quickOpenModal.style.display = "block";
924
  }
925
  </script>
926
  </body>