Thumbnails for images and videos #87

Merged
Anna Sirota merged 28 commits from thumbnails into main 2024-04-25 17:50:58 +02:00
2 changed files with 31 additions and 1 deletions
Showing only changes of commit db3d784cbf - Show all commits

View File

@ -1,3 +1,6 @@
from unittest.mock import patch, ANY
from pathlib import Path
from django.test import TestCase from django.test import TestCase
from files.utils import ( from files.utils import (
@ -5,8 +8,12 @@ from files.utils import (
find_exact_path, find_exact_path,
find_path_by_name, find_path_by_name,
get_thumbnail_upload_to, get_thumbnail_upload_to,
make_thumbnails,
) )
# Reusing test files from the extensions app
TEST_FILES_DIR = Path(__file__).resolve().parent.parent.parent / 'extensions' / 'tests' / 'files'
class UtilsTest(TestCase): class UtilsTest(TestCase):
manifest = 'blender_manifest.toml' manifest = 'blender_manifest.toml'
@ -112,3 +119,26 @@ class UtilsTest(TestCase):
): ):
with self.subTest(file_hash=file_hash, kwargs=kwargs): with self.subTest(file_hash=file_hash, kwargs=kwargs):
self.assertEqual(get_thumbnail_upload_to(file_hash, **kwargs), expected) self.assertEqual(get_thumbnail_upload_to(file_hash, **kwargs), expected)
@patch('files.utils.resize_image')
def test_make_thumbnails(self, mock_resize_image):
self.assertEqual(
{
'l': {'path': 'thumbnails/fo/foobar_1920x1080.png', 'size': (1920, 1080)},
's': {'path': 'thumbnails/fo/foobar_640x360.png', 'size': (640, 360)},
},
make_thumbnails(TEST_FILES_DIR / 'test_preview_image_0001.png', 'foobar'),
)
self.assertEqual(len(mock_resize_image.mock_calls), 2)
for expected_size in ((1920, 1080), (640, 360)):
with self.subTest(expected_size=expected_size):
mock_resize_image.assert_any_call(
ANY,
expected_size,
ANY,
output_format='PNG',
quality=83,
optimize=True,
progressive=True,
)

View File

@ -225,7 +225,7 @@ def resize_image(image: Image, size: tuple, output, output_format: str = 'PNG',
source_image.save(output, output_format, **output_params) source_image.save(output, output_format, **output_params)
def make_thumbnails(file_path: str, file_hash: str, output_format: str = THUMBNAIL_FORMAT) -> list: def make_thumbnails(file_path: str, file_hash: str, output_format: str = THUMBNAIL_FORMAT) -> dict:
"""Generate thumbnail files for given file and a predefined list of dimensions. """Generate thumbnail files for given file and a predefined list of dimensions.
Resulting thumbnail paths a derived from the given file hash and thumbnail sizes. Resulting thumbnail paths a derived from the given file hash and thumbnail sizes.