Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -950,27 +950,40 @@ async def handle_upload(file: UploadFile = File(...)):
|
|
950 |
return JSONResponse(content={"error": "Failed"}, status_code=500)
|
951 |
|
952 |
original_extension = os.path.splitext(file.filename)[1][1:]
|
953 |
-
|
954 |
-
|
955 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
956 |
if not upload_result or 'upload_url' not in upload_result:
|
957 |
return JSONResponse(content={"error": "Failed to upload"}, status_code=500)
|
958 |
|
959 |
file_content = await file.read()
|
960 |
-
upload_success = await retry_upload(upload_result['upload_url'], file_content,
|
961 |
if not upload_success:
|
962 |
return JSONResponse(content={"error": "FAILED GOD MAN AFTER alot of attempts"}, status_code=500)
|
963 |
|
964 |
original_url = upload_result['serving_url']
|
965 |
mirrored_url = f"/rbxg/{original_url.split('/pbxt/')[1]}"
|
|
|
|
|
|
|
966 |
|
967 |
return JSONResponse(content={"url": mirrored_url, "originalExtension": original_extension})
|
968 |
|
969 |
@app.get("/rbxg/{path:path}")
|
970 |
async def handle_file_stream(path: str, request: Request):
|
971 |
original_url = f'https://replicate.delivery/pbxt/{path}'
|
|
|
|
|
|
|
|
|
972 |
range_header = request.headers.get('Range')
|
973 |
-
|
974 |
headers = {'Range': range_header} if range_header else {}
|
975 |
response = requests.get(original_url, headers=headers, stream=True)
|
976 |
|
@@ -985,7 +998,6 @@ async def handle_file_stream(path: str, request: Request):
|
|
985 |
if response.status_code == 206:
|
986 |
headers['Content-Range'] = response.headers.get('Content-Range')
|
987 |
|
988 |
-
# Set the correct Content-Type based on the original file extension
|
989 |
original_extension = os.path.splitext(path)[1][1:]
|
990 |
content_type, _ = mimetypes.guess_type(f"file.{original_extension}")
|
991 |
if content_type:
|
|
|
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']
|
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 |
|
971 |
original_url = upload_result['serving_url']
|
972 |
mirrored_url = f"/rbxg/{original_url.split('/pbxt/')[1]}"
|
973 |
+
|
974 |
+
if original_extension.lower() not in supported_types:
|
975 |
+
mirrored_url = mirrored_url.replace('.png', '')
|
976 |
|
977 |
return JSONResponse(content={"url": mirrored_url, "originalExtension": original_extension})
|
978 |
|
979 |
@app.get("/rbxg/{path:path}")
|
980 |
async def handle_file_stream(path: str, request: Request):
|
981 |
original_url = f'https://replicate.delivery/pbxt/{path}'
|
982 |
+
|
983 |
+
if not path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.mp4', '.mp3', '.pdf', '.txt')):
|
984 |
+
original_url += '.png'
|
985 |
+
|
986 |
range_header = request.headers.get('Range')
|
|
|
987 |
headers = {'Range': range_header} if range_header else {}
|
988 |
response = requests.get(original_url, headers=headers, stream=True)
|
989 |
|
|
|
998 |
if response.status_code == 206:
|
999 |
headers['Content-Range'] = response.headers.get('Content-Range')
|
1000 |
|
|
|
1001 |
original_extension = os.path.splitext(path)[1][1:]
|
1002 |
content_type, _ = mimetypes.guess_type(f"file.{original_extension}")
|
1003 |
if content_type:
|