Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,29 @@ import httpx
|
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
DENO_API_URL = "https://dataerrr99.deno.dev"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
albums = {}
|
| 17 |
|
| 18 |
HTML_CONTENT = """
|
|
@@ -287,39 +310,6 @@ SEARCH_RESULTS_HTML = """
|
|
| 287 |
</html>
|
| 288 |
"""
|
| 289 |
|
| 290 |
-
async def save_album_to_db(album_name: str, album_id: str, album_files: list):
|
| 291 |
-
async with httpx.AsyncClient() as client:
|
| 292 |
-
try:
|
| 293 |
-
response = await client.post(DENO_API_URL, json={
|
| 294 |
-
"albumName": album_name,
|
| 295 |
-
"albumLink": album_id,
|
| 296 |
-
"files": album_files,
|
| 297 |
-
"createdAt": datetime.now().isoformat()
|
| 298 |
-
})
|
| 299 |
-
return response.json()
|
| 300 |
-
except Exception as e:
|
| 301 |
-
print(f"Error saving to DB: {str(e)}")
|
| 302 |
-
return None
|
| 303 |
-
|
| 304 |
-
async def load_albums_from_db():
|
| 305 |
-
async with httpx.AsyncClient() as client:
|
| 306 |
-
try:
|
| 307 |
-
response = await client.get(DENO_API_URL)
|
| 308 |
-
if response.status_code == 200:
|
| 309 |
-
albums_data = response.json()['data']
|
| 310 |
-
for album in albums_data:
|
| 311 |
-
albums[album['albumLink']] = {
|
| 312 |
-
'name': album['albumName'],
|
| 313 |
-
'created_at': album['createdAt'],
|
| 314 |
-
'files': album.get('files', [])
|
| 315 |
-
}
|
| 316 |
-
except Exception as e:
|
| 317 |
-
print(f"Error loading albums from DB: {str(e)}")
|
| 318 |
-
|
| 319 |
-
@app.on_event("startup")
|
| 320 |
-
async def startup_event():
|
| 321 |
-
await load_albums_from_db()
|
| 322 |
-
|
| 323 |
def get_file_type(filename):
|
| 324 |
mime_type, _ = mimetypes.guess_type(filename)
|
| 325 |
if mime_type:
|
|
@@ -339,38 +329,28 @@ async def search_albums(query: str = ""):
|
|
| 339 |
db_albums = await get_albums_from_db()
|
| 340 |
|
| 341 |
matching_albums = {
|
| 342 |
-
|
| 343 |
-
|
|
|
|
| 344 |
}
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
return SEARCH_RESULTS_HTML.format(
|
| 347 |
query=query,
|
| 348 |
result_count=len(matching_albums),
|
| 349 |
-
results=
|
| 350 |
)
|
| 351 |
|
| 352 |
-
@app.post("/upload")
|
| 353 |
-
async def handle_upload(request: Request, file: UploadFile = File(...)):
|
| 354 |
-
if not file:
|
| 355 |
-
return {"error": "No file uploaded"}, 400
|
| 356 |
-
|
| 357 |
-
cookies = await get_cookies()
|
| 358 |
-
if 'csrftoken' not in cookies or 'sessionid' not in cookies:
|
| 359 |
-
return {"error": "Failed to get cookies"}, 500
|
| 360 |
-
|
| 361 |
-
upload_result = await initiate_upload(cookies, file.filename, file.content_type)
|
| 362 |
-
if not upload_result or 'upload_url' not in upload_result:
|
| 363 |
-
return {"error": "Failed to initiate upload"}, 500
|
| 364 |
-
|
| 365 |
-
file_content = await file.read()
|
| 366 |
-
upload_success = await retry_upload(upload_result['upload_url'], file_content, file.content_type)
|
| 367 |
-
if not upload_success:
|
| 368 |
-
return {"error": "Upload failed"}, 500
|
| 369 |
-
|
| 370 |
-
base_url = str(request.base_url).rstrip('/')
|
| 371 |
-
redirect_url = f"{base_url}/upload/{upload_result['serving_url'].split('/pbxt/')[1]}"
|
| 372 |
-
return RedirectResponse(url=redirect_url, status_code=303)
|
| 373 |
-
|
| 374 |
@app.post("/album/create")
|
| 375 |
async def create_album(
|
| 376 |
request: Request,
|
|
@@ -390,22 +370,21 @@ async def create_album(
|
|
| 390 |
upload_success = await retry_upload(upload_result['upload_url'], file_content, file.content_type)
|
| 391 |
if upload_success:
|
| 392 |
serving_path = upload_result['serving_url'].split('/pbxt/')[1]
|
| 393 |
-
|
| 394 |
'filename': file.filename,
|
| 395 |
'path': serving_path,
|
| 396 |
'content_type': file.content_type,
|
| 397 |
'uploaded_at': datetime.now().isoformat()
|
| 398 |
-
}
|
| 399 |
-
album_files.append(file_data)
|
| 400 |
|
| 401 |
-
|
| 402 |
'name': album_name,
|
| 403 |
'created_at': datetime.now().isoformat(),
|
| 404 |
'files': album_files
|
| 405 |
}
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
await save_album_to_db(album_name, album_id
|
| 409 |
|
| 410 |
base_url = str(request.base_url).rstrip('/')
|
| 411 |
return RedirectResponse(url=f"{base_url}/album/{album_id}", status_code=303)
|
|
@@ -416,10 +395,48 @@ async def view_album(album_id: str):
|
|
| 416 |
return "Album not found", 404
|
| 417 |
|
| 418 |
album = albums[album_id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
return ALBUM_VIEW_HTML.format(
|
| 420 |
album_name=album['name'],
|
| 421 |
album_id=album_id,
|
| 422 |
-
file_list=
|
| 423 |
)
|
| 424 |
|
| 425 |
@app.get("/album/{album_id}/download")
|
|
@@ -497,4 +514,4 @@ async def retry_upload(upload_url, file_content, content_type, max_retries=5):
|
|
| 497 |
|
| 498 |
if __name__ == "__main__":
|
| 499 |
import uvicorn
|
| 500 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
DENO_API_URL = "https://dataerrr99.deno.dev"
|
| 16 |
+
|
| 17 |
+
async def save_album_to_db(album_name: str, album_id: str):
|
| 18 |
+
async with httpx.AsyncClient() as client:
|
| 19 |
+
try:
|
| 20 |
+
response = await client.post(DENO_API_URL, json={
|
| 21 |
+
"albumName": album_name,
|
| 22 |
+
"albumLink": album_id
|
| 23 |
+
})
|
| 24 |
+
return response.json()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error saving to DB: {str(e)}")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
async def get_albums_from_db():
|
| 30 |
+
async with httpx.AsyncClient() as client:
|
| 31 |
+
try:
|
| 32 |
+
response = await client.get(DENO_API_URL)
|
| 33 |
+
return response.json()
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error fetching from DB: {str(e)}")
|
| 36 |
+
return {"data": []}
|
| 37 |
+
|
| 38 |
+
# In-memory storage for albums
|
| 39 |
albums = {}
|
| 40 |
|
| 41 |
HTML_CONTENT = """
|
|
|
|
| 310 |
</html>
|
| 311 |
"""
|
| 312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
def get_file_type(filename):
|
| 314 |
mime_type, _ = mimetypes.guess_type(filename)
|
| 315 |
if mime_type:
|
|
|
|
| 329 |
db_albums = await get_albums_from_db()
|
| 330 |
|
| 331 |
matching_albums = {
|
| 332 |
+
album['link']: albums[album['link']]
|
| 333 |
+
for album in db_albums['data']
|
| 334 |
+
if album['link'] in albums and query in album['name'].lower()
|
| 335 |
}
|
| 336 |
|
| 337 |
+
results_html = ""
|
| 338 |
+
for album_id, album in matching_albums.items():
|
| 339 |
+
results_html += f"""
|
| 340 |
+
<div class="album-item">
|
| 341 |
+
<h3>{album['name']}</h3>
|
| 342 |
+
<p>Created: {album['created_at']}</p>
|
| 343 |
+
<p>Files: {len(album['files'])}</p>
|
| 344 |
+
<a href="/album/{album_id}" class="button">View Album</a>
|
| 345 |
+
</div>
|
| 346 |
+
"""
|
| 347 |
+
|
| 348 |
return SEARCH_RESULTS_HTML.format(
|
| 349 |
query=query,
|
| 350 |
result_count=len(matching_albums),
|
| 351 |
+
results=results_html
|
| 352 |
)
|
| 353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
@app.post("/album/create")
|
| 355 |
async def create_album(
|
| 356 |
request: Request,
|
|
|
|
| 370 |
upload_success = await retry_upload(upload_result['upload_url'], file_content, file.content_type)
|
| 371 |
if upload_success:
|
| 372 |
serving_path = upload_result['serving_url'].split('/pbxt/')[1]
|
| 373 |
+
album_files.append({
|
| 374 |
'filename': file.filename,
|
| 375 |
'path': serving_path,
|
| 376 |
'content_type': file.content_type,
|
| 377 |
'uploaded_at': datetime.now().isoformat()
|
| 378 |
+
})
|
|
|
|
| 379 |
|
| 380 |
+
albums[album_id] = {
|
| 381 |
'name': album_name,
|
| 382 |
'created_at': datetime.now().isoformat(),
|
| 383 |
'files': album_files
|
| 384 |
}
|
| 385 |
+
|
| 386 |
+
# Save to Deno KV database
|
| 387 |
+
await save_album_to_db(album_name, album_id)
|
| 388 |
|
| 389 |
base_url = str(request.base_url).rstrip('/')
|
| 390 |
return RedirectResponse(url=f"{base_url}/album/{album_id}", status_code=303)
|
|
|
|
| 395 |
return "Album not found", 404
|
| 396 |
|
| 397 |
album = albums[album_id]
|
| 398 |
+
file_list_html = ""
|
| 399 |
+
|
| 400 |
+
for file in album['files']:
|
| 401 |
+
file_url = f"/upload/{file['path']}"
|
| 402 |
+
file_type = get_file_type(file['filename'])
|
| 403 |
+
|
| 404 |
+
preview_html = ""
|
| 405 |
+
if file_type == 'image':
|
| 406 |
+
preview_html = f"""
|
| 407 |
+
<div class="preview-container">
|
| 408 |
+
<img src="{file_url}" alt="{file['filename']}" onclick="openModal('{file_url}')">
|
| 409 |
+
</div>
|
| 410 |
+
"""
|
| 411 |
+
elif file_type == 'video':
|
| 412 |
+
preview_html = f"""
|
| 413 |
+
<div class="preview-container">
|
| 414 |
+
<video onclick="openModal('{file_url}')" style="cursor: pointer;">
|
| 415 |
+
<source src="{file_url}" type="{file['content_type']}">
|
| 416 |
+
Your browser does not support the video tag.
|
| 417 |
+
</video>
|
| 418 |
+
</div>
|
| 419 |
+
"""
|
| 420 |
+
else:
|
| 421 |
+
preview_html = f"""
|
| 422 |
+
<div class="preview-container">
|
| 423 |
+
<p>No preview available for {file['filename']}</p>
|
| 424 |
+
</div>
|
| 425 |
+
"""
|
| 426 |
+
|
| 427 |
+
file_list_html += f"""
|
| 428 |
+
<div class="file-item">
|
| 429 |
+
{preview_html}
|
| 430 |
+
<p>{file['filename']}</p>
|
| 431 |
+
<a href="{file_url}" class="button" onclick="openModal('{file_url}'); return false;">View</a>
|
| 432 |
+
<a href="{file_url}?download=true" class="button" target="_blank">Download</a>
|
| 433 |
+
</div>
|
| 434 |
+
"""
|
| 435 |
+
|
| 436 |
return ALBUM_VIEW_HTML.format(
|
| 437 |
album_name=album['name'],
|
| 438 |
album_id=album_id,
|
| 439 |
+
file_list=file_list_html
|
| 440 |
)
|
| 441 |
|
| 442 |
@app.get("/album/{album_id}/download")
|
|
|
|
| 514 |
|
| 515 |
if __name__ == "__main__":
|
| 516 |
import uvicorn
|
| 517 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|