Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
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
|
@@ -7,6 +8,14 @@ from typing import Dict
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
HTML_CONTENT = """
|
11 |
<!DOCTYPE html>
|
12 |
<html lang="en">
|
@@ -400,7 +409,7 @@ HTML_CONTENT = """
|
|
400 |
const progressContainer = document.getElementById('progressContainer');
|
401 |
const loadingSpinner = document.getElementById('loadingSpinner');
|
402 |
const resultContainer = document.getElementById('resultContainer');
|
403 |
-
|
404 |
const modal = document.getElementById('embedModal');
|
405 |
const span = document.getElementsByClassName("close")[0];
|
406 |
const embedLinkInput = document.getElementById('embedLink');
|
@@ -408,14 +417,14 @@ HTML_CONTENT = """
|
|
408 |
|
409 |
fileInput.addEventListener('change', handleFileSelect);
|
410 |
|
411 |
-
uploadForm.addEventListener('submit', (e) => {
|
412 |
e.preventDefault();
|
413 |
if (fileInput.files.length > 0) {
|
414 |
-
uploadFile(fileInput.files[0]);
|
415 |
}
|
416 |
});
|
417 |
|
418 |
-
|
419 |
e.preventDefault();
|
420 |
dropZone.classList.add('drag-over');
|
421 |
});
|
@@ -477,59 +486,27 @@ HTML_CONTENT = """
|
|
477 |
const formData = new FormData();
|
478 |
formData.append('file', file);
|
479 |
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
resetUploadState();
|
496 |
-
return;
|
497 |
-
} else {
|
498 |
-
throw new Error('Upload failed: ' + response.error);
|
499 |
-
}
|
500 |
-
} else if (xhr.status === 308) {
|
501 |
-
// Partial upload successful, update uploadedBytes
|
502 |
-
const range = xhr.getResponseHeader('Range');
|
503 |
-
if (range) {
|
504 |
-
uploadedBytes = parseInt(range.split('-')[1]) + 1;
|
505 |
-
}
|
506 |
-
} else {
|
507 |
-
throw new Error(`HTTP error! status: ${xhr.status}`);
|
508 |
-
}
|
509 |
-
};
|
510 |
-
|
511 |
-
xhr.onerror = function() {
|
512 |
-
throw new Error('Network error occurred');
|
513 |
-
};
|
514 |
-
|
515 |
-
const chunk = file.slice(uploadedBytes, uploadedBytes + 1024 * 1024); // 1MB chunks
|
516 |
-
xhr.send(chunk);
|
517 |
-
|
518 |
-
// Wait for the request to complete
|
519 |
-
await new Promise((resolve, reject) => {
|
520 |
-
xhr.onloadend = resolve;
|
521 |
-
xhr.onerror = reject;
|
522 |
-
});
|
523 |
-
|
524 |
-
if (uploadedBytes >= totalBytes) {
|
525 |
-
break; // Upload complete
|
526 |
-
}
|
527 |
-
} catch (error) {
|
528 |
-
console.error('Upload error:', error);
|
529 |
-
// Wait for a short time before retrying
|
530 |
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
531 |
-
// The loop will continue, retrying the upload from where it left off
|
532 |
}
|
|
|
|
|
|
|
|
|
|
|
533 |
}
|
534 |
}
|
535 |
|
@@ -549,14 +526,6 @@ HTML_CONTENT = """
|
|
549 |
return container;
|
550 |
}
|
551 |
|
552 |
-
function updateProgress(event, progressBar, uploadedBytes) {
|
553 |
-
if (event.lengthComputable) {
|
554 |
-
const totalBytes = uploadedBytes + event.total;
|
555 |
-
const percentComplete = ((uploadedBytes + event.loaded) / totalBytes) * 100;
|
556 |
-
progressBar.style.width = percentComplete + '%';
|
557 |
-
}
|
558 |
-
}
|
559 |
-
|
560 |
function resetUploadState() {
|
561 |
fileInput.value = '';
|
562 |
fileName.textContent = '';
|
@@ -628,21 +597,23 @@ async def index():
|
|
628 |
|
629 |
@app.post("/upload")
|
630 |
async def handle_upload(file: UploadFile = File(...)):
|
631 |
-
if not file
|
632 |
-
|
|
|
|
|
633 |
|
634 |
cookies = await get_cookies()
|
635 |
if 'csrftoken' not in cookies or 'sessionid' not in cookies:
|
636 |
-
|
637 |
|
638 |
upload_result = await initiate_upload(cookies, file.filename, file.content_type)
|
639 |
if not upload_result or 'upload_url' not in upload_result:
|
640 |
-
|
641 |
|
642 |
file_content = await file.read()
|
643 |
upload_success = await retry_upload(upload_result['upload_url'], file_content, file.content_type)
|
644 |
if not upload_success:
|
645 |
-
|
646 |
|
647 |
original_url = upload_result['serving_url']
|
648 |
mirrored_url = f"/rbxg/{original_url.split('/pbxt/')[1]}"
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Request, HTTPException
|
2 |
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import requests
|
5 |
import time
|
6 |
import asyncio
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
app.add_middleware(
|
12 |
+
CORSMiddleware,
|
13 |
+
allow_origins=["*"],
|
14 |
+
allow_credentials=True,
|
15 |
+
allow_methods=["*"],
|
16 |
+
allow_headers=["*"],
|
17 |
+
)
|
18 |
+
|
19 |
HTML_CONTENT = """
|
20 |
<!DOCTYPE html>
|
21 |
<html lang="en">
|
|
|
409 |
const progressContainer = document.getElementById('progressContainer');
|
410 |
const loadingSpinner = document.getElementById('loadingSpinner');
|
411 |
const resultContainer = document.getElementById('resultContainer');
|
412 |
+
const dropZone = document.getElementById('dropZone');
|
413 |
const modal = document.getElementById('embedModal');
|
414 |
const span = document.getElementsByClassName("close")[0];
|
415 |
const embedLinkInput = document.getElementById('embedLink');
|
|
|
417 |
|
418 |
fileInput.addEventListener('change', handleFileSelect);
|
419 |
|
420 |
+
uploadForm.addEventListener('submit', async (e) => {
|
421 |
e.preventDefault();
|
422 |
if (fileInput.files.length > 0) {
|
423 |
+
await uploadFile(fileInput.files[0]);
|
424 |
}
|
425 |
});
|
426 |
|
427 |
+
dropZone.addEventListener('dragover', (e) => {
|
428 |
e.preventDefault();
|
429 |
dropZone.classList.add('drag-over');
|
430 |
});
|
|
|
486 |
const formData = new FormData();
|
487 |
formData.append('file', file);
|
488 |
|
489 |
+
try {
|
490 |
+
const response = await fetch('/upload', {
|
491 |
+
method: 'POST',
|
492 |
+
body: formData
|
493 |
+
});
|
494 |
+
|
495 |
+
if (!response.ok) {
|
496 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
497 |
+
}
|
498 |
+
|
499 |
+
const result = await response.json();
|
500 |
+
if (result.url) {
|
501 |
+
addResultLink(result.url, file.name);
|
502 |
+
} else {
|
503 |
+
throw new Error('Upload failed: ' + result.error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
504 |
}
|
505 |
+
} catch (error) {
|
506 |
+
console.error('Upload error:', error);
|
507 |
+
alert('Upload failed: ' + error.message);
|
508 |
+
} finally {
|
509 |
+
resetUploadState();
|
510 |
}
|
511 |
}
|
512 |
|
|
|
526 |
return container;
|
527 |
}
|
528 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
529 |
function resetUploadState() {
|
530 |
fileInput.value = '';
|
531 |
fileName.textContent = '';
|
|
|
597 |
|
598 |
@app.post("/upload")
|
599 |
async def handle_upload(file: UploadFile = File(...)):
|
600 |
+
if not file:
|
601 |
+
raise HTTPException(status_code=400, detail="No file part")
|
602 |
+
if file.filename == '':
|
603 |
+
raise HTTPException(status_code=400, detail="No selected file")
|
604 |
|
605 |
cookies = await get_cookies()
|
606 |
if 'csrftoken' not in cookies or 'sessionid' not in cookies:
|
607 |
+
raise HTTPException(status_code=500, detail="Failed to obtain necessary cookies")
|
608 |
|
609 |
upload_result = await initiate_upload(cookies, file.filename, file.content_type)
|
610 |
if not upload_result or 'upload_url' not in upload_result:
|
611 |
+
raise HTTPException(status_code=500, detail="Failed to initiate upload")
|
612 |
|
613 |
file_content = await file.read()
|
614 |
upload_success = await retry_upload(upload_result['upload_url'], file_content, file.content_type)
|
615 |
if not upload_success:
|
616 |
+
raise HTTPException(status_code=500, detail="File upload failed after multiple attempts")
|
617 |
|
618 |
original_url = upload_result['serving_url']
|
619 |
mirrored_url = f"/rbxg/{original_url.split('/pbxt/')[1]}"
|