Spaces:
Running
Running
fix: 0-byte files on Google Drive - resumable upload + validation
Browse filesFiles changed (6)
79 unmodified lines80818283848579 unmodified linesfpath = f.get("path", "")fname = f.get("name", os.path.basename(fpath))if fpath and os.path.exists(fpath):result = drive_svc.upload_file(run_folder_id, fpath, fname)if result:uploaded.append({79 unmodified lines808182838485868779 unmodified linesfpath = f.get("path", "")fname = f.get("name", os.path.basename(fpath))if fpath and os.path.exists(fpath):if os.path.getsize(fpath) == 0:continueresult = drive_svc.upload_file(run_folder_id, fpath, fname)if result:uploaded.append({
45 unmodified lines46474849505145 unmodified linesdef save_upload(sid: str, filename: str, data: bytes, folder: str = "uploads") -> str:d = sub_dir(sid, folder)safe = os.path.basename(filename).replace("/", "_")path = os.path.join(d, safe)45 unmodified lines464748495051525345 unmodified linesdef 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 lines1301311321331341351 unmodified line13713813914014114214314414514614714814915015115215315415515615715815916016116216316416516637 unmodified lines20420520620720820920 unmodified lines230231232233234235129 unmodified linesdef upload_file(folder_id: str, file_path: str, name: str | None = None) -> dict | None:if not os.path.exists(file_path):return Noneif not name:name = os.path.basename(file_path)1 unmodified lineif not h:return None# Step 1: Create file with metadatametadata = {"name": name, "parents": [folder_id]}cr = requests.post(f"{API_BASE}/files?fields=id,name,webViewLink",headers={**h, "Content-Type": "application/json"},json=metadata,)if cr.status_code not in (200, 201):return Nonefile_id = cr.json().get("id")if not file_id:return None# Step 2: Upload file content via simple media uploadwith open(file_path, "rb") as f:cr = requests.patch(f"{UPLOAD_BASE}/files/{file_id}?uploadType=media",headers={**h, "Content-Type": "application/octet-stream"},data=f,)if cr.status_code not in (200, 201):return None# Step 3: Fetch final file info with webViewLinkcr = requests.get(f"{API_BASE}/files/{file_id}?fields=id,name,webViewLink",headers=h,37 unmodified linesdef upload_content(folder_id: str, name: str, content: str, mime_type: str = "application/json") -> dict | None:"""Upload text content directly (no local file needed)."""h = _auth_header()if not h:return None20 unmodified lines)if cr.status_code == 200:return cr.json()return None129 unmodified lines1301311321331341351361371381 unmodified line14014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317437 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 Nonefile_size = os.path.getsize(file_path)if file_size == 0:return Noneif not name:name = os.path.basename(file_path)1 unmodified lineif not h:return None# Use resumable upload for ALL files (simple upload limited to 5MB)# Step 1: Initiate resumable sessionmetadata = {"name": name, "parents": [folder_id]}cr = requests.post(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 Nonesession_uri = cr.headers.get("Location")if not session_uri:return None# Step 2: Upload file content via resumable sessionwith open(file_path, "rb") as f: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 webViewLinkfile_id = cr.json().get("id") if cr.status_code in (200, 201) else Noneif not file_id:return Nonecr = requests.get(f"{API_BASE}/files/{file_id}?fields=id,name,webViewLink",headers=h,37 unmodified linesdef 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 Noneh = _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 baitry:delete_file(file_id)except Exception:passreturn None
132 unmodified lines133134135136137138139132 unmodified linesaudio_out = video_outsrt_out = os.path.join(out_dir, "subtitle.srt")with open(srt_out, "w", encoding="utf-8") as f:f.write("")return DubResult(video_out=video_out, audio_out=audio_out, srt_out=srt_out,language="unknown", n_lines=0, note=note)132 unmodified lines133134135136137138139132 unmodified linesaudio_out = video_outsrt_out = os.path.join(out_dir, "subtitle.srt")with open(srt_out, "w", encoding="utf-8") as f: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 lines30313233343529 unmodified linesfpath = fm.get("path", "")fname = fm.get("name", os.path.basename(fpath))if fpath and os.path.exists(fpath):result = drive_svc.upload_file(run_fid, fpath, fname)if result:drive_files.append({29 unmodified lines30313233343536373829 unmodified linesfpath = 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:continueresult = drive_svc.upload_file(run_fid, fpath, fname)if result:drive_files.append({
94 unmodified lines959697989910010110210373 unmodified lines17717817918018118294 unmodified linesif data and "result" in data and "image" in data["result"]:img_b64 = data["result"]["image"]os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)with open(output_path, "wb") as f:f.write(base64.b64decode(img_b64))return output_patherrors = data.get("errors", []) if data else []73 unmodified lineselse:raise RuntimeError("OpenAI returned no image content (no url or b64_json)")os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)with open(output_path, "wb") as f:f.write(img_bytes)94 unmodified lines959697989910010110210310410510610710873 unmodified lines18218318418518618718818919094 unmodified linesif 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(img_bytes)return output_patherrors = data.get("errors", []) if data else []73 unmodified lineselse: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