Class: ImagesController
- Inherits:
-
CrmController
- Object
- ActionController::Base
- ApplicationController
- CrmController
- ImagesController
- Includes:
- Controllers::RequireTurboFrame
- Defined in:
- app/controllers/images_controller.rb
Overview
Controller: images.
Constant Summary collapse
- TAB_ACTIONS =
Tab fragments never render standalone — non-frame hits redirect to show ?tab=.
%i[tab_file_info tab_embeddings tab_items tab_profiles tab_videos tab_duplicates].freeze
Constants included from Controllers::ReferenceFindable
Controllers::ReferenceFindable::ID_EMBEDDED_PATTERNS
Constants included from Controllers::AnalyticsEvents
Controllers::AnalyticsEvents::MAX_QUEUED_EVENTS, Controllers::AnalyticsEvents::SESSION_KEY
Constants included from Controllers::ErrorRendering
Controllers::ErrorRendering::NON_CONTENT_PATH_PREFIXES
Instance Method Summary collapse
-
#add_image_profile ⇒ void
Assigns this image as an item's profile of a given image type, replacing any existing profile of that type.
-
#ai_metadata_suggestions ⇒ Object
GET /images/:id/ai_metadata_suggestions Review AI-generated metadata suggestions and selectively apply them.
-
#analyze_with_vision ⇒ void
Queues an
ImageVisionWorkerjob to analyze the image with vision AI. -
#apply_ai_metadata ⇒ Object
PATCH /images/:id/apply_ai_metadata Apply selected suggestion fields to the image record.
-
#apply_upscale ⇒ Object
POST /images/:id/apply_upscale Queue the upscale worker in propose mode, then redirect to the job status page.
-
#check_duplicates ⇒ Object
Check for duplicate images during upload Computes pHash server-side using phash-rb gem for consistency with stored fingerprints.
-
#clone ⇒ void
Clones an image's data into a new unsaved record and renders the new form.
-
#confirm_upscale ⇒ Object
GET /images/:id/confirm_upscale?proposal_id= Show the compare slider so the user can accept or reject the upscaled proposal.
-
#confirm_upscale_submit ⇒ Object
POST /images/:id/confirm_upscale Accept (promote) or reject the upscaled proposal.
-
#create ⇒ Object
POST /images POST /images.json.
-
#create_multi ⇒ void
Creates one
Imageper uploaded ImageKit file from the multi-upload form. -
#destroy ⇒ void
Destroys the image.
-
#duplicates ⇒ Object
Find duplicate images using pHash fingerprints Uses PostgreSQL bit_count() for efficient Hamming distance calculation.
-
#edit ⇒ Object
GET /images/1/edit.
-
#edit_image_profiles ⇒ void
Renders the image profiles edit form.
-
#edit_multi ⇒ void
Renders the bulk edit form for the selected images.
-
#find_similar ⇒ Object
GET /images/:id/find_similar Find visually similar images using unified embeddings (Gemini Embedding 2).
-
#full_ai_analysis ⇒ void
Queues an
ImageFullAnalysisWorkerjob, reusing the running job if one exists. -
#generate_ai_metadata ⇒ Object
POST /images/:id/generate_ai_metadata Enqueue AI metadata suggestion generation for an existing library image.
-
#generate_fingerprint ⇒ void
Queues an
ImageFingerprintWorkerjob to compute the image's pHash fingerprint. -
#get_forms ⇒ Object
method for wyimage redactor plugin to pull partial of image library filter form and upload form.
-
#get_image_url ⇒ Object
get the image url for the given image, using the options specified.
-
#index ⇒ Object
GET /images GET /images.json.
-
#info_popover ⇒ Object
Lazy-loaded body for the image-library info popover.
-
#lightbox_duplicates ⇒ Object
Lightbox view for duplicate image preview.
-
#make_primary_item_image ⇒ void
Marks the image as the primary item image for its linked (or selected) items.
-
#merge_duplicates ⇒ Object
Merge duplicate images - transfer all references from source to target.
-
#merge_preview ⇒ Object
Full-page merge preview with all options.
-
#new ⇒ Object
GET /images/new GET /images/new.json.
-
#new_multi ⇒ void
Renders the multi-image upload form.
-
#pagination ⇒ void
Renders a paginated page of the image library with selection context.
-
#purge_cache ⇒ void
Requests a CDN cache purge for the image.
-
#refresh_embedding ⇒ void
Regenerates the image's unified Gemini embedding (text or image+text).
-
#refresh_imagekit_metadata ⇒ Object
Refresh ImageKit metadata for an image Fetches the latest metadata from ImageKit API If ImageKit has a pHash and we don't have a fingerprint, copies it to fingerprint column.
-
#remove_image_profile ⇒ void
Removes an image profile assignment from this image.
-
#show ⇒ void
Shows a single image.
-
#tab_duplicates ⇒ Object
Find potential duplicate images for this specific image using pHash fingerprint Lazy-loaded tab on the image show page.
-
#tab_embeddings ⇒ void
Renders the AI embeddings tab fragment.
-
#tab_file_info ⇒ void
Renders the file info tab fragment.
-
#tab_items ⇒ void
Renders the items tab fragment with the image's linked items, paginated.
-
#tab_profiles ⇒ void
Renders the image profiles tab fragment.
-
#tab_videos ⇒ void
Renders the videos tab fragment with videos using this image as poster.
-
#tags_lookup ⇒ Object
GET /images/tags_lookup Remote autocomplete for the filter panel's tag pickers — SQL-filtered so the page never has to embed all ~4k image tags.
-
#transform ⇒ void
Previews an image transform (resize, crop, format) from submitted options.
-
#update ⇒ Object
PUT /images/1 PUT /images/1.json.
-
#update_image_profiles ⇒ void
Saves image profile assignments, removing conflicting profiles per item/type.
-
#update_multi ⇒ void
Bulk-updates the selected images from the multi-edit form.
-
#update_multiple_profiles ⇒ void
Applies, removes, or swaps an image type profile across multiple items.
-
#upload ⇒ Object
This method is called from DropZone and uppy.js.
-
#upscale_preview ⇒ Object
GET /images/:id/upscale_preview Preview page for AI upscaling with format/quality controls.
-
#visual_search ⇒ Object
Visual search - find similar images by URL, file upload, or pasted image Supports two modes: - pHash: Perceptual hash for exact/near duplicate detection - clip: AI visual embedding for semantic similarity (Gemini Embedding 2).
Methods included from Controllers::RequireTurboFrame
require_in_pane_tab_frames, require_turbo_frame
Methods inherited from CrmController
#access_denied, #context_id, #context_object, #crm_home_path, #current_ability, #default_url_options, #download_temp, #get_tempfile_path_for_download, #init_status_job_collector, #initialize_crm_lazy_chunks, #persist_enqueued_status_jobs, #record_not_found, #redirect_to_job_or_fallback, #render_edit_action, #set_context, #set_download_path, #stash_file_for_temp_download, #sync_admin_presence_cookie, #touch_employee_last_seen
Methods inherited from ApplicationController
#account_impersonated?, #add_to_flash, #after_sign_in_path_for, #bypass_forgery_protection?, #chat_enabled?, #cloudflare_cleared?, #default_catalog, #default_url_options, #enable_turbo_frames, #find_publication, #fix_invalid_accept_header, #init_js_utils, #is_globals_call?, #layout_by_resource, #locale_store, #redirect_to, #require_employee_for_crm, #set_base_host, #set_real_ip, #set_report_errors_for, #should_render_layout?, #skip_layout_for_turbo_frame?, #stamp_impersonation_context, #tab_frame_breakout_request?, #warmlyyours_canada_ip?, #warmlyyours_ip?, #y
Methods included from Controllers::ReturnPathHandling
#check_for_return_path, #redirect_to_return_path_or_default
Methods included from Controllers::AnalyticsEvents
#consume_queued_analytics_events, #registration_lead_type, #track_event
Methods included from Controllers::DeviceDetection
Methods included from Controllers::SubdomainDetection
#is_crm_request?, #is_www_request?, #json_request?
Methods included from Controllers::TurboSafeRedirect
Methods included from Controllers::TrackingDetection
#bot_request?, #gdpr_country?, #gdpr_country_data, #prevent_bots, #set_tracking_cookie, #track_visitor?
Methods included from Controllers::AcceleratedFileSending
#send_file_accelerated, #send_upload_accelerated
Methods included from Controllers::ErrorRendering
#excp_string, #mail_to_for_error_reporting, #render_400, #render_404, #render_406, #render_410, #render_500, #render_invalid_authenticity_token, #render_ip_spoof_error, #render_unpermitted_parameters, #safe_referer_or_fallback
Methods included from Controllers::TurnstileVerification
#load_turnstile_script_tag, #turnstile_lazy_widget, #turnstile_script_tag, #turnstile_widget, #validate_turnstile!
Methods included from Controllers::CloudflareCaching
edge_cached, #edge_cached_action?, #reset_cloudflare_cache, #set_cloudflare_cache, #skip_edge_cache!, #skip_session
Methods included from Controllers::Webpackable
#preload_webpack_fonts, #webpack_css_include, #webpack_css_url, #webpack_js_include, #wpd_is_running?
Methods included from Controllers::Localizable
#cloudflare_country_locale, #determine_request_locale, #geocoder_locale, #guest_user_locale_check, #locale_optional_www_auth_path?, #param_locale, #set_locale, #set_request_locale, #skip_localization?, #warmlyyours_ip_locale
Methods included from Controllers::Authenticable
#access_denied, #authenticate_account, #authenticate_account!, #authenticate_account_from_login_token!, #check_is_a_manager, #check_is_a_sales_manager, #check_is_an_admin, #check_is_an_employee, #check_party, #clear_mismatched_guest_user, #create_guest_user, #credentials?, #current_or_guest_user, #current_or_guest_user_id_read_only, #current_user, #devise_mapping, #fully_logged_in?, #generate_bot_id, #guest_user, #identifiable?, #init_current_user, #initialize_guest, #load_context_user, #logging_in, #resource, #resource_name, #restrict_access_for_non_employees, #scrubbed_request_path, #user_object, #warn_on_session_guest_id_leak
Methods included from UrlsHelper
#catalog_breadcrumb_links, #catalog_link, #catalog_link_for_product_line, #catalog_link_for_sku, #cms_link, #delocalized_path, #path_to_sales_product_sku, #path_to_sales_product_sku_for_product_line, #path_to_sales_product_sku_for_product_line_slug, #product_line_from_catalog_link, #protocol_neutral_url, #sanitize_external_url, #valid_external_url?
Instance Method Details
#add_image_profile ⇒ void
This method returns an undefined value.
Assigns this image as an item's profile of a given image type, replacing any
existing profile of that type.
612 613 614 615 616 617 618 619 620 |
# File 'app/controllers/images_controller.rb', line 612 def add_image_profile @image = Image.find(params[:id]) # This operation assumes a replacement if another image is present image_type = params.dig(:image_profile, :image_type) item_id = params.dig(:image_profile, :item_id) @image_profile = apply_image_profile(item_id:, image_type:, image_id: @image.id) flash[:error] = @image_profile.errors_to_s if @image_profile.errors.any? redirect_to_return_path_or_default image_path(@image, anchor: 'profiles') end |
#ai_metadata_suggestions ⇒ Object
GET /images/:id/ai_metadata_suggestions
Review AI-generated metadata suggestions and selectively apply them.
1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'app/controllers/images_controller.rb', line 1048 def (:update, Image) @image = Image.find(params[:id]) return if @image..present? flash[:warning] = 'No AI suggestions yet — please generate them first.' redirect_to edit_image_path(@image) and return end |
#analyze_with_vision ⇒ void
This method returns an undefined value.
Queues an ImageVisionWorker job to analyze the image with vision AI.
207 208 209 210 211 212 213 214 215 |
# File 'app/controllers/images_controller.rb', line 207 def analyze_with_vision @image = Image.find(params[:id]) (:update, @image) return_path = image_path(@image, tab: 'ai_embeddings') jid = ImageVisionWorker.perform_async(@image.id, { force: true, redirect_to: return_path }) redirect_to_job_or_fallback(jid, return_path) end |
#apply_ai_metadata ⇒ Object
PATCH /images/:id/apply_ai_metadata
Apply selected suggestion fields to the image record.
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 |
# File 'app/controllers/images_controller.rb', line 1060 def (:update, Image) @image = Image.find(params[:id]) fields = Array(params[:apply_fields]).map(&:to_sym) unless fields.any? flash[:warning] = 'No fields selected.' redirect_to (@image) and return end suggestions = (@image. || {}).with_indifferent_access updates = {} updates[:title] = suggestions['title'] if fields.include?(:title) updates[:meta_title] = suggestions['meta_title'] if fields.include?(:meta_title) updates[:meta_description] = suggestions['meta_description'] if fields.include?(:meta_description) updates[:notes] = suggestions['notes'] if fields.include?(:notes) if fields.include?(:tags) = Array(suggestions['tags']).map(&:to_s).compact_blank # Merge with existing non-AI tags to preserve manually assigned ones existing = Array(@image.).reject { |t| t == ImageGenerationWorker::AI_GENERATED_TAG } updates[:tags] = (existing + ).uniq end if updates.any? @image.update!(updates) flash[:success] = "Applied #{fields.size} AI suggestion(s) to image." end redirect_to edit_image_path(@image) end |
#apply_upscale ⇒ Object
POST /images/:id/apply_upscale
Queue the upscale worker in propose mode, then redirect to the job status page.
The worker creates an UpscaleProposal on completion and redirects to confirm_upscale.
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 |
# File 'app/controllers/images_controller.rb', line 862 def apply_upscale @image = Image.find(params[:id]) (:update, @image) unless @image.upscale_eligible? flash[:error] = (@image) redirect_to image_path(@image) and return end format = params[:format].presence || @image.default_upscale_format quality = (params[:quality] || @image.default_upscale_quality).to_i.clamp(1, 100) engine = params[:engine].presence || 'imagekit' topaz_model = params[:topaz_model].presence || 'standard_v2' scale_factor = (params[:scale_factor] || 2).to_i.clamp(2, 6) if engine == 'topaz' && !TopazLabsClient.available? flash[:error] = 'Topaz Labs is not configured. Please use ImageKit or contact an administrator.' redirect_to upscale_preview_image_path(@image) and return end jid = ImageUpscaleWorker.perform_async( @image.id, format: format, quality: quality, engine: engine, topaz_model: topaz_model, scale_factor: scale_factor, propose: true, back_url: upscale_preview_image_path(@image) ) Rails.logger.info "[apply_upscale] Queued ImageUpscaleWorker #{jid} for Image #{@image.id} " \ "(engine: #{engine}, scale: #{scale_factor}x, propose: true)" redirect_to_job_or_fallback(jid, upscale_preview_image_path(@image)) end |
#check_duplicates ⇒ Object
Check for duplicate images during upload
Computes pHash server-side using phash-rb gem for consistency with stored fingerprints
POST /images/check_duplicates
Params:
- file: uploaded image file (required)
- threshold: optional, defaults to 5 (very strict)
- exclude_id: optional, image ID to exclude from results (for edits)
Returns JSON:
{ duplicates: [...], count: N, fingerprint: "hex..." }
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'app/controllers/images_controller.rb', line 303 def check_duplicates (:read, Image) file = params[:file] return render json: { error: 'No file provided' }, status: :unprocessable_content if file.blank? threshold = (params[:threshold] || 5).to_i.clamp(0, 15) exclude_id = params[:exclude_id].to_i if params[:exclude_id].present? # Compute pHash server-side using phash-rb fingerprint = compute_phash_from_upload(file) return render json: { error: 'Could not compute fingerprint' }, status: :unprocessable_content if fingerprint.blank? duplicates = Image.find_phash_duplicates_of(fingerprint, threshold: threshold, limit: 10) # Filter out excluded image duplicates = duplicates.reject { |d| d[:image].id == exclude_id } if exclude_id results = duplicates.map do |result| { id: result[:image].id, title: result[:image].title, slug: result[:image].slug, thumbnail_url: result[:image].thumbnail_url, image_path: image_path(result[:image]), distance: result[:distance], similarity: (1.0 - (result[:distance].to_f / 64.0)).round(2) } end render json: { duplicates: results, count: results.size, fingerprint: fingerprint } end |
#clone ⇒ void
This method returns an undefined value.
Clones an image's data into a new unsaved record and renders the new form.
761 762 763 764 765 766 767 768 |
# File 'app/controllers/images_controller.rb', line 761 def clone (:create, Image) @original_image = Image.find(params[:id]) # Todo. create a proper image copy from imagekit @image = @original_image.deep_dup flash.now[:info] = 'Image data cloned' render :new end |
#confirm_upscale ⇒ Object
GET /images/:id/confirm_upscale?proposal_id=
Show the compare slider so the user can accept or reject the upscaled proposal.
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
# File 'app/controllers/images_controller.rb', line 901 def confirm_upscale @image = Image.find(params[:id]) @proposal = UpscaleProposal.find(params[:proposal_id]) (:update, @image) if @proposal.image_id != @image.id flash[:error] = 'Proposal does not belong to this image.' redirect_to image_path(@image) and return end return if @proposal.status == 'pending' flash[:warning] = 'This proposal has already been processed.' redirect_to image_path(@image) and return end |
#confirm_upscale_submit ⇒ Object
POST /images/:id/confirm_upscale
Accept (promote) or reject the upscaled proposal.
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'app/controllers/images_controller.rb', line 919 def confirm_upscale_submit @image = Image.find(params[:id]) @proposal = UpscaleProposal.find(params[:proposal_id]) (:update, @image) if @proposal.image_id != @image.id || @proposal.status != 'pending' flash[:error] = 'Invalid or already-processed proposal.' redirect_to image_path(@image) and return end if params[:commit_action] == 'reject' @proposal.delete_ik_asset! @proposal.destroy flash[:info] = 'Upscale rejected. You can try again with different settings.' redirect_to upscale_preview_image_path(@image) and return end # Accept: promote the proposal to a permanent Image save_as_new = params[:save_as_new] != '0' if save_as_new new_image = promote_proposal_as_new_image(@image, @proposal) if new_image @proposal.update!(status: 'applied') flash[:success] = "Upscaled image saved successfully." redirect_to image_path(new_image) else flash[:error] = 'Failed to create upscaled image. Please try again.' redirect_to confirm_upscale_image_path(@image, proposal_id: @proposal.id) end else success = promote_proposal_in_place(@image, @proposal) if success @proposal.update!(status: 'applied') flash[:success] = "Image replaced with upscaled version." redirect_to image_path(@image) else flash[:error] = 'Failed to replace image. Please try again.' redirect_to confirm_upscale_image_path(@image, proposal_id: @proposal.id) end end end |
#create ⇒ Object
POST /images
POST /images.json
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'app/controllers/images_controller.rb', line 711 def create (:create, Image) @image = Image.new(params[:image]) respond_to do |format| if @image.save format.html do flash[:info] = 'Image was successfully created.' redirect_to_return_path_or_default image_path(@image) end format.json do render json: { id: @image.id, thumbnail: @image.thumbnail_url(dimensions: params[:thumbnail_dimensions].presence), preview_url: @image.thumbnail_url(dimensions: '200x200'), title: @image.info, alt: @image.title, width: @image., height: @image. }, status: :created end else format.html { render action: 'new', status: :unprocessable_content } format.json { render json: { errors: @image.errors_to_s }, status: :unprocessable_content } end end end |
#create_multi ⇒ void
This method returns an undefined value.
Creates one Image per uploaded ImageKit file from the multi-upload form.
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
# File 'app/controllers/images_controller.rb', line 508 def create_multi (:create, Image) images = [] errors = [] Rails.logger.debug { "Create multi params: #{params.inspect}" } files_list_param = params.dig(:files, :files_list) Rails.logger.debug { "Files list param: #{files_list_param.inspect}" } if files_list_param.blank? Rails.logger.debug 'Files list param is blank, setting empty array' file_list = [] else begin file_list = JSON.parse(files_list_param) Rails.logger.debug { "Parsed file list: #{file_list.inspect}" } rescue JSON::ParserError => _e Rails.logger.error "Failed to parse files_list JSON: #{files_list_param.inspect}" file_list = [] end end file_list.each_with_index do |file_uid, index| # removing junk, empty values, blank arrays, etc. # Handle case where image_default params might be missing image_default_params = params[:image_default] || {} new_image_attributes = image_default_params.dup.transform_values do |v| v.is_a?(Array) ? v.compact_blank : v end.reject { |_, v| v.blank? || (v.is_a?(Array) && v.empty?) }.to_h # It might seems redundant do re-attach the file already in the store # but it is the best way to have all the accessor and dragonfly analyzer kick in # Use ImageKitFactory helper method begin res = ImageKitFactory.get_file(file_uid) file_slug = res&.name new_image_attributes[:asset] = res&.to_h&.deep_symbolize_keys new_image_attributes[:title] = "#{new_image_attributes[:title]} (#{index + 1})" if index.positive? && new_image_attributes[:title].present? # Derive title from file name new_image_attributes[:title] ||= humanize_slug(file_slug) if file_slug.present? image = Image.new(new_image_attributes) image.extract_info_from_asset image.save rescue StandardError => e errors << "Failed to fetch file details: #{e.}" end if image&.persisted? images << image else errors << "Image could not be saved: #{image.errors_to_s}" end nil end if file_list.empty? flash[:error] = 'No files detected' redirect_to url_for(action: :new_multi) elsif errors.present? flash[:error] = errors.to_sentence.capitalize redirect_to url_for(action: :new_multi) else flash[:info] = 'Images successfull uploaded' redirect_to edit_multi_images_path(image_ids: images.map(&:id)) end end |
#destroy ⇒ void
This method returns an undefined value.
Destroys the image.
821 822 823 824 825 826 |
# File 'app/controllers/images_controller.rb', line 821 def destroy @image = Image.find(params[:id]) (:destroy, @image) @image.destroy if can?(:destroy, @image) redirect_to images_path end |
#duplicates ⇒ Object
Find duplicate images using pHash fingerprints
Uses PostgreSQL bit_count() for efficient Hamming distance calculation
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 |
# File 'app/controllers/images_controller.rb', line 977 def duplicates (:read, Image) @threshold = (params[:threshold] || 0).to_i.clamp(0, 20) @limit = (params[:limit] || 100).to_i.clamp(10, 500) # Queue refresh if requested - redirect to job status page if params[:refresh] == '1' return_path = duplicates_images_path(threshold: @threshold) jid = ImageDuplicateFinderWorker.perform_async( threshold: 15, # Use higher threshold for more coverage redirect_to: return_path ) redirect_to_job_or_fallback(jid, return_path) and return end # Get stats about the pairs table @total_pairs = ImageDuplicatePair.count @pending_pairs = ImageDuplicatePair.pending.count @last_scan = ImageDuplicatePair.maximum(:updated_at) # Build clusters from pending pairs within threshold clusters = ImageDuplicatePair.build_clusters(threshold: @threshold) # Sort by size (largest first) and limit sorted_clusters = clusters.sort_by { |c| -c.size }.first(@limit) # Load all images in a single query to avoid N+1 all_image_ids = sorted_clusters.flat_map(&:to_a).uniq all_images = Image.where(id: all_image_ids).includes(:items, :image_profiles).index_by(&:id) # Enrich with image data using pre-loaded images @duplicate_groups = sorted_clusters.map do |image_ids| images = image_ids.to_a.filter_map { |id| all_images[id] } { images: images.map { |img| duplicate_image_data(img) }, count: images.size } end end |
#edit ⇒ Object
GET /images/1/edit
573 574 575 576 |
# File 'app/controllers/images_controller.rb', line 573 def edit @image = Image.find(params[:id]) (:update, @image) end |
#edit_image_profiles ⇒ void
This method returns an undefined value.
Renders the image profiles edit form.
580 581 582 583 |
# File 'app/controllers/images_controller.rb', line 580 def edit_image_profiles @image = Image.find(params[:id]) (:update, @image) end |
#edit_multi ⇒ void
This method returns an undefined value.
Renders the bulk edit form for the selected images.
674 675 676 677 678 679 680 681 |
# File 'app/controllers/images_controller.rb', line 674 def edit_multi if params[:image_ids].present? (:update, Image) @images = Image.where(id: params[:image_ids]).order(:id) else redirect_to_return_path_or_default images_path end end |
#find_similar ⇒ Object
GET /images/:id/find_similar
Find visually similar images using unified embeddings (Gemini Embedding 2)
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'app/controllers/images_controller.rb', line 356 def find_similar @image = Image.find(params[:id]) # Check if image has unified embedding unified = @image.(:unified) if unified&..blank? flash[:warning] = 'This image does not have an AI embedding yet. Run AI Analysis first.' redirect_to image_path(@image, anchor: 'file_info') and return end # Find similar images using unified embeddings @threshold = (params[:threshold] || 0.7).to_f.clamp(0.5, 0.99) @limit = (params[:limit] || 20).to_i.clamp(1, 50) @similar_images = @image.find_visually_similar(limit: @limit) # Calculate similarity scores for display @results = @similar_images.filter_map do |similar_image| next if similar_image.id == @image.id similar_unified = similar_image.(:unified) next if similar_unified&..blank? # Calculate cosine similarity between the two unified embeddings similarity = calculate_cosine_similarity( unified., similar_unified. ) next if similarity < @threshold { image: similar_image, similarity: similarity, similarity_percent: (similarity * 100).round(1) } end.sort_by { |r| -r[:similarity] } end |
#full_ai_analysis ⇒ void
This method returns an undefined value.
Queues an ImageFullAnalysisWorker job, reusing the running job if one exists.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'app/controllers/images_controller.rb', line 338 def full_ai_analysis @image = Image.find(params[:id]) (:update, @image) return_path = image_path(@image, tab: 'ai_embeddings') existing_jid = ImageFullAnalysisWorker.find_running_jid(@image.id) return redirect_to job_path(existing_jid) if existing_jid jid = ImageFullAnalysisWorker.perform_async(@image.id, { force: true, redirect_to: return_path }) ImageFullAnalysisWorker.track_jid(@image.id, jid) if jid Rails.logger.info "[full_ai_analysis] Queued ImageFullAnalysisWorker job #{jid} for Image #{@image.id}" redirect_to_job_or_fallback(jid, return_path) end |
#generate_ai_metadata ⇒ Object
POST /images/:id/generate_ai_metadata
Enqueue AI metadata suggestion generation for an existing library image.
1037 1038 1039 1040 1041 1042 1043 1044 |
# File 'app/controllers/images_controller.rb', line 1037 def (:update, Image) @image = Image.find(params[:id]) jid = ImageMetadataSuggestionsWorker.perform_async(@image.id, account_id: current_account.id) Rails.logger.info "[ImagesController#generate_ai_metadata] Queued #{jid} for Image #{@image.id}" redirect_to_job_or_fallback(jid, image_path(@image)) end |
#generate_fingerprint ⇒ void
This method returns an undefined value.
Queues an ImageFingerprintWorker job to compute the image's pHash fingerprint.
219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'app/controllers/images_controller.rb', line 219 def generate_fingerprint @image = Image.find(params[:id]) (:update, @image) # Set return path for after job completes return_path = image_path(@image, tab: 'ai_embeddings') # Queue the fingerprint job with status tracking jid = ImageFingerprintWorker.perform_async(@image.id, { force: true, redirect_to: return_path }) Rails.logger.info "[generate_fingerprint] Queued ImageFingerprintWorker job #{jid} for Image #{@image.id}" redirect_to_job_or_fallback(jid, return_path) end |
#get_forms ⇒ Object
method for wyimage redactor plugin to pull partial of image library filter form and upload form
782 783 784 785 786 787 788 789 |
# File 'app/controllers/images_controller.rb', line 782 def get_forms @q = Image.ransack({}) @image = Image.new render json: { filter_form: render_to_string(partial: 'filters_form', formats: %i[html], layout: false, locals: { per_page: 16 }), upload_form: render_to_string('new_simple', formats: %i[html], layout: false) } end |
#get_image_url ⇒ Object
get the image url for the given image, using the options specified
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 |
# File 'app/controllers/images_controller.rb', line 792 def get_image_url @image = Image.find(params[:image_id]) size = params[:dimensions].presence || params[:preset_dimensions].presence width, height = size&.split('x')&.filter_map(&:presence)&.map(&:to_i) border = params[:border].presence borderx, bordery = border&.split('x')&.filter_map(&:presence)&.map(&:to_i) background = params[:background].presence # height will be a function of width and image ratio if width && @image. && @image. height_to_width_ratio = @image..to_f / @image. height = (width * height_to_width_ratio).round end # Redo our size with computed values size = "#{width}x#{height}" image_url = @image.image_url(size: size, borderx: borderx, bordery: bordery, background: background) render json: { url: image_url, alt: params[:alt] || @image.seo_title, caption: params[:caption], class: params[:class], width: width, height: height, size: 0, image_id: @image.id } end |
#index ⇒ Object
GET /images
GET /images.json
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/controllers/images_controller.rb', line 13 def index # Direct ID lookup: redirect straight to the image if the query is a numeric ID # Skip for simple=1 (image picker JSON) — handled by prepend_direct_match instead search_query = params[:search_query].to_s.strip if params[:simple] != '1' && search_query.match?(/\A\d+\z/) image = Image.find_by(id: search_query.to_i) return redirect_to image_path(image) if image end load_images @selectable = params[:showcase_id].present? || params[:floor_plan_display_id].present? || params[:party_id].present? || params[:opportunity_id].present? || params[:toggle_select].to_b @allow_select = true # Load already-linked image IDs for visual feedback @linked_image_ids = if params[:showcase_id].present? ShowcaseDigitalAsset.where(showcase_id: params[:showcase_id]).pluck(:digital_asset_id) elsif params[:floor_plan_display_id].present? FloorPlanDisplayDigitalAsset.where(floor_plan_display_id: params[:floor_plan_display_id]).pluck(:digital_asset_id) elsif params[:party_id].present? Party.find(params[:party_id]).digital_asset_ids elsif params[:opportunity_id].present? Opportunity.find(params[:opportunity_id]).digital_asset_ids else [] end respond_to do |format| format.html do if request.headers['Turbo-Frame'] == 'image-picker-content' # Render just the picker content for lazy-loaded turbo frame render partial: '/images/picker_content', layout: false elsif request.xhr? # return just a page of images render partial: '/images/images' else render end end format.turbo_stream do # Handle infinite scroll with Turbo Streams render partial: '/images/infinite_scroll' end format.json do render json: (if params[:simple] == '1' { images: @simple_images, page: @page, last_page: @pagy.next.nil? } else @images end) end end end |
#info_popover ⇒ Object
Lazy-loaded body for the image-library info popover. Fetched on demand the
first time a thumbnail's info icon is opened (image_info_popover_controller),
so the SKU / product-line lookups never run for the whole grid upfront.
410 411 412 413 414 |
# File 'app/controllers/images_controller.rb', line 410 def info_popover @image = Image.find(params[:id]) (:show, @image) render partial: 'images/info_popover', locals: { image: @image }, layout: false end |
#lightbox_duplicates ⇒ Object
Lightbox view for duplicate image preview
1094 1095 1096 1097 1098 |
# File 'app/controllers/images_controller.rb', line 1094 def lightbox_duplicates (:read, Image) @image = Image.includes(:items, :image_profiles).find(params[:id]) render layout: false end |
#make_primary_item_image ⇒ void
This method returns an undefined value.
Marks the image as the primary item image for its linked (or selected) items.
964 965 966 967 968 969 970 971 972 973 |
# File 'app/controllers/images_controller.rb', line 964 def make_primary_item_image @image = Image.find(params[:id]) specific_item_ids = params[:specific_item_ids].presence specific_items = Item.where(id: specific_item_ids) @image.mark_as_primary_item_image(specific_items) @image.make_primary_item_image = true @image.save flash[:info] = 'Image was made primary item image for linked items' redirect_to image_path(@image) end |
#merge_duplicates ⇒ Object
Merge duplicate images - transfer all references from source to target
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 |
# File 'app/controllers/images_controller.rb', line 1164 def merge_duplicates (:update, Image) target_id = params[:target_id].to_i all_image_ids = Array(params[:image_ids]).map(&:to_i).uniq source_ids = all_image_ids.reject { |id| id == target_id } if target_id.zero? || source_ids.empty? flash[:error] = 'Please select a target image and at least one source image to merge' redirect_to duplicates_images_path and return end # Only allow ImageKit deletion in production (assets are shared across environments) delete_from_imagekit = Rails.env.production? && params[:delete_source_imagekit] == '1' source_assets = [] # Collect ImageKit assets for deletion after transaction begin ActiveRecord::Base.transaction do # Eager load associations that exist on the model to avoid N+1 queries includes_for_merge = %i[ items product_lines product_categories parties opportunities reviews image_profiles slugs ] target_image = Image.includes(*includes_for_merge).find(target_id) source_images = Image.includes(*includes_for_merge).where(id: source_ids).to_a source_image_ids = source_images.map(&:id) all_image_ids = [target_id] + source_image_ids # Pre-fetch showcase_digital_assets for all images showcase_assets_by_image = ShowcaseDigitalAsset .where(digital_asset_id: all_image_ids) .group_by(&:digital_asset_id) # Pre-fetch floor_plan_display_digital_assets for all images floor_plan_assets_by_image = FloorPlanDisplayDigitalAsset .where(digital_asset_id: all_image_ids) .group_by(&:digital_asset_id) # Pre-fetch content_embeddings for source images = ContentEmbedding .where(embeddable_type: 'Image', embeddable_id: source_image_ids) .group_by(&:embeddable_id) # Pre-fetch all image duplicate pairs for source images in one query duplicate_pairs_by_source = ImageDuplicatePair .where(image_a_id: source_image_ids) .or(ImageDuplicatePair.where(image_b_id: source_image_ids)) .group_by { |pair| [pair.image_a_id, pair.image_b_id].find { |id| source_image_ids.include?(id) } } # Pre-fetch tags for all images (target + sources) to avoid N+1 in taggable concern = Tagging.where(taggable_type: 'Image', taggable_id: all_image_ids) .includes(:tag) .group_by(&:taggable_id) .transform_values { |taggings| taggings.map { |t| t.tag.name } } # Collect source assets before we delete the records source_images.each do |img| source_assets << { id: img.id, asset: img.asset&.dup } if img.asset.present? end # Apply field selections apply_merge_field_selections(target_image, source_images, params) # Merge all source images into target using pre-loaded data merge_image_associations_batch( sources: source_images, target: target_image, duplicate_pairs_by_source: duplicate_pairs_by_source, tags_by_image: , showcase_assets_by_image: showcase_assets_by_image, floor_plan_assets_by_image: floor_plan_assets_by_image, content_embeddings_by_image: ) # Track merged IDs for legacy reference lookup # This allows hardcoded Image.find(old_id) to still work via merged_from_ids existing_merged_ids = target_image.merged_from_ids || [] new_merged_ids = source_images.flat_map do |img| # Include the source image ID and any IDs it had absorbed from previous merges [img.id] + (img.merged_from_ids || []) end target_image.update!(merged_from_ids: (existing_merged_ids + new_merged_ids).uniq) # Delete source images from database (skip ImageKit callback) source_images.each do |source_image| source_image.skip_imagekit_deletion = true source_image.destroy! end Rails.logger.info "[ImageMerge] Successfully merged #{source_ids.join(', ')} into #{target_id}" end # Delete from ImageKit AFTER successful transaction if delete_from_imagekit deleted_count = 0 source_assets.each do |asset_info| file_id = asset_info[:asset]['file_id'] || asset_info[:asset]['fileId'] next unless file_id begin ImageKitFactory.delete_file(file_id) deleted_count += 1 Rails.logger.info "[ImageMerge] Deleted image #{asset_info[:id]} from ImageKit" rescue StandardError => e Rails.logger.warn "[ImageMerge] Could not delete #{asset_info[:id]} from ImageKit: #{e.}" end end Rails.logger.info "[ImageMerge] Deleted #{deleted_count}/#{source_assets.size} files from ImageKit" end flash[:success] = "Successfully merged #{source_ids.size} images into ##{target_id}" rescue StandardError => e Rails.logger.error "[ImageMerge] Transaction failed: #{e.}" Rails.logger.error e.backtrace.first(10).join("\n") flash[:error] = "Merge failed: #{e.}" end redirect_to duplicates_images_path end |
#merge_preview ⇒ Object
Full-page merge preview with all options
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 |
# File 'app/controllers/images_controller.rb', line 1101 def merge_preview (:update, Image) image_ids = Array(params[:image_ids]).map(&:to_i).uniq if image_ids.size < 2 flash[:error] = 'Please select at least 2 images to merge' redirect_to duplicates_images_path and return end @images = Image.includes(:items, :image_profiles, :product_lines, :product_categories, :parties, :opportunities) .where(id: image_ids).order(:id).to_a # Calculate scores for each image to suggest best target @image_scores = @images.each_with_object({}) do |img, hash| score = 0 score += img.items.size * 3 score += img.image_profiles.size * 2 score += img.product_lines.size score += img.product_categories.size score += 20 if img.primary_item_images.exists? score += 5 if img..present? score += 3 if img.source.present? score += img..size hash[img.id] = score end @suggested_target = @images.max_by { |img| @image_scores[img.id] } # Gather all unique values across images for each field @field_options = { title: @images.map { |i| { id: i.id, value: i.title } }.reject { |h| h[:value].blank? }, meta_title: @images.map { |i| { id: i.id, value: i. } }.reject { |h| h[:value].blank? }, reference_number: @images.map { |i| { id: i.id, value: i.reference_number } }.reject { |h| h[:value].blank? }, source: @images.map { |i| { id: i.id, value: i.source } }.reject { |h| h[:value].blank? }.uniq { |h| h[:value] }, notes: @images.map { |i| { id: i.id, value: i.notes } }.reject { |h| h[:value].blank? }, location: @images.map { |i| { id: i.id, value: i.location } }.reject { |h| h[:value].blank? }.uniq { |h| h[:value] } } # Collections to combine (show what will be merged) @combined = { tags: @images.flat_map(&:tags).uniq.sort, locales: @images.flat_map(&:locales).uniq.sort, product_lines: @images.flat_map(&:product_lines).uniq, product_categories: @images.flat_map(&:product_categories).uniq, items: @images.flat_map(&:items).uniq, parties: @images.flat_map(&:parties).uniq, opportunities: @images.flat_map(&:opportunities).uniq, slug_histories: FriendlyId::Slug.where(sluggable_type: 'DigitalAsset', sluggable_id: image_ids).pluck(:slug).uniq } # References that will be updated @references = { primary_image_items: Item.where(primary_image_id: image_ids).pluck(:id, :name), image_profiles: ImageProfile.where(image_id: image_ids).count, showcases: ShowcaseDigitalAsset.where(digital_asset_id: image_ids).count, floor_plans: FloorPlanDisplayDigitalAsset.where(digital_asset_id: image_ids).count, video_posters: Video.where(poster_image_id: image_ids).count, preview_articles: Article.where(preview_image_id: image_ids).count } end |
#new ⇒ Object
GET /images/new
GET /images/new.json
117 118 119 120 121 122 123 124 125 |
# File 'app/controllers/images_controller.rb', line 117 def new @image = Image.new(params[:image] || {}) (:create, @image) respond_to do |format| format.html # new.html.erb format.json { render json: @image } end end |
#new_multi ⇒ void
This method returns an undefined value.
Renders the multi-image upload form.
129 130 131 |
# File 'app/controllers/images_controller.rb', line 129 def new_multi (:create, Image) end |
#pagination ⇒ void
This method returns an undefined value.
Renders a paginated page of the image library with selection context.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/images_controller.rb', line 70 def pagination @selectable = params[:showcase_id].present? || params[:floor_plan_display_id].present? || params[:party_id].present? || params[:opportunity_id].present? || params[:toggle_select].to_b load_images # Load already-linked image IDs for visual feedback @linked_image_ids = if params[:showcase_id].present? ShowcaseDigitalAsset.where(showcase_id: params[:showcase_id]).pluck(:digital_asset_id) elsif params[:floor_plan_display_id].present? FloorPlanDisplayDigitalAsset.where(floor_plan_display_id: params[:floor_plan_display_id]).pluck(:digital_asset_id) elsif params[:party_id].present? Party.find(params[:party_id]).digital_asset_ids elsif params[:opportunity_id].present? Opportunity.find(params[:opportunity_id]).digital_asset_ids else [] end render layout: should_render_layout? end |
#purge_cache ⇒ void
This method returns an undefined value.
Requests a CDN cache purge for the image.
830 831 832 833 834 835 |
# File 'app/controllers/images_controller.rb', line 830 def purge_cache @image = Image.find(params[:id]) res = @image.purge_cache flash[:info] = "Cache purge requested for #{@image.url}, response: #{res.inspect}" redirect_to image_path(@image) end |
#refresh_embedding ⇒ void
This method returns an undefined value.
Regenerates the image's unified Gemini embedding (text or image+text).
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'app/controllers/images_controller.rb', line 157 def @image = Image.find(params[:id]) (:update, @image) content = @image.(:primary) if content.blank? flash[:warning] = 'No content available for embedding' return redirect_to image_path(@image, tab: 'ai_embeddings') end begin truncated_content = content.to_s.truncate(8_000, omission: '...') image_url = @image.ik_raw_url text_vector = if image_url.present? Embedding::Gemini.(image_url, text: truncated_content, dimensions: 1536) else Embedding::Gemini.(truncated_content, dimensions: 1536) end if text_vector.present? = ContentEmbedding.find_or_initialize_by( embeddable_type: 'Image', embeddable_id: @image.id, content_type: 'unified', locale: 'en' ) . = text_vector . = Embedding::Gemini.model_name . = 1536 .content_hash = @image.(:primary) .token_count = truncated_content.split.size .save! flash[:info] = "Embedding refreshed (#{Embedding::Gemini.model_name}, 1536d)" else flash[:warning] = 'Embedding generation returned empty result' end rescue Embedding::Gemini::Error => e flash[:danger] = "Embedding failed: #{e.}" Rails.logger.error "[refresh_embedding] Gemini error for Image #{@image.id}: #{e.}" end redirect_to image_path(@image, tab: 'ai_embeddings') end |
#refresh_imagekit_metadata ⇒ Object
Refresh ImageKit metadata for an image
Fetches the latest metadata from ImageKit API
If ImageKit has a pHash and we don't have a fingerprint, copies it to fingerprint column
POST /images/:id/refresh_imagekit_metadata
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'app/controllers/images_controller.rb', line 238 def @image = Image.find(params[:id]) (:update, @image) if @image.ik_file_id.blank? flash[:error] = 'Image has no ImageKit file ID' return redirect_back_or_to(tab_file_info_image_path(@image)) end begin # Fetch metadata from ImageKit API response = ImageKitFactory.(@image.ik_file_id) = response&.to_h&.with_indifferent_access if .present? updates_made = [] # If ImageKit has pHash and we don't have fingerprint, use it # ImageKit provides pHash as a large decimal number # We store fingerprint as bigint (converted from hex representation) if [:pHash].present? && @image.fingerprint.blank? # Convert ImageKit's decimal pHash to hex, then to bigint phash_hex = [:pHash].to_i.to_s(16).rjust(16, '0') fingerprint_int = Image.hex_to_fingerprint(phash_hex) @image.fingerprint = fingerprint_int updates_made << "fingerprint: #{phash_hex} (#{fingerprint_int})" end # Update other metadata in asset (not pHash - fingerprint is our source of truth) @image.ik_has_alpha = [:hasAlpha] if .key?(:hasAlpha) @image.asset['quality'] = [:quality] if .key?(:quality) @image.asset['density'] = [:density] if .key?(:density) @image.asset['exif'] = [:exif] if [:exif].present? @image.save! flash[:notice] = if updates_made.any? "ImageKit metadata refreshed. Updated: #{updates_made.join(', ')}" elsif [:pHash].present? "ImageKit has pHash but fingerprint already set to: #{@image.fingerprint_hex}" else 'ImageKit metadata refreshed (pHash not yet available from ImageKit)' end else flash[:warning] = 'ImageKit returned empty metadata' end rescue StandardError => e Rails.logger.error "[refresh_imagekit_metadata] Error for Image #{@image.id}: #{e.}" flash[:error] = "Failed to refresh metadata: #{e.}" end redirect_back_or_to(tab_file_info_image_path(@image)) end |
#remove_image_profile ⇒ void
This method returns an undefined value.
Removes an image profile assignment from this image.
624 625 626 627 628 629 630 |
# File 'app/controllers/images_controller.rb', line 624 def remove_image_profile @image = Image.find(params[:id]) @image_profile = @image.image_profiles.find(params[:image_profile_id]) @image_profile.destroy flash[:error] = @image_profile.errors_to_s if @image_profile.errors.any? redirect_to_return_path_or_default image_path(@image, anchor: 'profiles') end |
#show ⇒ void
This method returns an undefined value.
Shows a single image.
111 112 113 |
# File 'app/controllers/images_controller.rb', line 111 def show @image = Image.find(params[:id]) end |
#tab_duplicates ⇒ Object
Find potential duplicate images for this specific image using pHash fingerprint
Lazy-loaded tab on the image show page
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'app/controllers/images_controller.rb', line 442 def tab_duplicates @image = Image.find(params[:id]) (:show, @image) @threshold = (params[:threshold] || 0).to_i.clamp(0, 20) @has_fingerprint = @image.fingerprint.present? @duplicates = if @has_fingerprint @image.find_phash_duplicates(threshold: @threshold, limit: 50) else [] end respond_to do |format| format.html { render layout: should_render_layout? } format.turbo_stream end end |
#tab_embeddings ⇒ void
This method returns an undefined value.
Renders the AI embeddings tab fragment.
146 147 148 149 150 151 152 153 |
# File 'app/controllers/images_controller.rb', line 146 def @image = Image.find(params[:id]) (:show, @image) respond_to do |format| format.html { render layout: should_render_layout? } format.turbo_stream end end |
#tab_file_info ⇒ void
This method returns an undefined value.
Renders the file info tab fragment.
135 136 137 138 139 140 141 142 |
# File 'app/controllers/images_controller.rb', line 135 def tab_file_info @image = Image.find(params[:id]) (:show, @image) respond_to do |format| format.html { render layout: should_render_layout? } format.turbo_stream end end |
#tab_items ⇒ void
This method returns an undefined value.
Renders the items tab fragment with the image's linked items, paginated.
397 398 399 400 401 402 403 404 405 |
# File 'app/controllers/images_controller.rb', line 397 def tab_items @image = Image.find(params[:id]) @pagy, @items = pagy(@image.all_my_items.includes(:product_lines).order(:sku), limit: 50) (:show, @image) respond_to do |format| format.html { render layout: should_render_layout? } format.turbo_stream end end |
#tab_profiles ⇒ void
This method returns an undefined value.
Renders the image profiles tab fragment.
418 419 420 421 422 423 424 425 426 |
# File 'app/controllers/images_controller.rb', line 418 def tab_profiles @image = Image.find(params[:id]) @image_profiles = @image.image_profiles (:show, @image) respond_to do |format| format.html { render layout: should_render_layout? } format.turbo_stream end end |
#tab_videos ⇒ void
This method returns an undefined value.
Renders the videos tab fragment with videos using this image as poster.
430 431 432 433 434 435 436 437 438 |
# File 'app/controllers/images_controller.rb', line 430 def tab_videos @image = Image.find(params[:id]) @videos = @image.video_posters.includes(:product_categories, :items, :product_lines).order(:title) (:show, @image) respond_to do |format| format.html { render layout: should_render_layout? } format.turbo_stream end end |
#tags_lookup ⇒ Object
GET /images/tags_lookup
Remote autocomplete for the filter panel's tag pickers — SQL-filtered so the
page never has to embed all ~4k image tags.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/controllers/images_controller.rb', line 93 def (:read, Image) query = params[:q].to_s.strip page = [params[:page].to_i, 1].max per_page = (params[:per_page].presence || 20).to_i.clamp(1, 100) scope = Tag.joins(:taggings).where(taggings: { taggable_type: 'Image' }).distinct.order(:name) scope = scope.where(Tag.arel_table[:name].matches("%#{Tag.sanitize_sql_like(query)}%")) if query.present? names = scope.offset((page - 1) * per_page).limit(per_page + 1).pluck(:name) render json: { results: names.first(per_page).map { |name| { id: name, text: name } }, pagination: { more: names.size > per_page } } end |
#transform ⇒ void
This method returns an undefined value.
Previews an image transform (resize, crop, format) from submitted options.
772 773 774 775 776 777 778 779 |
# File 'app/controllers/images_controller.rb', line 772 def transform @image = Image.find(params[:id]) @image_transform = Image::Transform.new(params[:image_transform] || {}) @image_path = @image.image_url # TODO: webp: false was here, is it necessary? @url = @image.image_url(@image_transform.attributes) @download_url = @image.image_url({ download: true, webp: false }.merge(@image_transform.attributes)) end |
#update ⇒ Object
PUT /images/1
PUT /images/1.json
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'app/controllers/images_controller.rb', line 741 def update @image = Image.find(params[:id]) (:update, @image) respond_to do |format| if @image.update(params[:image]) flash[:warning] = 'A new image upload was detected, it will take some time for cache to clear, please monitor the Purge Cache Status field. Once complete. Clear your browser cache.' if params.dig(:image, :new_image).present? format.html do redirect_to_return_path_or_default image_path(@image), notice: 'Image was successfully updated.' end format.json { head :no_content } else format.html { render action: 'edit', status: :unprocessable_content } format.json { render json: @image.errors, status: :unprocessable_content } end end end |
#update_image_profiles ⇒ void
This method returns an undefined value.
Saves image profile assignments, removing conflicting profiles per item/type.
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'app/controllers/images_controller.rb', line 587 def update_image_profiles @image = Image.find(params[:id]) (:update, @image) image_params = params.dig(:image, :image_profiles_attributes)&.values || [] Image.transaction do image_params.each do |image_param| image_type = image_param[:image_type] item_id = image_param[:item_id] image_param[:_destroy].to_b image_profile_id = image_param[:id] # If we have a new image_profile_id, we will first remove existing image of that type assigned to that item to prevent duplicates ImageProfile.where.not(id: image_profile_id).find_by(image_type: image_type, item_id: item_id)&.destroy end # [{"image_type"=>"AMZ_MAIN", "item_id"=>"107", "transform_params"=>"{}", "_destroy"=>"0", "id"=>"448994"}, {"image_type"=>"AMZ_PT02", "item_id"=>"607", "transform_params"=>"{}"}] if @image.update(params[:image]) redirect_to_return_path_or_default image_path(@image), notice: 'Image was successfully updated.' else render :edit_image_profiles, status: :unprocessable_content end end end |
#update_multi ⇒ void
This method returns an undefined value.
Bulk-updates the selected images from the multi-edit form.
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'app/controllers/images_controller.rb', line 685 def update_multi (:update, Image) updated_images = [] error_images = [] image_ids = params[:image]&.keys || [] images = Image.where(id: image_ids).includes(:parties, :opportunities, :items) (params[:image] || {}).each do |image_id, attributes| image = images.find { |img| img.id == image_id.to_i } if image.update(attributes) updated_images << image else error_images << image end end if error_images.present? @images = error_images render :edit_multi, status: :unprocessable_content else flash[:info] = 'Images updated' redirect_params = { q: { id_in: updated_images.map(&:id) } } if updated_images.present? redirect_to images_path(redirect_params) end end |
#update_multiple_profiles ⇒ void
This method returns an undefined value.
Applies, removes, or swaps an image type profile across multiple items.
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'app/controllers/images_controller.rb', line 634 def update_multiple_profiles @image = Image.find(params[:id]) @item_ids = (params[:item_ids].presence || [])&.map(&:to_i) image_type = params.dig(:profile_actions, :image_type) swap_image_type = params.dig(:profile_actions, :swap_image_type) # Apply to each errors = [] image_action = params[:commit]&.downcase @item_ids.each do |item_id| if image_action&.start_with?('apply') image_profile = apply_image_profile(item_id:, image_type:, image_id: @image.id) errors << "#{item_id}: #{image_profile.errors_to_s}. " if image_profile.errors.present? elsif image_action&.start_with?('remove') ImageProfile.find_by(item_id: item_id, image_id: @image.id, image_type: image_type)&.destroy elsif image_action&.start_with?('swap') && swap_image_type.present? ImageProfile.transaction do ImageProfile.where(item_id: item_id, image_type: image_type).update_all(image_type: 'TEMPORARY') ImageProfile.where(item_id: item_id, image_type: swap_image_type).update_all(image_type: image_type) ImageProfile.where(item_id: item_id, image_type: 'TEMPORARY').update_all(image_type: swap_image_type) end else errors << "#{item_id}: Invalid Action selected #{image_action}, Apply or Remove" end end @image_profiles = @image.image_profiles if errors.present? flash[:error] = errors.join("\n") elsif @item_ids.blank? || image_type.blank? flash[:warning] = 'Items and image type to apply must be selected' else flash[:notice] = "Image type operation applied to #{@item_ids.size} item(s)" end respond_to do |format| format.turbo_stream format.html { redirect_to_return_path_or_default image_path(@image, anchor: 'profiles') } end end |
#upload ⇒ Object
This method is called from DropZone and uppy.js
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'app/controllers/images_controller.rb', line 465 def upload (:create, Image) uploaded_pics = params[:file] # Take the files which are sent by HTTP POST request. files_list = [] Rails.logger.debug { "Upload params: #{params.inspect}" } Rails.logger.debug { "Uploaded pics class: #{uploaded_pics.class}" } Rails.logger.debug { "Uploaded pics: #{uploaded_pics.inspect}" } # Handle both Dropzone (hash of files) and uppy.js (single file) formats if uploaded_pics.is_a?(ActionDispatch::Http::UploadedFile) # uppy.js sends a single file files_to_process = [uploaded_pics] Rails.logger.debug 'Processing single file from uppy.js' elsif uploaded_pics.is_a?(Hash) # Dropzone sends a hash of files {"0" => file1, "1" => file2} files_to_process = uploaded_pics.values Rails.logger.debug 'Processing hash of files from Dropzone' else # Fallback for other formats files_to_process = Array(uploaded_pics) Rails.logger.debug 'Processing array fallback' end files_to_process.each do |file| next unless file.is_a?(ActionDispatch::Http::UploadedFile) Rails.logger.debug { "Processing file: #{file.original_filename}" } res = Image.upload_file_to_ik(file) Rails.logger.debug { "Upload response: #{res.inspect}" } # ImageKit 4.0 returns snake_case keys: file_id instead of fileId # Response is symbolized, so use symbol keys uid = res.dig(:response, :file_id) || res.dig(:response, :fileId) || res.dig(:response, 'file_id') || res.dig(:response, 'fileId') files_list << uid if uid Rails.logger.debug { "File uploaded with ID: #{uid}" } end Rails.logger.debug { "Files list: #{files_list}" } render json: { message: 'You have successfully uploaded your images.', files_list: files_list.to_json } end |
#upscale_preview ⇒ Object
GET /images/:id/upscale_preview
Preview page for AI upscaling with format/quality controls
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 |
# File 'app/controllers/images_controller.rb', line 839 def upscale_preview @image = Image.find(params[:id]) (:update, @image) unless @image.upscale_eligible? flash[:warning] = (@image) redirect_to image_path(@image) and return end # Default format and quality from image @format = params[:format].presence || @image.default_upscale_format @quality = (params[:quality] || @image.default_upscale_quality).to_i.clamp(1, 100) @engine = params[:engine].presence || 'imagekit' @topaz_model = params[:topaz_model].presence || 'standard_v2' @scale_factor = (params[:scale_factor] || 4).to_i.clamp(2, 6) # Available format options @format_options = available_upscale_formats(@image) end |
#visual_search ⇒ Object
Visual search - find similar images by URL, file upload, or pasted image
Supports two modes:
- pHash: Perceptual hash for exact/near duplicate detection
- clip: AI visual embedding for semantic similarity (Gemini Embedding 2)
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 |
# File 'app/controllers/images_controller.rb', line 1022 def visual_search (:read, Image) # Check if this is a search request or just loading the page if params[:phash].present? || params[:url].present? || params[:file].present? || params[:base64].present? perform_visual_search else @results = nil @mode = params[:mode] || 'phash' @threshold = params[:threshold] end end |