Files
NibasaViewer/viewer/management/commands/makethumbnails.py
2023-07-30 19:07:32 -04:00

45 lines
1.5 KiB
Python

# Standard library imports.
from pathlib import Path
# External library imports.
import filetype
from PIL import Image
# Django imports.
from django.core.management.base import BaseCommand
from django.conf import settings
# Project imports.
from viewer.utils import make_thumbnail
###########################################################################################
# Command subclass. #
###########################################################################################
class Command(BaseCommand):
"""
Assorted data fix and clean up commands for the Meters module.
"""
def _make_missing_thumbnails(self, directory):
"""
Goes through the GALLERY_ROOT directory recursively and creates a new thumbnail
for each image that does not have a corresponding file (same name) in the THUMBNAILS_ROOT
directory.
"""
# Make a thumbnail for each file in the current directory.
for image in [i for i in directory.iterdir() if i.is_file() and filetype.is_image(str(i))]:
make_thumbnail(image)
# Handle each sub-directory recursively.
for subdir in [i for i in directory.iterdir() if i.is_dir()]:
self._make_missing_thumbnails(subdir)
def handle(self, *args, **options):
try:
self._make_missing_thumbnails(settings.GALLERY_ROOT)
except KeyboardInterrupt as e:
self.stderr.write(self.style.ERROR('\nInterrupted'))