Name uploaded and downloaded files J-<id>.<ext>

Files added through the upload pipeline and Download to Library are
renamed to their J-ID right after indexing, so every new library file is
traceable by its identifier (scanned files keep their existing names).
index_file now returns the created location so callers can rename it;
the location record is updated to the new path.
This commit is contained in:
2026-09-17 11:56:01 -05:00
parent d4f5df2e4e
commit bf00cf36a2
4 changed files with 29 additions and 6 deletions
+20 -2
View File
@@ -41,7 +41,7 @@ def compute_md5(path):
def index_file(path, folder):
"""Index one file into MediaItem/MediaLocation.
Returns (item, created_item, created_location).
Returns (item, created_item, location, created_location).
"""
path = Path(path)
folder = Path(folder)
@@ -61,7 +61,25 @@ def index_file(path, folder):
"mtime": stat.st_mtime,
},
)
return item, created_item, created_location
return item, created_item, location, created_location
def rename_location_to_j_id(item, location):
"""Name a freshly indexed copy J-<id>.<ext> inside its own folder."""
path = Path(location.path)
if not path.exists():
return location
if path.stem == f"J-{item.id}":
return location
target = unique_destination(path.parent, f"J-{item.id}{path.suffix}")
path.rename(target)
parent = Path(location.rel_path).parent
location.path = str(target)
location.rel_path = (
str(parent / target.name) if str(parent) != "." else target.name
)
location.save(update_fields=["path", "rel_path"])
return location
def parse_tags(raw):