Offline check ser ok ud.
This commit is contained in:
@@ -50,10 +50,12 @@ def scan_library(library_id: int, library_path: str, db_path: str,
|
||||
# Byg indeks over kendte filer
|
||||
known = {}
|
||||
for row in conn.execute(
|
||||
"SELECT local_path, file_modified_at FROM songs WHERE library_id=?",
|
||||
"SELECT local_path, file_modified_at, file_missing FROM songs WHERE library_id=?",
|
||||
(library_id,)
|
||||
).fetchall():
|
||||
known[row["local_path"]] = row["file_modified_at"]
|
||||
# Sange markeret som manglende medtages ikke i known — de skal altid genscanes
|
||||
if not row["file_missing"]:
|
||||
known[row["local_path"]] = row["file_modified_at"]
|
||||
|
||||
# Find alle musikfiler
|
||||
all_files = []
|
||||
@@ -87,10 +89,44 @@ def scan_library(library_id: int, library_path: str, db_path: str,
|
||||
tags = read_tags(fp)
|
||||
extra = json.dumps(tags.get("extra_tags", {}), ensure_ascii=False)
|
||||
|
||||
# Match 1: præcis sti-match
|
||||
existing = conn.execute(
|
||||
"SELECT id, bpm FROM songs WHERE local_path=?", (path_str,)
|
||||
).fetchone()
|
||||
|
||||
# Match 2: titel+artist match — fil er flyttet eller var missing
|
||||
if not existing:
|
||||
title = tags.get("title", "")
|
||||
artist = tags.get("artist", "")
|
||||
if title:
|
||||
# Prioritér file_missing=1 sange, men tag også sange med ugyldig sti
|
||||
existing = conn.execute("""
|
||||
SELECT id, bpm FROM songs
|
||||
WHERE title=? AND artist=? AND file_missing=1
|
||||
LIMIT 1
|
||||
""", (title, artist)).fetchone()
|
||||
if not existing:
|
||||
# Tjek om der er en sang med samme titel+artist men ugyldig sti
|
||||
existing = conn.execute("""
|
||||
SELECT id, bpm, local_path FROM songs
|
||||
WHERE title=? AND artist=? AND file_missing=0
|
||||
LIMIT 1
|
||||
""", (title, artist)).fetchone()
|
||||
if existing:
|
||||
from pathlib import Path as _Path
|
||||
old_path = existing["local_path"] or ""
|
||||
if old_path and not _Path(old_path).exists():
|
||||
pass # Sti er ugyldig — brug dette match
|
||||
else:
|
||||
existing = None # Sti er valid — det er en anden fil
|
||||
|
||||
if existing:
|
||||
# Opdater stien så den peger på den nye placering
|
||||
conn.execute(
|
||||
"UPDATE songs SET local_path=? WHERE id=?",
|
||||
(path_str, existing["id"])
|
||||
)
|
||||
|
||||
if existing:
|
||||
bpm = tags.get("bpm", 0)
|
||||
if not overwrite_bpm and existing["bpm"] and existing["bpm"] > 0:
|
||||
|
||||
Reference in New Issue
Block a user