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:
@@ -67,7 +67,9 @@ class Command(BaseCommand):
|
||||
continue
|
||||
path = Path(root) / name
|
||||
try:
|
||||
item, created_item, created_location = index_file(path, folder)
|
||||
item, created_item, _location, created_location = index_file(
|
||||
path, folder
|
||||
)
|
||||
except OSError as exc:
|
||||
skipped += 1
|
||||
self.stderr.write(f"Skipped {path}: {exc}")
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -55,7 +55,8 @@ def complete_temp_upload(temp, download_url=None):
|
||||
except Exception:
|
||||
destination.unlink(missing_ok=True)
|
||||
raise
|
||||
item, _, _ = services.index_file(destination, folder)
|
||||
item, _, location, _ = services.index_file(destination, folder)
|
||||
services.rename_location_to_j_id(item, location)
|
||||
if temp.file:
|
||||
temp.file.delete(save=False)
|
||||
else:
|
||||
@@ -66,7 +67,8 @@ def complete_temp_upload(temp, download_url=None):
|
||||
)
|
||||
with temp.file.open("rb") as source, open(destination, "wb") as target:
|
||||
shutil.copyfileobj(source, target)
|
||||
item, _, _ = services.index_file(destination, folder)
|
||||
item, _, location, _ = services.index_file(destination, folder)
|
||||
services.rename_location_to_j_id(item, location)
|
||||
temp.file.delete(save=False)
|
||||
|
||||
temp.library_item = item
|
||||
|
||||
@@ -212,7 +212,7 @@ class DownloadToLibraryView(APIView):
|
||||
destination = services.unique_destination(folder, name)
|
||||
try:
|
||||
services.download_file(url, destination)
|
||||
item, _, _ = services.index_file(destination, folder)
|
||||
item, _, location, _ = services.index_file(destination, folder)
|
||||
except Exception as exc: # noqa: BLE001 - report download failures
|
||||
destination.unlink(missing_ok=True)
|
||||
return Response(
|
||||
@@ -239,6 +239,7 @@ class DownloadToLibraryView(APIView):
|
||||
|
||||
if update_fields:
|
||||
item.save(update_fields=update_fields + ["updated_at"])
|
||||
services.rename_location_to_j_id(item, location)
|
||||
return Response(
|
||||
MediaItemSerializer(item, context={"request": request}).data,
|
||||
status=status.HTTP_201_CREATED,
|
||||
|
||||
Reference in New Issue
Block a user