From 1af2a2be06308bdb3bd75741d013b7624b9d0393 Mon Sep 17 00:00:00 2001 From: AIIrondev Date: Wed, 12 Aug 2026 00:32:00 +0200 Subject: [PATCH] implementation of the image --- Web/app.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/Web/app.py b/Web/app.py index 5cd6251..cb4cd44 100755 --- a/Web/app.py +++ b/Web/app.py @@ -6471,6 +6471,54 @@ def item_edit(id): db_instance = client[cfg.MONGODB_DB] items_col = db_instance['items'] + # Bilder verarbeiten (GridFS) + images_to_keep = request.form.getlist('existing_images') + original_images = current_item.get('Images', []) + images = [img for img in original_images if img in images_to_keep] + + new_files = request.files.getlist('images') + if new_files and new_files[0].filename != '': + fs = get_gridfs() + for file in new_files: + if file and file.filename: + is_allowed, error_msg = allowed_file(file.filename, file) + if not is_allowed: + flash(error_msg, 'error') + return redirect(redirect_target) + + try: + secure_name = secure_filename(file.filename) + file.seek(0) + image_bytes = file.read() + + if not image_bytes: + continue + + optimized_io = io.BytesIO() + with Image.open(io.BytesIO(image_bytes)) as img: + if img.mode not in ('RGB', 'RGBA'): + img = img.convert('RGBA') + + max_width = 800 + if img.width > max_width: + ratio = max_width / img.width + img = img.resize((max_width, int(img.height * ratio)), Image.Resampling.LANCZOS) + + img.save(optimized_io, format='WEBP', quality=85, optimize=True) + + optimized_io.seek(0) + new_filename = f"{uuid.uuid4().hex}_{int(time.time())}.webp" + + fs.put( + optimized_io, + filename=new_filename, + content_type='image/webp', + metadata={'original_filename': secure_name, 'item_id': str(id)} + ) + images.append(new_filename) + except Exception as e: + app.logger.error(f"Bild-Fehler bei Item {id}: {e}") + has_code_error = False for code in all_codes_to_check: if not code: @@ -6497,7 +6545,7 @@ def item_edit(id): name=name, ort=ort, beschreibung=beschreibung, - #images=images, + images=images, verfuegbar=verfuegbar, filter1=filter1, filter2=filter2,