Spaces:
Running
Running
fix: 0-byte files on Google Drive - resumable upload + validation
Browse filesFiles changed (6)
79 unmodified lines808182838485868779 unmodified lines fpath = f.get("path", "") fname = f.get("name", os.path.basename(fpath)) if fpath and os.path.exists(fpath): if os.path.getsize(fpath) == 0: continue result = drive_svc.upload_file(run_folder_id, fpath, fname) if result: uploaded.append({45 unmodified lines464748495051525345 unmodified lines
def save_upload(sid: str, filename: str, data: bytes, folder: str = "uploads") -> str: if not data or len(data) == 0: raise ValueError(f"Upload file '{filename}' is empty (0 bytes)") d = sub_dir(sid, folder) safe = os.path.basename(filename).replace("/", "_") path = os.path.join(d, safe)129 unmodified lines1301311321331341351361371381 unmodified line14014114214014314414514614314714814915015115214915015315415515615315715815515615715916016116216316416516616716816917017117217317437 unmodified lines21221321421521621721821920 unmodified lines240241242243244245246247248249250129 unmodified linesdef upload_file(folder_id: str, file_path: str, name: str | None = None) -> dict | None: if not os.path.exists(file_path): return None file_size = os.path.getsize(file_path) if file_size == 0: return None if not name: name = os.path.basename(file_path)
1 unmodified line if not h: return None
# Step 1: Create file with metadata # Use resumable upload for ALL files (simple upload limited to 5MB) # Step 1: Initiate resumable session metadata = {"name": name, "parents": [folder_id]} cr = requests.post( f"{API_BASE}/files?fields=id,name,webViewLink", f"{UPLOAD_BASE}/files?uploadType=resumable&fields=id,name,webViewLink", headers={**h, "Content-Type": "application/json"}, json=metadata, ) if cr.status_code not in (200, 201): return None file_id = cr.json().get("id") if not file_id: session_uri = cr.headers.get("Location") if not session_uri: return None
# Step 2: Upload file content via simple media upload # Step 2: Upload file content via resumable session with open(file_path, "rb") as f: cr = requests.patch( f"{UPLOAD_BASE}/files/{file_id}?uploadType=media", headers={**h, "Content-Type": "application/octet-stream"}, cr = requests.put( session_uri, headers={**h, "Content-Type": "application/octet-stream", "Content-Length": str(file_size)}, data=f, ) if cr.status_code not in (200, 201): return None
# Step 3: Fetch final file info with webViewLink file_id = cr.json().get("id") if cr.status_code in (200, 201) else None if not file_id: return None cr = requests.get( f"{API_BASE}/files/{file_id}?fields=id,name,webViewLink", headers=h,37 unmodified lines
def upload_content(folder_id: str, name: str, content: str, mime_type: str = "application/json") -> dict | None: """Upload text content directly (no local file needed).""" if not content: return None h = _auth_header() if not h: return None20 unmodified lines ) if cr.status_code == 200: return cr.json() # Cleanup: xoa file rỗng khi upload that bai try: delete_file(file_id) except Exception: pass return None
132 unmodified lines133134135136136137138139132 unmodified lines audio_out = video_out srt_out = os.path.join(out_dir, "subtitle.srt") with open(srt_out, "w", encoding="utf-8") as f: f.write("") f.write("1\n00:00:00,000 --> 00:00:02,000\n[No subtitle - dubbing failed]\n\n") return DubResult(video_out=video_out, audio_out=audio_out, srt_out=srt_out, language="unknown", n_lines=0, note=note)
29 unmodified lines30313233343536373829 unmodified lines fpath = fm.get("path", "") fname = fm.get("name", os.path.basename(fpath)) if fpath and os.path.exists(fpath): # Bo qua file rong (0 byte) if os.path.getsize(fpath) == 0: continue result = drive_svc.upload_file(run_fid, fpath, fname) if result: drive_files.append({94 unmodified lines959697989910010110210310410010510610710873 unmodified lines18218318418518618718818919094 unmodified lines
if data and "result" in data and "image" in data["result"]: img_b64 = data["result"]["image"] if not img_b64: raise RuntimeError("Flux API returned empty image data") img_bytes = base64.b64decode(img_b64) if len(img_bytes) == 0: raise RuntimeError("Flux API returned empty image bytes") os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True) with open(output_path, "wb") as f: f.write(base64.b64decode(img_b64)) f.write(img_bytes) return output_path
errors = data.get("errors", []) if data else []73 unmodified lines else: raise RuntimeError("OpenAI returned no image content (no url or b64_json)")
if len(img_bytes) == 0: raise RuntimeError("OpenAI returned empty image data")
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True) with open(output_path, "wb") as f: f.write(img_bytes)backend/app/api/history_api.py+2
backend/app/core/files.py+2
backend/app/services/drive/service.py+23-8
backend/app/services/dubber/pipeline.py+1-1
backend/app/services/history/auto_save.py+3
backend/app/services/listing/generator.py+9-1