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

@@ -497,6 +497,10 @@ class LocalLibraryDB:
cursor = self.connection.cursor()
# Update file hash in files table if present
if metadata.get('hash'):
cursor.execute("UPDATE files SET file_hash = ? WHERE id = ?", (metadata['hash'], file_id))
known_urls = metadata.get('known_urls', [])
if not isinstance(known_urls, str):
known_urls = json.dumps(known_urls)
@@ -534,6 +538,72 @@ class LocalLibraryDB:
except Exception as e:
logger.error(f"[save_metadata] ❌ Error saving metadata for {file_path}: {e}", exc_info=True)
raise
def save_file_info(self, file_path: Path, metadata: Dict[str, Any], tags: List[str]) -> None:
"""Save metadata and tags for a file in a single transaction."""
try:
str_path = str(file_path.resolve())
logger.debug(f"[save_file_info] Starting save for: {str_path}")
file_id = self.get_or_create_file_entry(file_path)
cursor = self.connection.cursor()
# Update file hash in files table if present
if metadata.get('hash'):
cursor.execute("UPDATE files SET file_hash = ? WHERE id = ?", (metadata['hash'], file_id))
# 1. Save Metadata
known_urls = metadata.get('known_urls', [])
if not isinstance(known_urls, str):
known_urls = json.dumps(known_urls)
relationships = metadata.get('relationships', [])
if not isinstance(relationships, str):
relationships = json.dumps(relationships)
cursor.execute("""
INSERT INTO metadata (
file_id, hash, known_urls, relationships,
duration, size, ext, media_type, media_kind,
time_imported, time_modified
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT(file_id) DO UPDATE SET
hash = excluded.hash,
known_urls = excluded.known_urls,
relationships = excluded.relationships,
duration = excluded.duration,
size = excluded.size,
ext = excluded.ext,
media_type = excluded.media_type,
media_kind = excluded.media_kind,
time_modified = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
""", (
file_id, metadata.get('hash'), known_urls, relationships,
metadata.get('duration'), metadata.get('size'), metadata.get('ext'),
metadata.get('media_type'), metadata.get('media_kind')
))
# 2. Save Tags
# We assume tags list is complete and includes title if needed
cursor.execute("DELETE FROM tags WHERE file_id = ?", (file_id,))
for tag in tags:
tag = tag.strip()
if tag:
cursor.execute("""
INSERT OR IGNORE INTO tags (file_id, tag, tag_type)
VALUES (?, ?, 'user')
""", (file_id, tag))
self.connection.commit()
logger.debug(f"[save_file_info] ✅ Committed metadata and tags for file_id {file_id}")
except Exception as e:
logger.error(f"[save_file_info] ❌ Error saving file info for {file_path}: {e}", exc_info=True)
raise
def get_tags(self, file_path: Path) -> List[str]:
"""Get all tags for a file."""
@@ -572,12 +642,15 @@ class LocalLibraryDB:
cursor.execute("DELETE FROM tags WHERE file_id = ?", (file_id,))
logger.debug(f"[save_tags] Deleted existing tags for file_id {file_id}")
if existing_title:
# Check if new tags provide a title
new_title_provided = any(str(t).strip().lower().startswith("title:") for t in tags)
if existing_title and not new_title_provided:
cursor.execute("""
INSERT INTO tags (file_id, tag, tag_type) VALUES (?, ?, 'user')
""", (file_id, existing_title[0]))
logger.debug(f"[save_tags] Preserved existing title tag")
else:
elif not existing_title and not new_title_provided:
filename_without_ext = file_path.stem
if filename_without_ext:
# Normalize underscores to spaces for consistency