Spaces:
Running
Running
fix: 0-byte files on Google Drive - resumable upload + validation
Browse files
backend/app/api/history_api.py
CHANGED
|
@@ -80,6 +80,8 @@ def auto_save(body: dict):
|
|
| 80 |
fpath = f.get("path", "")
|
| 81 |
fname = f.get("name", os.path.basename(fpath))
|
| 82 |
if fpath and os.path.exists(fpath):
|
|
|
|
|
|
|
| 83 |
result = drive_svc.upload_file(run_folder_id, fpath, fname)
|
| 84 |
if result:
|
| 85 |
uploaded.append({
|
|
|
|
| 80 |
fpath = f.get("path", "")
|
| 81 |
fname = f.get("name", os.path.basename(fpath))
|
| 82 |
if fpath and os.path.exists(fpath):
|
| 83 |
+
if os.path.getsize(fpath) == 0:
|
| 84 |
+
continue
|
| 85 |
result = drive_svc.upload_file(run_folder_id, fpath, fname)
|
| 86 |
if result:
|
| 87 |
uploaded.append({
|
backend/app/core/files.py
CHANGED
|
@@ -46,6 +46,8 @@ def sub_dir(sid: str, *parts: str) -> str:
|
|
| 46 |
|
| 47 |
|
| 48 |
def save_upload(sid: str, filename: str, data: bytes, folder: str = "uploads") -> str:
|
|
|
|
|
|
|
| 49 |
d = sub_dir(sid, folder)
|
| 50 |
safe = os.path.basename(filename).replace("/", "_")
|
| 51 |
path = os.path.join(d, safe)
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
def save_upload(sid: str, filename: str, data: bytes, folder: str = "uploads") -> str:
|
| 49 |
+
if not data or len(data) == 0:
|
| 50 |
+
raise ValueError(f"Upload file '{filename}' is empty (0 bytes)")
|
| 51 |
d = sub_dir(sid, folder)
|
| 52 |
safe = os.path.basename(filename).replace("/", "_")
|
| 53 |
path = os.path.join(d, safe)
|
backend/app/services/drive/service.py
CHANGED
|
@@ -130,6 +130,9 @@ def find_or_none(parent_id: str, name: str) -> str | None:
|
|
| 130 |
def upload_file(folder_id: str, file_path: str, name: str | None = None) -> dict | None:
|
| 131 |
if not os.path.exists(file_path):
|
| 132 |
return None
|
|
|
|
|
|
|
|
|
|
| 133 |
if not name:
|
| 134 |
name = os.path.basename(file_path)
|
| 135 |
|
|
@@ -137,30 +140,35 @@ def upload_file(folder_id: str, file_path: str, name: str | None = None) -> dict
|
|
| 137 |
if not h:
|
| 138 |
return None
|
| 139 |
|
| 140 |
-
#
|
|
|
|
| 141 |
metadata = {"name": name, "parents": [folder_id]}
|
| 142 |
cr = requests.post(
|
| 143 |
-
f"{
|
| 144 |
headers={**h, "Content-Type": "application/json"},
|
| 145 |
json=metadata,
|
| 146 |
)
|
| 147 |
if cr.status_code not in (200, 201):
|
| 148 |
return None
|
| 149 |
-
|
| 150 |
-
if not
|
| 151 |
return None
|
| 152 |
|
| 153 |
-
# Step 2: Upload file content via
|
| 154 |
with open(file_path, "rb") as f:
|
| 155 |
-
cr = requests.
|
| 156 |
-
|
| 157 |
-
headers={**h, "Content-Type": "application/octet-stream"
|
|
|
|
| 158 |
data=f,
|
| 159 |
)
|
| 160 |
if cr.status_code not in (200, 201):
|
| 161 |
return None
|
| 162 |
|
| 163 |
# Step 3: Fetch final file info with webViewLink
|
|
|
|
|
|
|
|
|
|
| 164 |
cr = requests.get(
|
| 165 |
f"{API_BASE}/files/{file_id}?fields=id,name,webViewLink",
|
| 166 |
headers=h,
|
|
@@ -204,6 +212,8 @@ def find_file_by_name(folder_id: str, name: str) -> dict | None:
|
|
| 204 |
|
| 205 |
def upload_content(folder_id: str, name: str, content: str, mime_type: str = "application/json") -> dict | None:
|
| 206 |
"""Upload text content directly (no local file needed)."""
|
|
|
|
|
|
|
| 207 |
h = _auth_header()
|
| 208 |
if not h:
|
| 209 |
return None
|
|
@@ -230,6 +240,11 @@ def upload_content(folder_id: str, name: str, content: str, mime_type: str = "ap
|
|
| 230 |
)
|
| 231 |
if cr.status_code == 200:
|
| 232 |
return cr.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
return None
|
| 234 |
|
| 235 |
|
|
|
|
| 130 |
def upload_file(folder_id: str, file_path: str, name: str | None = None) -> dict | None:
|
| 131 |
if not os.path.exists(file_path):
|
| 132 |
return None
|
| 133 |
+
file_size = os.path.getsize(file_path)
|
| 134 |
+
if file_size == 0:
|
| 135 |
+
return None
|
| 136 |
if not name:
|
| 137 |
name = os.path.basename(file_path)
|
| 138 |
|
|
|
|
| 140 |
if not h:
|
| 141 |
return None
|
| 142 |
|
| 143 |
+
# Use resumable upload for ALL files (simple upload limited to 5MB)
|
| 144 |
+
# Step 1: Initiate resumable session
|
| 145 |
metadata = {"name": name, "parents": [folder_id]}
|
| 146 |
cr = requests.post(
|
| 147 |
+
f"{UPLOAD_BASE}/files?uploadType=resumable&fields=id,name,webViewLink",
|
| 148 |
headers={**h, "Content-Type": "application/json"},
|
| 149 |
json=metadata,
|
| 150 |
)
|
| 151 |
if cr.status_code not in (200, 201):
|
| 152 |
return None
|
| 153 |
+
session_uri = cr.headers.get("Location")
|
| 154 |
+
if not session_uri:
|
| 155 |
return None
|
| 156 |
|
| 157 |
+
# Step 2: Upload file content via resumable session
|
| 158 |
with open(file_path, "rb") as f:
|
| 159 |
+
cr = requests.put(
|
| 160 |
+
session_uri,
|
| 161 |
+
headers={**h, "Content-Type": "application/octet-stream",
|
| 162 |
+
"Content-Length": str(file_size)},
|
| 163 |
data=f,
|
| 164 |
)
|
| 165 |
if cr.status_code not in (200, 201):
|
| 166 |
return None
|
| 167 |
|
| 168 |
# Step 3: Fetch final file info with webViewLink
|
| 169 |
+
file_id = cr.json().get("id") if cr.status_code in (200, 201) else None
|
| 170 |
+
if not file_id:
|
| 171 |
+
return None
|
| 172 |
cr = requests.get(
|
| 173 |
f"{API_BASE}/files/{file_id}?fields=id,name,webViewLink",
|
| 174 |
headers=h,
|
|
|
|
| 212 |
|
| 213 |
def upload_content(folder_id: str, name: str, content: str, mime_type: str = "application/json") -> dict | None:
|
| 214 |
"""Upload text content directly (no local file needed)."""
|
| 215 |
+
if not content:
|
| 216 |
+
return None
|
| 217 |
h = _auth_header()
|
| 218 |
if not h:
|
| 219 |
return None
|
|
|
|
| 240 |
)
|
| 241 |
if cr.status_code == 200:
|
| 242 |
return cr.json()
|
| 243 |
+
# Cleanup: xoa file rỗng khi upload that bai
|
| 244 |
+
try:
|
| 245 |
+
delete_file(file_id)
|
| 246 |
+
except Exception:
|
| 247 |
+
pass
|
| 248 |
return None
|
| 249 |
|
| 250 |
|
backend/app/services/dubber/pipeline.py
CHANGED
|
@@ -133,7 +133,7 @@ def _passthrough(video: str, out_dir: str, opt: DubOptions, note: str) -> DubRes
|
|
| 133 |
audio_out = video_out
|
| 134 |
srt_out = os.path.join(out_dir, "subtitle.srt")
|
| 135 |
with open(srt_out, "w", encoding="utf-8") as f:
|
| 136 |
-
f.write("")
|
| 137 |
return DubResult(video_out=video_out, audio_out=audio_out, srt_out=srt_out,
|
| 138 |
language="unknown", n_lines=0, note=note)
|
| 139 |
|
|
|
|
| 133 |
audio_out = video_out
|
| 134 |
srt_out = os.path.join(out_dir, "subtitle.srt")
|
| 135 |
with open(srt_out, "w", encoding="utf-8") as f:
|
| 136 |
+
f.write("1\n00:00:00,000 --> 00:00:02,000\n[No subtitle - dubbing failed]\n\n")
|
| 137 |
return DubResult(video_out=video_out, audio_out=audio_out, srt_out=srt_out,
|
| 138 |
language="unknown", n_lines=0, note=note)
|
| 139 |
|
backend/app/services/history/auto_save.py
CHANGED
|
@@ -30,6 +30,9 @@ def auto_save_result(
|
|
| 30 |
fpath = fm.get("path", "")
|
| 31 |
fname = fm.get("name", os.path.basename(fpath))
|
| 32 |
if fpath and os.path.exists(fpath):
|
|
|
|
|
|
|
|
|
|
| 33 |
result = drive_svc.upload_file(run_fid, fpath, fname)
|
| 34 |
if result:
|
| 35 |
drive_files.append({
|
|
|
|
| 30 |
fpath = fm.get("path", "")
|
| 31 |
fname = fm.get("name", os.path.basename(fpath))
|
| 32 |
if fpath and os.path.exists(fpath):
|
| 33 |
+
# Bo qua file rong (0 byte)
|
| 34 |
+
if os.path.getsize(fpath) == 0:
|
| 35 |
+
continue
|
| 36 |
result = drive_svc.upload_file(run_fid, fpath, fname)
|
| 37 |
if result:
|
| 38 |
drive_files.append({
|
backend/app/services/listing/generator.py
CHANGED
|
@@ -95,9 +95,14 @@ def generate_image_flux(prompt, output_path, input_path=None, negative_prompt=No
|
|
| 95 |
|
| 96 |
if data and "result" in data and "image" in data["result"]:
|
| 97 |
img_b64 = data["result"]["image"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
|
| 99 |
with open(output_path, "wb") as f:
|
| 100 |
-
f.write(
|
| 101 |
return output_path
|
| 102 |
|
| 103 |
errors = data.get("errors", []) if data else []
|
|
@@ -177,6 +182,9 @@ def generate_image_openai(prompt, output_path, model, input_path=None, quality="
|
|
| 177 |
else:
|
| 178 |
raise RuntimeError("OpenAI returned no image content (no url or b64_json)")
|
| 179 |
|
|
|
|
|
|
|
|
|
|
| 180 |
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
|
| 181 |
with open(output_path, "wb") as f:
|
| 182 |
f.write(img_bytes)
|
|
|
|
| 95 |
|
| 96 |
if data and "result" in data and "image" in data["result"]:
|
| 97 |
img_b64 = data["result"]["image"]
|
| 98 |
+
if not img_b64:
|
| 99 |
+
raise RuntimeError("Flux API returned empty image data")
|
| 100 |
+
img_bytes = base64.b64decode(img_b64)
|
| 101 |
+
if len(img_bytes) == 0:
|
| 102 |
+
raise RuntimeError("Flux API returned empty image bytes")
|
| 103 |
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
|
| 104 |
with open(output_path, "wb") as f:
|
| 105 |
+
f.write(img_bytes)
|
| 106 |
return output_path
|
| 107 |
|
| 108 |
errors = data.get("errors", []) if data else []
|
|
|
|
| 182 |
else:
|
| 183 |
raise RuntimeError("OpenAI returned no image content (no url or b64_json)")
|
| 184 |
|
| 185 |
+
if len(img_bytes) == 0:
|
| 186 |
+
raise RuntimeError("OpenAI returned empty image data")
|
| 187 |
+
|
| 188 |
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
|
| 189 |
with open(output_path, "wb") as f:
|
| 190 |
f.write(img_bytes)
|