Add extension to image filename

This commit is contained in:
Rodrigo Agundez 2025-02-07 08:22:20 +09:00
parent 4974c9cbb0
commit 312f273a1b

View File

@ -3,6 +3,7 @@ import base64
import io import io
import json import json
import logging import logging
import mimetypes
import re import re
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
@ -411,15 +412,16 @@ def load_url_image_data(url, headers=None):
def upload_image(request, data, image_data, content_type, user): def upload_image(request, data, image_data, content_type, user):
image_format = mimetypes.guess_extension(content_type)
file = UploadFile( file = UploadFile(
file=io.BytesIO(image_data), file=io.BytesIO(image_data),
filename="image", # will be converted to a unique ID on upload_file filename=f"generated{image_format}", # will be converted to a unique ID on upload_file
headers={ headers={
"content-type": content_type, "content-type": content_type,
}, },
) )
file_item = upload_file(request, file, user) file_item = upload_file(request, file, user)
file_body_path = IMAGE_CACHE_DIR.joinpath(f"{file_item.id}.json") file_body_path = IMAGE_CACHE_DIR.joinpath(f"{file_item.filename}.json")
with open(file_body_path, "w") as f: with open(file_body_path, "w") as f:
json.dump(data, f) json.dump(data, f)