coollsd commited on
Commit
8b7ed24
1 Parent(s): a7d7c12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -60
app.py CHANGED
@@ -1,23 +1,12 @@
1
- from fastapi import FastAPI, Request, HTTPException
2
- from fastapi.responses import JSONResponse, StreamingResponse
3
  import requests
4
  import time
5
  import asyncio
6
  from typing import Dict
7
- import base64
8
- import secrets
9
- from cryptography.fernet import Fernet
10
- from starlette.middleware.sessions import SessionMiddleware
11
 
12
  app = FastAPI()
13
 
14
- # Add SessionMiddleware to the application
15
- app.add_middleware(SessionMiddleware, secret_key=secrets.token_urlsafe(32))
16
-
17
- # Generate a secret key for Fernet encryption
18
- FERNET_KEY = Fernet.generate_key()
19
- fernet = Fernet(FERNET_KEY)
20
-
21
  HTML_CONTENT = """
22
  <!DOCTYPE html>
23
  <html lang="en">
@@ -447,7 +436,7 @@ HTML_CONTENT = """
447
  align-items: flex-start;
448
  }
449
 
450
- .history-item-actions {
451
  margin-top: 10px;
452
  flex-wrap: wrap;
453
  }
@@ -456,7 +445,7 @@ HTML_CONTENT = """
456
  font-size: 0.9rem;
457
  }
458
 
459
- .drop-zone {
460
  padding: 15px;
461
  }
462
 
@@ -800,28 +789,11 @@ HTML_CONTENT = """
800
  actionsContainer.appendChild(embedBtn);
801
  }
802
 
803
- const encryptBtn = document.createElement('button');
804
- encryptBtn.textContent = 'Encrypt';
805
- encryptBtn.className = 'small-btn';
806
- encryptBtn.onclick = () => {
807
- const encryptedUrl = encryptUrl(item.url);
808
- navigator.clipboard.writeText(window.location.origin + '/encrypted/' + encryptedUrl).then(() => {
809
- alert('Encrypted link copied to clipboard!');
810
- });
811
- };
812
- actionsContainer.appendChild(encryptBtn);
813
-
814
  historyItem.appendChild(actionsContainer);
815
  historyList.appendChild(historyItem);
816
  });
817
  historyModal.style.display = "block";
818
  }
819
-
820
- function encryptUrl(url) {
821
- const key = 'your-secret-key'; // Replace with a secure key
822
- const encodedUrl = btoa(url);
823
- return encodedUrl.split('').reverse().join('');
824
- }
825
  </script>
826
  </body>
827
  </html>
@@ -875,30 +847,46 @@ async def handle_video_stream(path: str, request: Request):
875
 
876
  return StreamingResponse(generate(), status_code=response.status_code, headers=headers)
877
 
878
- @app.get("/encrypt")
879
- async def encrypt_url(request: Request, url: str):
880
- session = request.session
881
- session_id = secrets.token_urlsafe(16)
882
- session[session_id] = url
883
- encrypted_url = fernet.encrypt(f"{session_id}:{url}".encode()).decode()
884
- return JSONResponse(content={"encrypted_url": f"/encrypted/{encrypted_url}"})
885
-
886
- @app.get("/encrypted/{encrypted_url}")
887
- async def handle_encrypted_url(encrypted_url: str, request: Request):
888
- try:
889
- decrypted = fernet.decrypt(encrypted_url.encode()).decode()
890
- session_id, original_url = decrypted.split(':', 1)
891
-
892
- if session_id not in request.session or request.session[session_id] != original_url:
893
- raise HTTPException(status_code=403, detail="Invalid or expired encrypted URL")
894
-
895
- if '/pbxt/' not in original_url:
896
- raise HTTPException(status_code=400, detail="Invalid URL format")
897
-
898
- path = original_url.split('/pbxt/')[1]
899
- return await handle_video_stream(path, request)
900
- except Exception as e:
901
- raise HTTPException(status_code=400, detail="Invalid encrypted URL")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
902
 
903
  async def get_cookies() -> Dict[str, str]:
904
  try:
@@ -954,7 +942,3 @@ async def retry_upload(upload_url: str, file_content: bytes, content_type: str,
954
  delay = min(delay * 2, 60) # Exponential backoff, capped at 60 seconds
955
 
956
  return False
957
-
958
- if __name__ == "__main__":
959
- import uvicorn
960
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
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
 
8
  app = FastAPI()
9
 
 
 
 
 
 
 
 
10
  HTML_CONTENT = """
11
  <!DOCTYPE html>
12
  <html lang="en">
 
436
  align-items: flex-start;
437
  }
438
 
439
+ .history-item-actions {
440
  margin-top: 10px;
441
  flex-wrap: wrap;
442
  }
 
445
  font-size: 0.9rem;
446
  }
447
 
448
+ .drop-zone {
449
  padding: 15px;
450
  }
451
 
 
789
  actionsContainer.appendChild(embedBtn);
790
  }
791
 
 
 
 
 
 
 
 
 
 
 
 
792
  historyItem.appendChild(actionsContainer);
793
  historyList.appendChild(historyItem);
794
  });
795
  historyModal.style.display = "block";
796
  }
 
 
 
 
 
 
797
  </script>
798
  </body>
799
  </html>
 
847
 
848
  return StreamingResponse(generate(), status_code=response.status_code, headers=headers)
849
 
850
+ @app.get("/embed")
851
+ async def embed_video(url: str, thumbnail: str):
852
+ html = f'''
853
+ <html>
854
+ <head>
855
+ <meta property="og:type" content="video.other">
856
+ <meta property="og:video" content="{url}">
857
+ <meta property="og:video:url" content="{url}">
858
+ <meta property="og:video:secure_url" content="{url}">
859
+ <meta property="og:video:type" content="video/mp4">
860
+ <meta property="og:video:width" content="1280">
861
+ <meta property="og:video:height" content="720">
862
+ <meta property="og:image" content="{thumbnail}">
863
+ <meta property="og:image:secure_url" content="{thumbnail}">
864
+ <meta property="og:image:width" content="1280">
865
+ <meta property="og:image:height" content="720">
866
+ <meta property="og:image:type" content="image/png">
867
+ <style>
868
+ body, html {{ margin: 0; padding: 0; height: 100%; background: #000; }}
869
+ #thumbnail {{ width: 100%; height: 100%; object-fit: contain; cursor: pointer; }}
870
+ #video {{ display: none; width: 100%; height: 100%; object-fit: contain; }}
871
+ </style>
872
+ </head>
873
+ <body>
874
+ <img id="thumbnail" src="{thumbnail}" onclick="playVideo()">
875
+ <video id="video" controls autoplay>
876
+ <source src="{url}" type="video/mp4">
877
+ Your browser does not support the video tag.
878
+ </video>
879
+ <script>
880
+ function playVideo() {{
881
+ document.getElementById('thumbnail').style.display = 'none';
882
+ document.getElementById('video').style.display = 'block';
883
+ document.getElementById('video').play();
884
+ }}
885
+ </script>
886
+ </body>
887
+ </html>
888
+ '''
889
+ return HTMLResponse(content=html)
890
 
891
  async def get_cookies() -> Dict[str, str]:
892
  try:
 
942
  delay = min(delay * 2, 60) # Exponential backoff, capped at 60 seconds
943
 
944
  return False