Commit ·
6ac4df4
1
Parent(s): 98019c1
production audio files failure fixed
Browse files
backend/app/routers/projects.py
CHANGED
|
@@ -4544,11 +4544,16 @@ def _sync_audio_filenames_to_order(db: Session, project: Project) -> None:
|
|
| 4544 |
each other's audio — and a freshly generated ``scene_{order}.mp3`` can overwrite a
|
| 4545 |
file another scene still owns. This resyncs disk + DB to the current order.
|
| 4546 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4547 |
Call AFTER every scene's ``scene.order`` is set to its final value and flushed, and
|
| 4548 |
BEFORE generating any new voiceover for the current order (so the new file can't
|
| 4549 |
clobber an existing scene's audio). Renames go through a temp name first so a cyclic
|
| 4550 |
-
remap (e.g. 2↔3) can't overwrite a file that hasn't been moved yet. Best-effort:
|
| 4551 |
-
per-file failure is logged and skipped rather than aborting the whole operation.
|
| 4552 |
"""
|
| 4553 |
import re
|
| 4554 |
from app.models.asset import Asset, AssetType
|
|
@@ -4604,6 +4609,25 @@ def _sync_audio_filenames_to_order(db: Session, project: Project) -> None:
|
|
| 4604 |
except OSError as e:
|
| 4605 |
print(f"[PROJECTS] audio resync: stage failed for {old_filename}: {e}")
|
| 4606 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4607 |
for s, old_filename, new_filename, temp_path in staged:
|
| 4608 |
new_path = os.path.join(audio_dir, new_filename)
|
| 4609 |
if temp_path:
|
|
@@ -4616,19 +4640,38 @@ def _sync_audio_filenames_to_order(db: Session, project: Project) -> None:
|
|
| 4616 |
# Update the scene's stored path to the new filename.
|
| 4617 |
s.voiceover_path = new_path
|
| 4618 |
|
| 4619 |
-
#
|
| 4620 |
-
#
|
| 4621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4622 |
for a in assets_by_filename.get(old_filename, []):
|
| 4623 |
a.filename = new_filename
|
| 4624 |
a.local_path = new_path
|
| 4625 |
-
if
|
| 4626 |
-
|
| 4627 |
-
|
| 4628 |
-
|
| 4629 |
-
|
| 4630 |
-
|
| 4631 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4632 |
|
| 4633 |
db.flush()
|
| 4634 |
|
|
|
|
| 4544 |
each other's audio — and a freshly generated ``scene_{order}.mp3`` can overwrite a
|
| 4545 |
file another scene still owns. This resyncs disk + DB to the current order.
|
| 4546 |
|
| 4547 |
+
Playback resolves a scene's audio by FILENAME and then serves the matching Asset's
|
| 4548 |
+
``r2_url`` when R2 is configured, so a correct rename must move BOTH the local file
|
| 4549 |
+
AND the R2 object (and update ``r2_url``/``r2_key``) — renaming the local file alone
|
| 4550 |
+
leaves production serving stale remote audio under the old name.
|
| 4551 |
+
|
| 4552 |
Call AFTER every scene's ``scene.order`` is set to its final value and flushed, and
|
| 4553 |
BEFORE generating any new voiceover for the current order (so the new file can't
|
| 4554 |
clobber an existing scene's audio). Renames go through a temp name first so a cyclic
|
| 4555 |
+
remap (e.g. 2↔3) can't overwrite a file/object that hasn't been moved yet. Best-effort:
|
| 4556 |
+
a per-file failure is logged and skipped rather than aborting the whole operation.
|
| 4557 |
"""
|
| 4558 |
import re
|
| 4559 |
from app.models.asset import Asset, AssetType
|
|
|
|
| 4609 |
except OSError as e:
|
| 4610 |
print(f"[PROJECTS] audio resync: stage failed for {old_filename}: {e}")
|
| 4611 |
|
| 4612 |
+
# Two-phase R2 move: copy every source object to a UNIQUE temp key first, then copy
|
| 4613 |
+
# temp → final. This mirrors the local temp-rename so a cyclic remap can't overwrite an
|
| 4614 |
+
# object still owned by another scene. Only runs when R2 is configured.
|
| 4615 |
+
r2_on = False
|
| 4616 |
+
try:
|
| 4617 |
+
r2_on = r2_storage.is_r2_configured()
|
| 4618 |
+
except Exception:
|
| 4619 |
+
r2_on = False
|
| 4620 |
+
r2_tmp_keys: dict[str, str] = {} # old_filename -> temp key it was staged to
|
| 4621 |
+
if r2_on:
|
| 4622 |
+
tmp_prefix = f"tmp_reorder_{int(time.time() * 1000)}_"
|
| 4623 |
+
for s, old_filename, new_filename in plans:
|
| 4624 |
+
old_key = r2_storage.audio_key(project.user_id, project.id, old_filename)
|
| 4625 |
+
tmp_key = r2_storage.audio_key(
|
| 4626 |
+
project.user_id, project.id, tmp_prefix + old_filename
|
| 4627 |
+
)
|
| 4628 |
+
if r2_storage.copy_object(old_key, tmp_key) is not None:
|
| 4629 |
+
r2_tmp_keys[old_filename] = tmp_key
|
| 4630 |
+
|
| 4631 |
for s, old_filename, new_filename, temp_path in staged:
|
| 4632 |
new_path = os.path.join(audio_dir, new_filename)
|
| 4633 |
if temp_path:
|
|
|
|
| 4640 |
# Update the scene's stored path to the new filename.
|
| 4641 |
s.voiceover_path = new_path
|
| 4642 |
|
| 4643 |
+
# Move the R2 object (temp → final key) and capture the new public URL so playback,
|
| 4644 |
+
# which serves the Asset's r2_url, points at the right audio.
|
| 4645 |
+
new_r2_url: Optional[str] = None
|
| 4646 |
+
new_r2_key = r2_storage.audio_key(project.user_id, project.id, new_filename) if r2_on else None
|
| 4647 |
+
if r2_on and old_filename in r2_tmp_keys and new_r2_key:
|
| 4648 |
+
new_r2_url = r2_storage.copy_object(r2_tmp_keys[old_filename], new_r2_key)
|
| 4649 |
+
|
| 4650 |
+
# Update the matching Asset row(s): filename, local_path, and R2 key/url.
|
| 4651 |
for a in assets_by_filename.get(old_filename, []):
|
| 4652 |
a.filename = new_filename
|
| 4653 |
a.local_path = new_path
|
| 4654 |
+
if r2_on and new_r2_key and new_r2_url:
|
| 4655 |
+
a.r2_key = new_r2_key
|
| 4656 |
+
a.r2_url = new_r2_url
|
| 4657 |
+
|
| 4658 |
+
# Clean up the temp R2 objects and the now-orphaned OLD-named objects. An old key is
|
| 4659 |
+
# only safe to delete if no scene's FINAL filename still maps to it (i.e. it wasn't a
|
| 4660 |
+
# no-op destination). Since misnamed sources are all being remapped, delete each old
|
| 4661 |
+
# key unless it is also some scene's new_filename.
|
| 4662 |
+
if r2_on:
|
| 4663 |
+
final_filenames = {new_filename for (_s, _old, new_filename) in plans}
|
| 4664 |
+
# Also include filenames of scenes that did NOT need moving (already correct) so we
|
| 4665 |
+
# never delete an object a correctly-named scene still uses.
|
| 4666 |
+
for s in scenes:
|
| 4667 |
+
m = re.search(r"scene_(\d+)\.mp3", s.voiceover_path or "", re.IGNORECASE)
|
| 4668 |
+
if m:
|
| 4669 |
+
final_filenames.add(os.path.basename(s.voiceover_path))
|
| 4670 |
+
for old_filename, tmp_key in r2_tmp_keys.items():
|
| 4671 |
+
r2_storage.delete_object(tmp_key)
|
| 4672 |
+
if old_filename not in final_filenames:
|
| 4673 |
+
old_key = r2_storage.audio_key(project.user_id, project.id, old_filename)
|
| 4674 |
+
r2_storage.delete_object(old_key)
|
| 4675 |
|
| 4676 |
db.flush()
|
| 4677 |
|
backend/app/services/r2_storage.py
CHANGED
|
@@ -382,6 +382,31 @@ def delete_object(key: str) -> bool:
|
|
| 382 |
return False
|
| 383 |
|
| 384 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
def delete_project_files(user_id: int, project_id: int) -> int:
|
| 386 |
"""
|
| 387 |
Delete ALL R2 objects for a project (images, audio, video).
|
|
|
|
| 382 |
return False
|
| 383 |
|
| 384 |
|
| 385 |
+
def copy_object(src_key: str, dest_key: str) -> Optional[str]:
|
| 386 |
+
"""Server-side copy an R2 object from ``src_key`` to ``dest_key``.
|
| 387 |
+
|
| 388 |
+
Returns the public URL of the destination on success, or None on error /
|
| 389 |
+
when R2 is not configured / when the source object doesn't exist. Does not
|
| 390 |
+
delete the source — callers that want a move delete it afterwards.
|
| 391 |
+
"""
|
| 392 |
+
if not is_r2_configured():
|
| 393 |
+
return None
|
| 394 |
+
if src_key == dest_key:
|
| 395 |
+
return public_url(dest_key)
|
| 396 |
+
try:
|
| 397 |
+
client = _get_client()
|
| 398 |
+
client.copy_object(
|
| 399 |
+
Bucket=settings.R2_BUCKET_NAME,
|
| 400 |
+
CopySource={"Bucket": settings.R2_BUCKET_NAME, "Key": src_key},
|
| 401 |
+
Key=dest_key,
|
| 402 |
+
)
|
| 403 |
+
print(f"[R2] Copied {src_key} -> {dest_key}")
|
| 404 |
+
return public_url(dest_key)
|
| 405 |
+
except ClientError as e:
|
| 406 |
+
print(f"[R2] Failed to copy {src_key} -> {dest_key}: {e}")
|
| 407 |
+
return None
|
| 408 |
+
|
| 409 |
+
|
| 410 |
def delete_project_files(user_id: int, project_id: int) -> int:
|
| 411 |
"""
|
| 412 |
Delete ALL R2 objects for a project (images, audio, video).
|
backend/tests/test_depth_scene_editing.py
CHANGED
|
@@ -176,3 +176,74 @@ def test_sync_audio_filenames__cyclic_swap_no_clobber(db_session, paid_user):
|
|
| 176 |
import shutil
|
| 177 |
shutil.rmtree(os.path.join(settings.MEDIA_DIR, f"projects/{project.id}"),
|
| 178 |
ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
import shutil
|
| 177 |
shutil.rmtree(os.path.join(settings.MEDIA_DIR, f"projects/{project.id}"),
|
| 178 |
ignore_errors=True)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def test_sync_audio_filenames__moves_r2_object_and_updates_asset_url(
|
| 182 |
+
db_session, paid_user, monkeypatch
|
| 183 |
+
):
|
| 184 |
+
"""When R2 is configured, playback serves the Asset's r2_url — so the resync must
|
| 185 |
+
move the R2 object and update r2_url/r2_key, not just the local file."""
|
| 186 |
+
import os
|
| 187 |
+
from app.config import settings
|
| 188 |
+
from app.models.asset import Asset, AssetType
|
| 189 |
+
from app.routers import projects as projects_mod
|
| 190 |
+
from app.services import r2_storage
|
| 191 |
+
|
| 192 |
+
project, scenes = _project_with_scenes(db_session, paid_user, n=3)
|
| 193 |
+
s1, s2, s3 = scenes
|
| 194 |
+
for s in scenes:
|
| 195 |
+
_write_scene_audio(project.id, s, s.order)
|
| 196 |
+
|
| 197 |
+
# Fake R2: an in-memory object store keyed by R2 key, seeded with each scene's audio.
|
| 198 |
+
store: dict[str, bytes] = {}
|
| 199 |
+
for s in scenes:
|
| 200 |
+
key = f"users/{paid_user.id}/projects/{project.id}/audio/scene_{s.order}.mp3"
|
| 201 |
+
store[key] = f"R2-AUDIO-FOR-SCENE-{s.id}".encode()
|
| 202 |
+
|
| 203 |
+
def fake_audio_key(uid, pid, filename):
|
| 204 |
+
return f"users/{uid}/projects/{pid}/audio/{filename}"
|
| 205 |
+
|
| 206 |
+
def fake_copy(src, dest):
|
| 207 |
+
if src not in store:
|
| 208 |
+
return None
|
| 209 |
+
store[dest] = store[src]
|
| 210 |
+
return f"https://cdn.test/{dest}"
|
| 211 |
+
|
| 212 |
+
def fake_delete(key):
|
| 213 |
+
store.pop(key, None)
|
| 214 |
+
return True
|
| 215 |
+
|
| 216 |
+
monkeypatch.setattr(r2_storage, "is_r2_configured", lambda: True)
|
| 217 |
+
monkeypatch.setattr(r2_storage, "audio_key", fake_audio_key)
|
| 218 |
+
monkeypatch.setattr(r2_storage, "copy_object", fake_copy)
|
| 219 |
+
monkeypatch.setattr(r2_storage, "delete_object", fake_delete)
|
| 220 |
+
|
| 221 |
+
# Create Asset rows with r2_url, as production would have.
|
| 222 |
+
for s in scenes:
|
| 223 |
+
db_session.add(Asset(
|
| 224 |
+
project_id=project.id, asset_type=AssetType.AUDIO,
|
| 225 |
+
local_path=s.voiceover_path, filename=f"scene_{s.order}.mp3",
|
| 226 |
+
r2_key=fake_audio_key(paid_user.id, project.id, f"scene_{s.order}.mp3"),
|
| 227 |
+
r2_url=f"https://cdn.test/users/{paid_user.id}/projects/{project.id}/audio/scene_{s.order}.mp3",
|
| 228 |
+
))
|
| 229 |
+
db_session.commit()
|
| 230 |
+
|
| 231 |
+
# Cyclic swap: s2 -> order 3, s3 -> order 2.
|
| 232 |
+
s2.order, s3.order = 3, 2
|
| 233 |
+
db_session.flush()
|
| 234 |
+
|
| 235 |
+
projects_mod._sync_audio_filenames_to_order(db_session, project)
|
| 236 |
+
db_session.commit()
|
| 237 |
+
db_session.expire_all()
|
| 238 |
+
|
| 239 |
+
assets = {a.filename: a for a in db_session.query(Asset).filter_by(project_id=project.id)}
|
| 240 |
+
# The asset now named scene_3.mp3 must point at s2's R2 content; scene_2.mp3 at s3's.
|
| 241 |
+
assert assets["scene_3.mp3"].r2_url == f"https://cdn.test/{assets['scene_3.mp3'].r2_key}"
|
| 242 |
+
assert store[assets["scene_3.mp3"].r2_key] == f"R2-AUDIO-FOR-SCENE-{s2.id}".encode()
|
| 243 |
+
assert store[assets["scene_2.mp3"].r2_key] == f"R2-AUDIO-FOR-SCENE-{s3.id}".encode()
|
| 244 |
+
# Temp keys are cleaned up (no lingering tmp_reorder_ objects).
|
| 245 |
+
assert not any("tmp_reorder_" in k for k in store)
|
| 246 |
+
|
| 247 |
+
import shutil
|
| 248 |
+
shutil.rmtree(os.path.join(settings.MEDIA_DIR, f"projects/{project.id}"),
|
| 249 |
+
ignore_errors=True)
|