working, images broken
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
# plugins/media/routes.py
|
||||
|
||||
import os
|
||||
from uuid import uuid4
|
||||
import uuid
|
||||
from werkzeug.utils import secure_filename
|
||||
from datetime import datetime
|
||||
from PIL import Image, ExifTags
|
||||
|
||||
@ -73,48 +74,40 @@ def _strip_exif(image: Image.Image) -> Image.Image:
|
||||
return image
|
||||
|
||||
|
||||
def _process_upload_file(
|
||||
file,
|
||||
uploader_id: int,
|
||||
plugin: str,
|
||||
related_id: int
|
||||
):
|
||||
"""
|
||||
Save the uploaded image (strip EXIF), write Media row with
|
||||
file_url, and return the Media instance.
|
||||
"""
|
||||
ext = os.path.splitext(file.filename)[1].lower()
|
||||
if ext not in {".jpg", ".jpeg", ".png", ".gif", ".webp"}:
|
||||
raise ValueError("Unsupported file type.")
|
||||
def _process_upload_file(file, uploader_id, plugin='', related_id=0, plant_id=None, growlog_id=None, caption=None):
|
||||
"""Handles saving an uploaded file and creating the Media record."""
|
||||
|
||||
# generate a stable filename
|
||||
filename = f"{uuid4().hex}{ext}"
|
||||
|
||||
# determine disk path
|
||||
abs_dir, subdir = get_upload_path(plugin, related_id)
|
||||
full_path = os.path.join(abs_dir, filename)
|
||||
|
||||
# strip EXIF and save
|
||||
img = Image.open(file)
|
||||
img = _strip_exif(img)
|
||||
img.save(full_path)
|
||||
|
||||
# create the DB record
|
||||
# Generate a unique filename
|
||||
now = datetime.utcnow()
|
||||
media = Media(
|
||||
uploader_id=uploader_id,
|
||||
file_url=f"{subdir}/{filename}",
|
||||
uploaded_at=now
|
||||
unique_id = str(uuid.uuid4()).replace("-", "")
|
||||
secure_name = secure_filename(file.filename)
|
||||
filename = f"{unique_id}_{secure_name}"
|
||||
|
||||
# Construct the save path
|
||||
storage_path = os.path.join(
|
||||
current_app.config['UPLOAD_FOLDER'],
|
||||
str(uploader_id),
|
||||
now.strftime('%Y/%m/%d')
|
||||
)
|
||||
os.makedirs(storage_path, exist_ok=True)
|
||||
|
||||
# legacy relationships
|
||||
if plugin == "plant":
|
||||
media.plant_id = related_id
|
||||
elif plugin == "growlog":
|
||||
media.growlog_id = related_id
|
||||
full_path = os.path.join(storage_path, filename)
|
||||
file.save(full_path)
|
||||
|
||||
db.session.add(media)
|
||||
db.session.commit()
|
||||
file_url = f"/{uploader_id}/{now.strftime('%Y/%m/%d')}/{filename}"
|
||||
|
||||
media = Media(
|
||||
plugin=plugin,
|
||||
related_id=related_id,
|
||||
filename=filename,
|
||||
uploaded_at=now,
|
||||
uploader_id=uploader_id,
|
||||
caption=caption,
|
||||
plant_id=plant_id,
|
||||
growlog_id=growlog_id,
|
||||
created_at=now,
|
||||
file_url=file_url
|
||||
)
|
||||
return media
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user