Spaces:
Sleeping
Sleeping
Download app.py from SatyaDev13/filboxtemp: direct link, hf CLI and curl.
- Browser
- Download file 4.21 kB
-
https://huggingface.co/spaces/SatyaDev13/filboxtemp/resolve/main/app.py
- Command line
-
hf download hf://spaces/SatyaDev13/filboxtemp/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SatyaDev13/filboxtemp/resolve/main/app.py
4.21 kB
| from fastapi import FastAPI, UploadFile, File, HTTPException, Request, Query | |
| from fastapi.responses import FileResponse, JSONResponse | |
| import uuid | |
| import os | |
| from pathlib import Path | |
| from datetime import datetime | |
| import aiofiles | |
| import hashlib | |
| app = FastAPI(title="Secure Temporary File Storage") | |
| TEMP_DIR = Path("/tmp/hf_temp") | |
| TEMP_DIR.mkdir(exist_ok=True) | |
| file_metadata = {} | |
| # Secret Hash (SHA-256) | |
| SECRET_HASH = "8e5f0ccea2b9e3f5319ca476186beb537ef8c306bbaa590d738f1baeae252b16" | |
| def verify_secret(secret: str) -> bool: | |
| """Verifikasi secret dengan SHA-256""" | |
| return hashlib.sha256(secret.encode()).hexdigest() == SECRET_HASH | |
| async def upload_file(file: UploadFile = File(...), request: Request = None): | |
| try: | |
| file_id = str(uuid.uuid4()) | |
| file_ext = os.path.splitext(file.filename)[1].lower() | |
| save_filename = f"{file_id}{file_ext}" | |
| save_path = TEMP_DIR / save_filename | |
| async with aiofiles.open(save_path, "wb") as f: | |
| content = await file.read() | |
| await f.write(content) | |
| base_url = str(request.base_url).rstrip("/") | |
| full_download_url = f"{base_url}/download/{save_filename}" | |
| file_metadata[file_id] = { | |
| "filename": file.filename, | |
| "save_filename": save_filename, | |
| "size": len(content), | |
| "uploaded_at": datetime.now().isoformat(), | |
| "path": str(save_path), | |
| "content_type": file.content_type | |
| } | |
| return JSONResponse({ | |
| "status": "success", | |
| "file_id": file_id, | |
| "filename": file.filename, | |
| "save_name": save_filename, | |
| "size": len(content), | |
| "download_url": full_download_url, | |
| "message": "File berhasil diupload" | |
| }) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def download_file(file_name: str): | |
| for fid, data in file_metadata.items(): | |
| if data["save_filename"] == file_name: | |
| file_path = Path(data["path"]) | |
| if not file_path.exists(): | |
| del file_metadata[fid] | |
| raise HTTPException(status_code=404, detail="File sudah dihapus") | |
| return FileResponse( | |
| path=file_path, | |
| filename=data["filename"], | |
| media_type=data.get("content_type", "application/octet-stream") | |
| ) | |
| raise HTTPException(status_code=404, detail="File tidak ditemukan") | |
| # === Protected Routes === | |
| async def list_files(request: Request, secret: str = Query(...)): | |
| if not verify_secret(secret): | |
| raise HTTPException(status_code=401, detail="Unauthorized - Secret salah") | |
| base_url = str(request.base_url).rstrip("/") | |
| return { | |
| "total_files": len(file_metadata), | |
| "files": [ | |
| { | |
| "file_id": fid, | |
| "original_name": data["filename"], | |
| "save_name": data["save_filename"], | |
| "size": data["size"], | |
| "download_url": f"{base_url}/download/{data['save_filename']}", | |
| "uploaded_at": data["uploaded_at"] | |
| } | |
| for fid, data in file_metadata.items() | |
| ] | |
| } | |
| async def check_file_existence(file_id: str): | |
| """Endpoint publik untuk cek apakah file masih ada""" | |
| exists = file_id in file_metadata | |
| if exists: | |
| file_path = Path(file_metadata[file_id]["path"]) | |
| exists = file_path.exists() | |
| return {"file_id": file_id, "exists": exists} | |
| async def delete_file(file_id: str, secret: str = Query(...)): | |
| if not verify_secret(secret): | |
| raise HTTPException(status_code=401, detail="Unauthorized - Secret salah") | |
| if file_id not in file_metadata: | |
| raise HTTPException(status_code=404, detail="File tidak ditemukan") | |
| file_path = Path(file_metadata[file_id]["path"]) | |
| if file_path.exists(): | |
| os.remove(file_path) | |
| del file_metadata[file_id] | |
| return {"status": "success", "message": "File berhasil dihapus"} |