diff --git a/backend/apps/library/management/commands/scan_files.py b/backend/apps/library/management/commands/scan_files.py index 58fe4ad..4fce7cc 100644 --- a/backend/apps/library/management/commands/scan_files.py +++ b/backend/apps/library/management/commands/scan_files.py @@ -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}") diff --git a/backend/apps/library/services.py b/backend/apps/library/services.py index f92955d..f42d3cf 100644 --- a/backend/apps/library/services.py +++ b/backend/apps/library/services.py @@ -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-. 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): diff --git a/backend/apps/library/uploads.py b/backend/apps/library/uploads.py index 6480e25..df20318 100644 --- a/backend/apps/library/uploads.py +++ b/backend/apps/library/uploads.py @@ -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 diff --git a/backend/apps/library/views.py b/backend/apps/library/views.py index 09a2d2d..475edd0 100644 --- a/backend/apps/library/views.py +++ b/backend/apps/library/views.py @@ -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,