From d1858bb1edc7350d53e3ee0d03dfc25dbf68d6ae Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Fri, 25 Oct 2024 16:02:27 -0400 Subject: [PATCH] flatten ifs in catalog_controller image processing --- app/controllers/catalog_controller.rb | 35 ++++++++++++--------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 59aabdb56b7..ad26aab1f37 100644 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -534,31 +534,28 @@ def st_upload_image err = false identify_catalog(params[:id]) + image_file = params.dig(:upload, :image) if params[:pressed] @record.picture = nil @record.save msg = _("Custom Image successfully removed") - elsif params[:upload] && params[:upload][:image] && - params[:upload][:image].respond_to?(:read) - ext = params[:upload][:image].original_filename.split(".").last.downcase - if !valid_image_file?(params[:upload][:image]) - msg = _("Custom Image must be a .png or .jpg file") - err = true - else - picture = {:content => params[:upload][:image].read, - :extension => ext} - if @record.picture.nil? - @record.picture = Picture.new(picture) - else - @record.picture.update(picture) - end - @record.save - msg = _("Custom Image file \"%{name}\" successfully uploaded") % - {:name => params[:upload][:image].original_filename} - end - else + elsif !image_file&.respond_to?(:read) msg = _("Use the Choose file button to locate a .png or .jpg image file") err = true + elsif !valid_image_file?(image_file) + msg = _("Custom Image must be a .png or .jpg file") + err = true + else + ext = File.extname(image_file.original_filename).downcase + picture = {:content => image_file.read, :extension => ext} + if @record.picture.nil? + @record.picture = Picture.new(picture) + else + @record.picture.update(picture) + end + @record.save + msg = _("Custom Image file \"%{name}\" successfully uploaded") % + {:name => image_file.original_filename} end params[:id] = x_build_node_id(@record) # Get the tree node id add_flash(msg, err == true ? :error : nil)