Files
cbwebreader/comic/management/commands/scan_comics.py
T
ajurna ce809ed1a9 **Refactor directory scanning and add scan task support**
- Added a new `scan_directory` task to enable folder scans as asynchronous tasks while maintaining compatibility with the immediate backend.
- Introduced the `reconcile` function, replacing `generate_directory` for improved separation of concerns between database writes and read operations.
- Established a unique constraint on `Directory` (`one_directory_name_per_parent`) to prevent duplicate directories under the same parent.
- Added a "Scan" button to the frontend for re-reading folders from disk, along with new task invocation endpoints in the API.
- Updated tests across modules to validate the new `scan_directory` task, unique constraints, and database indexing logic.
- Improved cleanup logic for stale files, directories, and thumbnails to handle concurrent operations efficiently.
- Simplified `scan_comics` management command by removing user dependency and aligning it with the new `reconcile` function.
- Enhanced frontend components (`TheComicReader`, `TheComicList`, and others) to include lifecycle method cleanups, prevent memory leaks, and handle task-based scans.
2026-10-13 15:58:06 +01:00

38 lines
1.1 KiB
Python

from pathlib import Path
from django.conf import settings
from django.core.management.base import BaseCommand, CommandParser
from loguru import logger
from comic.models import Directory
from comic.processing import reconcile
class Command(BaseCommand):
help = "Scan directories to Update Comic DB"
def __init__(self) -> None:
super().__init__()
self.OUTPUT = False
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument(
'--out',
action='store_true',
help='Output to console',
)
def handle(self, *args, **options) -> None:
self.OUTPUT = options.get('out', False)
self.scan_directory()
def scan_directory(self, directory: Directory | None = None) -> None:
reconcile(directory)
path = Path(settings.COMIC_BOOK_VOLUME, directory.path) if directory else settings.COMIC_BOOK_VOLUME
if self.OUTPUT:
self.stdout.write(str(path))
else:
logger.info(path)
for child in Directory.objects.filter(parent=directory):
self.scan_directory(child)