|
| 1 | +# Generated by Django 4.2.3 on 2023-09-29 14:27 |
| 2 | + |
| 3 | +from io import BytesIO |
| 4 | +from django.db import migrations |
| 5 | +import requests |
| 6 | +import webp |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from PIL import Image |
| 10 | + |
| 11 | +from files.service import SelectelSwiftStorage |
| 12 | + |
| 13 | + |
| 14 | +def convert_image_to_webp(pil_image: Image, quality: int) -> Optional[BytesIO]: |
| 15 | + """ |
| 16 | + Convert a PIL Image to WebP format. |
| 17 | +
|
| 18 | + Parameters: |
| 19 | + - pil_image (PIL.Image): A PIL Image object to be converted. |
| 20 | + - quality (int): The quality of the converted WebP image, 0 to 100. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + - bytes: The converted WebP image in bytes, or None if the conversion fails. |
| 24 | + """ |
| 25 | + try: |
| 26 | + config = webp.WebPConfig.new(preset=webp.WebPPreset.PHOTO, quality=quality) |
| 27 | + webp_data = webp.WebPPicture.from_pil(pil_image).encode(config=config) |
| 28 | + return BytesIO(webp_data.buffer()) |
| 29 | + except Exception as e: |
| 30 | + print(f"An error occurred: {e}") |
| 31 | + return None |
| 32 | + |
| 33 | + |
| 34 | +def migration(apps, schema_editor): |
| 35 | + UserFile = apps.get_model("files", "UserFile") |
| 36 | + files = UserFile.objects.all() |
| 37 | + storage = SelectelSwiftStorage() |
| 38 | + for i in files: |
| 39 | + # get all files that end in a .png or .jpg |
| 40 | + if i.link.endswith(".png") or i.link.endswith(".jpg"): |
| 41 | + old_link = i.link |
| 42 | + # convert .png/.jpg to .webp |
| 43 | + # download file and convert it to PIL image |
| 44 | + file = requests.get(i.link) |
| 45 | + pil_image = Image.open(BytesIO(file.content)) |
| 46 | + # make image be 512 by 512. cut coreners if needed |
| 47 | + # if hasattr(pil_image, '_getexif'): |
| 48 | + # # noinspection PyProtectedMember |
| 49 | + # orientation = pil_image._getexif().get(0x112, 1) |
| 50 | + # if orientation in (3, 6, 8): |
| 51 | + # pil_image = pil_image.rotate(90 * (orientation - 2)) |
| 52 | + |
| 53 | + pil_image.thumbnail((512, 512), Image.ANTIALIAS) |
| 54 | + # convert image to webp |
| 55 | + webp_file = convert_image_to_webp(pil_image, quality=80) |
| 56 | + # delete old file |
| 57 | + storage.delete(i.link) |
| 58 | + |
| 59 | + new_url = str(i.link).rsplit(".", 1)[0] + ".webp" |
| 60 | + token = storage._get_auth_token() |
| 61 | + requests.put( |
| 62 | + new_url, |
| 63 | + headers={ |
| 64 | + "X-Auth-Token": token, |
| 65 | + "Content-Type": "image/webp", |
| 66 | + }, |
| 67 | + data=webp_file, |
| 68 | + ) |
| 69 | + i.link = new_url |
| 70 | + i.extension = "webp" |
| 71 | + i.mime_type = "image/webp" |
| 72 | + i.save() |
| 73 | + # delete the one with the old url |
| 74 | + UserFile.objects.get(link=old_link).delete() |
| 75 | + |
| 76 | + |
| 77 | +class Migration(migrations.Migration): |
| 78 | + dependencies = [ |
| 79 | + ("files", "0006_alter_userfile_options"), |
| 80 | + ] |
| 81 | + |
| 82 | + operations = [ |
| 83 | + # FIXME |
| 84 | + # migrations.RunPython(migration, migrations.RunPython.noop), |
| 85 | + ] |
0 commit comments