diff --git a/README.md b/README.md index 248de09..6edba19 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ torrents and authentication. - [x] responsive ui - [x] automatically update metadata of shows - [x] automatically download new seasons/episodes of shows +- [x] add fallback to just copy files if hardlinks don't work - [ ] support for movies - [ ] expand README with more information and a quickstart guide - [ ] add notification system @@ -44,7 +45,6 @@ torrents and authentication. - [ ] make indexer module multithreaded - [ ] add support for deluge and transmission - [ ] improve reliability of scheduled tasks -- [ ] add fallback to just copy files if hardlinks don't work - [ ] _maybe_ rework the logo - [ ] _maybe_ add support for configuration via toml config file diff --git a/media_manager/main.py b/media_manager/main.py index a8093f0..21a6fd0 100644 --- a/media_manager/main.py +++ b/media_manager/main.py @@ -56,6 +56,7 @@ from media_manager.tv.service import ( # noqa: E402 ) from media_manager.config import BasicConfig # noqa: E402 +import shutil # noqa: E402 import media_manager.torrent.router as torrent_router # noqa: E402 from fastapi import FastAPI # noqa: E402 @@ -202,7 +203,7 @@ app.mount( log.info("Hello World!") -# Startup checks +# Startup filesystem checks try: test_dir = basic_config.tv_directory / Path(".media_manager_test_dir") test_dir.mkdir(parents=True, exist_ok=True) @@ -219,11 +220,6 @@ try: test_dir.unlink() log.info(f"Successfully created test file in Image directory at: {test_dir}") - test_dir = basic_config.image_directory / Path(".media_manager_test_dir") - test_dir.touch() - test_dir.unlink() - log.info(f"Successfully created test file in Image directory at: {test_dir}") - # check if hardlink creation works test_dir = basic_config.tv_directory / Path(".media_manager_test_dir") test_dir.mkdir(parents=True, exist_ok=True) @@ -235,14 +231,19 @@ try: test_torrent_file.touch() test_hardlink = test_dir / Path(".media_manager.test.hardlink") - test_hardlink.hardlink_to(test_torrent_file) - if not test_hardlink.samefile(test_torrent_file): - log.critical("Hardlink creation failed!") - - test_hardlink.unlink() - test_torrent_file.unlink() - torrent_dir.rmdir() - test_dir.rmdir() + try: + test_hardlink.hardlink_to(test_torrent_file) + if not test_hardlink.samefile(test_torrent_file): + log.critical("Hardlink creation failed!") + log.info("Successfully created test hardlink in TV directory") + except OSError as e: + log.error(f"Hardlink creation failed, falling back to copying files. Error: {e}") + shutil.copy(src=test_torrent_file, dst=test_hardlink) + finally: + test_hardlink.unlink() + test_torrent_file.unlink() + torrent_dir.rmdir() + test_dir.rmdir() except Exception as e: log.error(f"Error creating test directory: {e}") diff --git a/media_manager/torrent/utils.py b/media_manager/torrent/utils.py index b0bcfbb..ada4aa4 100644 --- a/media_manager/torrent/utils.py +++ b/media_manager/torrent/utils.py @@ -1,7 +1,7 @@ import logging import mimetypes from pathlib import Path - +import shutil import patoolib from media_manager.config import BasicConfig @@ -43,7 +43,14 @@ def get_torrent_filepath(torrent: Torrent): def import_file(target_file: Path, source_file: Path): if target_file.exists(): target_file.unlink() - target_file.hardlink_to(source_file) + try: + target_file.hardlink_to(source_file) + except OSError as e: + log.error( + f"Failed to create hardlink from {source_file} to {target_file}: {e}. " + "Falling back to copying the file." + ) + shutil.copy(src=source_file, dst=target_file) def import_torrent(torrent: Torrent) -> (list[Path], list[Path], list[Path]):