fix of the file upload in the edet_item funktions

This commit is contained in:
2026-08-01 18:35:28 +02:00
parent 2c6da44af8
commit df5a3265a1
+44 -32
View File
@@ -6097,19 +6097,19 @@ def edit_item(id):
current_permissions = us.get_effective_permissions(session['username']) current_permissions = us.get_effective_permissions(session['username'])
if not current_permissions['actions'].get('can_edit', False): if not current_permissions['actions'].get('can_edit', False):
flash('Ihnen fehlen die nötigen Berechtigungen, um diese Aktion (Löschen) auszuführen.', 'error') flash('Ihnen fehlen die nötigen Berechtigungen, um diese Aktion auszuführen.', 'error')
return redirect(url_for('home_admin')) return redirect(url_for('home_admin'))
if not cfg.MODULES.is_enabled('inventory'): if not cfg.MODULES.is_enabled('inventory'):
flash('Bibliotheks-Modul ist deaktiviert.', 'error') flash('Bibliotheks-Modul ist deaktiviert.', 'error')
return redirect(url_for('library_view')) return redirect(url_for('library_view'))
# Strip whitespace from all text fields fs = get_gridfs()
name = sanitize_form_value(request.form.get('name')) name = sanitize_form_value(request.form.get('name'))
ort = sanitize_form_value(request.form.get('ort')) ort = sanitize_form_value(request.form.get('ort'))
beschreibung = sanitize_form_value(request.form.get('beschreibung')) beschreibung = sanitize_form_value(request.form.get('beschreibung'))
# Strip whitespace from all filter values
filter1 = sanitize_form_value(request.form.getlist('filter')) filter1 = sanitize_form_value(request.form.getlist('filter'))
filter2 = sanitize_form_value(request.form.getlist('filter2')) filter2 = sanitize_form_value(request.form.getlist('filter2'))
filter3 = sanitize_form_value(request.form.getlist('filter3')) filter3 = sanitize_form_value(request.form.getlist('filter3'))
@@ -6134,63 +6134,75 @@ def edit_item(id):
if item_isbn: if item_isbn:
item_type = 'book' item_type = 'book'
# Check if code is unique (excluding the current item)
if code_4 and not it.is_code_unique(code_4, exclude_id=id): if code_4 and not it.is_code_unique(code_4, exclude_id=id):
flash('Der Code wird bereits verwendet. Bitte wählen Sie einen anderen Code.', 'error') flash('Der Code wird bereits verwendet. Bitte wählen Sie einen anderen Code.', 'error')
return redirect(url_for('home_admin')) return redirect(url_for('home_admin'))
# Get current item to check availability status
current_item = it.get_item(id) current_item = it.get_item(id)
if not current_item: if not current_item:
flash('Element nicht gefunden', 'error') flash('Element nicht gefunden', 'error')
return redirect(url_for('home_admin')) return redirect(url_for('home_admin'))
# Preserve current availability status
verfuegbar = current_item.get('Verfuegbar', True) verfuegbar = current_item.get('Verfuegbar', True)
# Handle existing images - get list of images to keep
images_to_keep = request.form.getlist('existing_images') images_to_keep = request.form.getlist('existing_images')
# Get the original list of images from the item
original_images = current_item.get('Images', []) original_images = current_item.get('Images', [])
# Keep only the images that weren't marked for deletion
images = [img for img in original_images if img in images_to_keep] images = [img for img in original_images if img in images_to_keep]
# Handle new image uploads
new_images = request.files.getlist('new_images') new_images = request.files.getlist('new_images')
# Process any new image uploads
for image in new_images: for image in new_images:
if image and image.filename: if image and image.filename:
is_allowed, error_message = allowed_file(image.filename) is_allowed, error_message = allowed_file(image.filename, image)
if is_allowed: if is_allowed:
# Get the file extension
_, ext_part = os.path.splitext(secure_filename(image.filename))
# Generate a completely unique filename using UUID
unique_id = str(uuid.uuid4())
timestamp = time.strftime("%Y%m%d%H%M%S")
# New filename format with UUID to ensure uniqueness
filename = f"{unique_id}_{timestamp}{ext_part}"
image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Optimize the image
try: try:
opt_result = generate_optimized_versions(filename, max_original_width=500, target_size_kb=80) secure_name = secure_filename(image.filename)
if opt_result['success'] and opt_result['original']:
filename = opt_result['original']
except Exception as e:
app.logger.error(f"Error optimizing image in edit_item: {e}")
images.append(filename) image.seek(0)
image_bytes = image.read()
if not image_bytes:
app.logger.error(f"Failed to read image in edit_item (0 bytes) for {secure_name}")
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 = 500
if img.width > max_width:
ratio = max_width / img.width
new_size = (max_width, int(img.height * ratio))
img = img.resize(new_size, 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,
'upload_context': 'edit_item',
'item_id': id
}
)
images.append(new_filename)
except Exception as e:
app.logger.error(f"Error processing new image in edit_item: {str(e)}")
else: else:
flash(error_message, 'error') flash(error_message, 'error')
return redirect(url_for('home_admin')) return redirect(url_for('home_admin'))
# If location is not in the predefined list, maybe add it (depending on policy)
predefined_locations = it.get_predefined_locations() predefined_locations = it.get_predefined_locations()
if ort and ort not in predefined_locations: if ort and ort not in predefined_locations:
it.add_predefined_location(ort) it.add_predefined_location(ort)