diff --git a/viewer/management/commands/makethumbnails.py b/viewer/management/commands/makethumbnails.py index 847aa33..a4e09bb 100644 --- a/viewer/management/commands/makethumbnails.py +++ b/viewer/management/commands/makethumbnails.py @@ -1,12 +1,9 @@ -# External library imports. -import filetype - # Django imports. from django.core.management.base import BaseCommand from django.conf import settings # Project imports. -from viewer.utils import make_thumbnail +from viewer.utils import make_thumbnail, is_image_file ########################################################################################### # Command subclass. # @@ -26,7 +23,7 @@ class Command(BaseCommand): """ # 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))]: + for image in [i for i in directory.iterdir() if i.is_file() and is_image_file(i)]: make_thumbnail(image) # Handle each sub-directory recursively. diff --git a/viewer/utils.py b/viewer/utils.py index bafe2ad..3ae0a2b 100644 --- a/viewer/utils.py +++ b/viewer/utils.py @@ -13,11 +13,26 @@ from django.conf import settings THUMB_SIZE = (128, 128) +# Supported image extensions for fast file filtering (avoids costly MIME-type detection) +IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.tiff', '.tif', '.svg'} + ########################################################################################### # Helper functions. # ########################################################################################### +def is_image_file(path): + """ + Fast image detection using file extension instead of MIME-type checking. + + :param path: A pathlib.Path instance or string path to check. + :return: True if the file extension matches a known image format. + """ + if isinstance(path, str): + path = Path(path) + return path.suffix.lower() in IMAGE_EXTENSIONS + + def make_thumbnail(image): """ Creates a thumbnail in the corresponding THUMBNAILS_ROOT directory for the given image. diff --git a/viewer/views.py b/viewer/views.py index 8faee89..75b2770 100644 --- a/viewer/views.py +++ b/viewer/views.py @@ -2,9 +2,6 @@ from pathlib import Path from math import ceil -# External library imports. -import filetype - # Django imports. from django.http import HttpResponseNotFound from django.conf import settings @@ -14,7 +11,7 @@ from django.shortcuts import (render, redirect) # Project imports. -from .utils import make_thumbnail +from .utils import make_thumbnail, is_image_file ########################################################################################### # CONSTANTS. # @@ -98,14 +95,14 @@ def gallery_view(request, path = None): if search == '': # If there is no search query then get all images and sub-directories of the current path. subdirs = sorted([i for i in full_path.iterdir() if i.is_dir()]) - images = sorted([i for i in full_path.iterdir() if i.is_file() and filetype.is_image(str(i))]) + images = sorted([i for i in full_path.iterdir() if i.is_file() and is_image_file(i)]) else: # If there is a search query then search the current directory recursively. subdirs, images = do_recursive_search(full_path, search) # Only keep image files. - images = [image for image in images if filetype.is_image(str(image))] + images = [image for image in images if is_image_file(image)] # For every sub-directory found, prepare it's name and path for rendering. subdir_data = [] @@ -165,7 +162,7 @@ def gallery_view(request, path = None): img_dir = settings.GALLERY_ROOT.joinpath(path).parent # Then get all sibling images. - images = sorted([i.name for i in img_dir.iterdir() if i.is_file() and filetype.is_image(str(i))]) + images = sorted([i.name for i in img_dir.iterdir() if i.is_file() and is_image_file(i)]) # Get the current image's index in it's directory. index = images.index(image.name)