This commit is contained in:
nose
2025-12-01 01:10:16 -08:00
parent 2b93edac10
commit 6b9ed7d4ab
17 changed files with 1644 additions and 470 deletions

View File

@@ -169,26 +169,14 @@ def _persist_local_metadata(
debug(f"[_persist_local_metadata] Absolute dest_path: {dest_path.resolve()}")
with LocalLibraryDB(library_root) as db:
# Save metadata FIRST to ensure file entry is created in DB
if any(payload.values()):
debug(f"[_persist_local_metadata] Saving metadata payload first")
try:
db.save_metadata(dest_path, payload)
debug(f"[_persist_local_metadata] ✅ Metadata saved")
except Exception as meta_exc:
log(f"[_persist_local_metadata] ❌ Failed to save metadata: {meta_exc}", file=sys.stderr)
raise
# Save tags to DB synchronously in same transaction
# For local storage, DB is the primary source of truth
if tags:
try:
debug(f"[_persist_local_metadata] Saving {len(tags)} tags to DB")
db.save_tags(dest_path, tags)
debug(f"[_persist_local_metadata] ✅ Tags saved to DB")
except Exception as tag_exc:
log(f"[_persist_local_metadata] ⚠️ Failed to save tags to DB: {tag_exc}", file=sys.stderr)
raise
# Use optimized single-transaction save
debug(f"[_persist_local_metadata] Saving metadata and {len(tags)} tags to DB")
try:
db.save_file_info(dest_path, payload, tags)
debug(f"[_persist_local_metadata] ✅ File info saved to DB")
except Exception as exc:
log(f"[_persist_local_metadata] ❌ Failed to save file info: {exc}", file=sys.stderr)
raise
# NOTE: Sidecar files are intentionally NOT created for local storage
# Local storage uses database as primary source, not sidecar files
@@ -261,6 +249,26 @@ def _handle_local_transfer(media_path: Path, destination_root: Path, result: Any
relationships = extract_relationships(result)
duration = extract_duration(result)
# Rename source file if title tag is present (to ensure destination has correct name)
title_tag = next((t for t in merged_tags if str(t).strip().lower().startswith("title:")), None)
if title_tag:
try:
from helper.utils import unique_path
title_val = title_tag.split(":", 1)[1].strip()
# Sanitize filename (keep spaces, but remove illegal chars)
safe_title = "".join(c for c in title_val if c.isalnum() or c in " ._-()[]").strip()
if safe_title:
new_name = safe_title + media_path.suffix
new_path = media_path.parent / new_name
if new_path != media_path:
# Ensure we don't overwrite existing files
new_path = unique_path(new_path)
media_path.rename(new_path)
media_path = new_path
debug(f"Renamed source file to match title: {media_path.name}")
except Exception as e:
log(f"Warning: Failed to rename file to match title: {e}", file=sys.stderr)
try:
dest_file = storage["local"].upload(media_path, location=str(destination_root), move=True)
except Exception as exc:
@@ -271,14 +279,16 @@ def _handle_local_transfer(media_path: Path, destination_root: Path, result: Any
file_hash = _resolve_file_hash(result, sidecar_hash, dest_path)
media_kind = _resolve_media_kind(result, dest_path)
# Ensure only ONE title tag that matches the actual filename
# Remove all existing title tags and add one based on the saved filename
merged_tags_no_titles = [t for t in merged_tags if not str(t).strip().lower().startswith("title:")]
filename_title = dest_path.stem.replace("_", " ").strip()
if filename_title:
merged_tags_no_titles.insert(0, f"title:{filename_title}")
# If we have a title tag, keep it. Otherwise, derive from filename.
has_title = any(str(t).strip().lower().startswith("title:") for t in merged_tags)
final_tags = merged_tags
_persist_local_metadata(destination_root, dest_path, merged_tags_no_titles, merged_urls, file_hash, relationships, duration, media_kind)
if not has_title:
filename_title = dest_path.stem.replace("_", " ").strip()
if filename_title:
final_tags.insert(0, f"title:{filename_title}")
_persist_local_metadata(destination_root, dest_path, final_tags, merged_urls, file_hash, relationships, duration, media_kind)
_cleanup_sidecar_files(media_path, sidecar_path)
debug(f"✅ Moved to local library: {dest_path}")
return 0, dest_path
@@ -897,8 +907,11 @@ def _run(result: Any, _args: Sequence[str], config: Dict[str, Any]) -> int:
pass
# If -delete flag is set, delete the file and .tags after successful upload
if delete_after_upload:
log(f"Deleting local files (as requested)...", file=sys.stderr)
# Also delete if the file is a temporary file from merge-file (contains .dlhx_ or (merged))
is_temp_merge = "(merged)" in media_path.name or ".dlhx_" in media_path.name
if delete_after_upload or is_temp_merge:
log(f"Deleting local files (as requested or temp file)...", file=sys.stderr)
try:
media_path.unlink()
log(f"✅ Deleted: {media_path.name}", file=sys.stderr)