coollsd commited on
Commit
514c417
·
verified ·
1 Parent(s): 1de1a5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -1,10 +1,11 @@
1
  from fastapi import FastAPI, File, UploadFile, Request
2
- from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
3
  import requests
4
  import time
5
  import asyncio
6
  from typing import Dict
7
  import os
 
8
 
9
  app = FastAPI()
10
 
@@ -424,7 +425,7 @@ HTML_CONTENT = """
424
  gap: 5px;
425
  }
426
 
427
- .quick-open-modal {
428
  display: none;
429
  position: fixed;
430
  z-index: 4;
@@ -774,11 +775,11 @@ HTML_CONTENT = """
774
  linkContainer.style.marginBottom = '10px';
775
 
776
  const link = document.createElement('a');
777
- link.href = url;
778
  link.textContent = `View ${fileName}`;
779
  link.className = 'result-link';
780
  link.target = '_blank';
781
- link.download = `${fileName.split('.')[0]}.${originalExtension}`;
782
 
783
  linkContainer.appendChild(link);
784
 
@@ -789,7 +790,7 @@ HTML_CONTENT = """
789
  copyBtn.textContent = 'Copy Link';
790
  copyBtn.className = 'small-btn copy-btn';
791
  copyBtn.onclick = () => {
792
- navigator.clipboard.writeText(window.location.origin + url).then(() => {
793
  alert('Link copied to clipboard!');
794
  });
795
  };
@@ -800,7 +801,7 @@ HTML_CONTENT = """
800
  embedBtn.textContent = 'Embed Video for Discord';
801
  embedBtn.className = 'small-btn embed-btn';
802
  embedBtn.onclick = () => {
803
- showEmbedModal(url);
804
  };
805
  buttonsContainer.appendChild(embedBtn);
806
  }
@@ -825,7 +826,7 @@ HTML_CONTENT = """
825
 
826
  function saveToHistory(fileName, url, originalExtension) {
827
  let history = JSON.parse(localStorage.getItem('uploadHistory')) || [];
828
- history.unshift({ fileName, url, originalExtension, timestamp: new Date().toISOString() });
829
  if (history.length > 500) history = history.slice(0, 500);
830
  localStorage.setItem('uploadHistory', JSON.stringify(history));
831
  }
@@ -924,7 +925,7 @@ HTML_CONTENT = """
924
  link.href = fullUrl;
925
  link.textContent = 'Download ' + fileName;
926
  link.target = '_blank';
927
- link.download = `${fileName.split('.')[0]}.${originalExtension}`;
928
  quickOpenContent.appendChild(link);
929
  }
930
 
@@ -949,14 +950,21 @@ async def handle_upload(file: UploadFile = File(...)):
949
  return JSONResponse(content={"error": "Failed"}, status_code=500)
950
 
951
  original_extension = os.path.splitext(file.filename)[1][1:]
952
- temp_filename = f"{file.filename}.png"
953
-
954
- upload_result = await initiate_upload(cookies, temp_filename, "image/png")
 
 
 
 
 
 
 
955
  if not upload_result or 'upload_url' not in upload_result:
956
  return JSONResponse(content={"error": "Failed to upload"}, status_code=500)
957
 
958
  file_content = await file.read()
959
- upload_success = await retry_upload(upload_result['upload_url'], file_content, "image/png")
960
  if not upload_success:
961
  return JSONResponse(content={"error": "FAILED GOD MAN AFTER alot of attempts"}, status_code=500)
962
 
@@ -984,6 +992,13 @@ async def handle_file_stream(path: str, request: Request):
984
  if response.status_code == 206:
985
  headers['Content-Range'] = response.headers.get('Content-Range')
986
 
 
 
 
 
 
 
 
987
  return StreamingResponse(generate(), status_code=response.status_code, headers=headers)
988
 
989
  @app.get("/embed")
 
1
  from fastapi import FastAPI, File, UploadFile, Request
2
+ from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse, FileResponse
3
  import requests
4
  import time
5
  import asyncio
6
  from typing import Dict
7
  import os
8
+ import mimetypes
9
 
10
  app = FastAPI()
11
 
 
425
  gap: 5px;
426
  }
427
 
428
+ .quick-open-modal {
429
  display: none;
430
  position: fixed;
431
  z-index: 4;
 
775
  linkContainer.style.marginBottom = '10px';
776
 
777
  const link = document.createElement('a');
778
+ link.href = url.replace('.png', '');
779
  link.textContent = `View ${fileName}`;
780
  link.className = 'result-link';
781
  link.target = '_blank';
782
+ link.download = fileName;
783
 
784
  linkContainer.appendChild(link);
785
 
 
790
  copyBtn.textContent = 'Copy Link';
791
  copyBtn.className = 'small-btn copy-btn';
792
  copyBtn.onclick = () => {
793
+ navigator.clipboard.writeText(window.location.origin + url.replace('.png', '')).then(() => {
794
  alert('Link copied to clipboard!');
795
  });
796
  };
 
801
  embedBtn.textContent = 'Embed Video for Discord';
802
  embedBtn.className = 'small-btn embed-btn';
803
  embedBtn.onclick = () => {
804
+ showEmbedModal(url.replace('.png', ''));
805
  };
806
  buttonsContainer.appendChild(embedBtn);
807
  }
 
826
 
827
  function saveToHistory(fileName, url, originalExtension) {
828
  let history = JSON.parse(localStorage.getItem('uploadHistory')) || [];
829
+ history.unshift({ fileName, url: url.replace('.png', ''), originalExtension, timestamp: new Date().toISOString() });
830
  if (history.length > 500) history = history.slice(0, 500);
831
  localStorage.setItem('uploadHistory', JSON.stringify(history));
832
  }
 
925
  link.href = fullUrl;
926
  link.textContent = 'Download ' + fileName;
927
  link.target = '_blank';
928
+ link.download = fileName;
929
  quickOpenContent.appendChild(link);
930
  }
931
 
 
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', 'zip', 'pdf', 'txt']
954
+
955
+ if original_extension.lower() in supported_types:
956
+ temp_filename = file.filename
957
+ content_type = file.content_type
958
+ else:
959
+ temp_filename = f"{file.filename}.png"
960
+ content_type = "image/png"
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
 
 
992
  if response.status_code == 206:
993
  headers['Content-Range'] = response.headers.get('Content-Range')
994
 
995
+ # Remove .png extension if present
996
+ if path.lower().endswith('.png'):
997
+ original_filename = os.path.splitext(path)[0]
998
+ content_type, _ = mimetypes.guess_type(original_filename)
999
+ if content_type:
1000
+ headers['Content-Type'] = content_type
1001
+
1002
  return StreamingResponse(generate(), status_code=response.status_code, headers=headers)
1003
 
1004
  @app.get("/embed")