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 4 additions and 3 deletions
Showing only changes of commit 4ff66ae241 - Show all commits

View File

@ -114,8 +114,8 @@ class UtilsTest(TestCase):
def test_get_thumbnail_upload_to(self):
for file_hash, kwargs, expected in (
('foobar', {}, 'thumbnails/fo/foobar.png'),
('deadbeaf', {'width': None, 'height': None}, 'thumbnails/de/deadbeaf.png'),
('deadbeaf', {'width': 640, 'height': 360}, 'thumbnails/de/deadbeaf_640x360.png'),
('deadbeef', {'width': None, 'height': None}, 'thumbnails/de/deadbeef.png'),
('deadbeef', {'width': 640, 'height': 360}, 'thumbnails/de/deadbeef_640x360.png'),
):
with self.subTest(file_hash=file_hash, kwargs=kwargs):
self.assertEqual(get_thumbnail_upload_to(file_hash, **kwargs), expected)

View File

@ -232,12 +232,12 @@ def make_thumbnails(file_path: str, file_hash: str, output_format: str = THUMBNA
Return a dict of size keys to output paths of generated thumbnail images.
"""
thumbnails = {}
image = Image.open(file_path)
for size_key, (w, h) in THUMBNAIL_SIZES.items():
output_path = get_thumbnail_upload_to(file_hash, width=w, height=h)
size = (w, h)
with tempfile.TemporaryFile() as f:
logger.info('Resizing %s to %s (%s)', file_path, size, output_format)
image = Image.open(file_path)
resize_image(
image,
size,
@ -254,6 +254,7 @@ def make_thumbnails(file_path: str, file_hash: str, output_format: str = THUMBNA
default_storage.delete(output_path)
default_storage.save(output_path, f)
thumbnails[size_key] = {'size': size, 'path': output_path}
image.close()
return thumbnails