Class: ImagesController
- Inherits:
-
CrmController
- Object
- ActionController::Base
- ApplicationController
- CrmController
- ImagesController
- Defined in:
- app/controllers/images_controller.rb
Overview
Controller: images.
Constant Summary
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
Constants included from Www::SeoHelper
Www::SeoHelper::AWARDS, Www::SeoHelper::CA_ADDRESS, Www::SeoHelper::CA_BUSINESS_HOURS, Www::SeoHelper::CA_CONTACT_POINT, Www::SeoHelper::CA_CURRENCIES, Www::SeoHelper::CA_DESCRIPTION, Www::SeoHelper::CA_FOUNDING_DATE, Www::SeoHelper::CA_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::CA_LEGAL_NAME, Www::SeoHelper::CA_LOCAL_BUSINESS, Www::SeoHelper::CA_ONLINE_STORE, Www::SeoHelper::CA_RETURN_POLICY, Www::SeoHelper::CA_SALES_DEPARTMENT, Www::SeoHelper::CA_SERVICE_AREA, Www::SeoHelper::CA_URL, Www::SeoHelper::CA_VAT_ID, Www::SeoHelper::CA_WAREHOUSE_DEPARTMENT, Www::SeoHelper::CA_WAREHOUSE_HOURS, Www::SeoHelper::COMPANY_EMAIL, Www::SeoHelper::COMPANY_LOGO, Www::SeoHelper::COMPANY_NAME, Www::SeoHelper::COMPANY_SLOGAN, Www::SeoHelper::EXPERTISE, Www::SeoHelper::FAX_NUMBER, Www::SeoHelper::GS1_COMPANY_PREFIX, Www::SeoHelper::ISO6523_CODE, Www::SeoHelper::PAYMENT_METHODS, Www::SeoHelper::PHONE_NUMBER, Www::SeoHelper::PRIMARY_NAICS, Www::SeoHelper::REFUND_TYPE, Www::SeoHelper::RETURN_FEES, Www::SeoHelper::RETURN_METHOD, Www::SeoHelper::RETURN_POLICY_CATEGORY, Www::SeoHelper::SECONDARY_NAICS, Www::SeoHelper::SOCIAL_PROFILES, Www::SeoHelper::US_ADDRESS, Www::SeoHelper::US_BUSINESS_HOURS, Www::SeoHelper::US_CONTACT_POINT, Www::SeoHelper::US_CURRENCIES, Www::SeoHelper::US_DESCRIPTION, Www::SeoHelper::US_FOUNDING_DATE, Www::SeoHelper::US_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::US_IMAGE, Www::SeoHelper::US_LEGAL_NAME, Www::SeoHelper::US_LOCAL_BUSINESS, Www::SeoHelper::US_ONLINE_STORE, Www::SeoHelper::US_RETURN_POLICY, Www::SeoHelper::US_SALES_DEPARTMENT, Www::SeoHelper::US_SERVICE_AREA, Www::SeoHelper::US_TAX_ID, Www::SeoHelper::US_URL, Www::SeoHelper::US_WAREHOUSE_DEPARTMENT, Www::SeoHelper::US_WAREHOUSE_HOURS
Constants included from IconHelper
IconHelper::CUSTOM_ICON_MAP, IconHelper::CUSTOM_SVG_DIR, IconHelper::DEFAULT_FAMILY
Instance Method Summary collapse
- #add_image_profile ⇒ Object
-
#ai_metadata_suggestions ⇒ Object
GET /images/:id/ai_metadata_suggestions Review AI-generated metadata suggestions and selectively apply them.
- #analyze_with_vision ⇒ Object
-
#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 ⇒ Object
-
#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 ⇒ Object
- #destroy ⇒ Object
-
#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 ⇒ Object
- #edit_multi ⇒ Object
-
#find_similar ⇒ Object
GET /images/:id/find_similar Find visually similar images using unified embeddings (Gemini Embedding 2).
- #full_ai_analysis ⇒ Object
-
#generate_ai_metadata ⇒ Object
POST /images/:id/generate_ai_metadata Enqueue AI metadata suggestion generation for an existing library image.
- #generate_fingerprint ⇒ Object
-
#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 ⇒ Object
-
#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 ⇒ Object
- #pagination ⇒ Object
- #purge_cache ⇒ Object
- #refresh_embedding ⇒ Object
-
#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 ⇒ Object
- #show ⇒ Object
-
#tab_duplicates ⇒ Object
Find potential duplicate images for this specific image using pHash fingerprint Lazy-loaded tab on the image show page.
- #tab_embeddings ⇒ Object
- #tab_file_info ⇒ Object
- #tab_items ⇒ Object
- #tab_profiles ⇒ Object
- #tab_videos ⇒ Object
- #transform ⇒ Object
-
#update ⇒ Object
PUT /images/1 PUT /images/1.json.
- #update_image_profiles ⇒ Object
- #update_multi ⇒ Object
- #update_multiple_profiles ⇒ Object
-
#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 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
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?, #stamp_impersonation_context, #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, #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 ApplicationHelper
#better_number_to_currency, #check_force_logout, #check_or_cross, #check_or_times, #embedded_tab_frame_id, #error_messages, #general_disclaimer_on_product_installation_and_local_codes, #gridjs_from_html_table, #gridjs_table, #is_wy_ip, #line_break, #parent_layout, #pass_or_fail, #render_error_messages_list, #render_video_card, #resolved_auth_form_turbo_frame, #return_path_or, #safe_css_color, #set_return_path_if_present, #set_section_if_present, #tab_frame_id, #to_underscore, #track_page?, #turbo_section_wrapper, #turbo_tabs_request?, #url_on_same_domain_as_request, #widget_index_daily_focus_index_path, #working_hours?, #yes_or_no, #yes_or_no_highlighted, #yes_or_no_with_check_or_cross, #youtube_video
Methods included from UppyUploaderHelper
#file_uploader, #image_uploader, #large_file_uploader_s3, #lead_sketch_uploader, #rma_image_uploader, #rma_image_uploader_s3, #uppy_uploader, #video_uploader
Methods included from Www::ImagesHelper
#image_asset_tag, #image_asset_url
Methods included from Www::SeoHelper
#add_page_schema, #add_webpage_schema, #canada?, #company_social_links, #ensure_context_json, #json_ld_script_tag, #local_business_schema, #online_store_id, #online_store_schema, #page_main_entity, #page_main_entity_json, #render_auto_collection_page_schema, #render_collection_page_schema, #render_local_business_schema, #render_online_store_schema, #render_page_schemas, #render_page_video_schemas, #render_webpage_schema, #render_webpage_schema_with_collections, #usa?
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?
Methods included from IconHelper
#account_nav_icon, #fa_icon, #star_rating_html
Instance Method Details
#add_image_profile ⇒ Object
557 558 559 560 561 562 563 564 565 |
# File 'app/controllers/images_controller.rb', line 557 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.
982 983 984 985 986 987 988 989 990 |
# File 'app/controllers/images_controller.rb', line 982 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 ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'app/controllers/images_controller.rb', line 169 def analyze_with_vision @image = Image.find(params[:id]) (:update, @image) return_path = (@image, target_id: '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.
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 |
# File 'app/controllers/images_controller.rb', line 995 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.
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 818 819 820 821 822 823 824 825 826 827 828 |
# File 'app/controllers/images_controller.rb', line 793 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..." }
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 291 292 293 294 295 296 |
# File 'app/controllers/images_controller.rb', line 265 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 ⇒ Object
696 697 698 699 700 701 702 703 |
# File 'app/controllers/images_controller.rb', line 696 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.
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 |
# File 'app/controllers/images_controller.rb', line 833 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.
852 853 854 855 856 857 858 859 860 861 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 |
# File 'app/controllers/images_controller.rb', line 852 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
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'app/controllers/images_controller.rb', line 648 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 ⇒ Object
460 461 462 463 464 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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'app/controllers/images_controller.rb', line 460 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 ⇒ Object
752 753 754 755 756 757 |
# File 'app/controllers/images_controller.rb', line 752 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
908 909 910 911 912 913 914 915 916 917 918 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 |
# File 'app/controllers/images_controller.rb', line 908 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
525 526 527 528 |
# File 'app/controllers/images_controller.rb', line 525 def edit @image = Image.find(params[:id]) (:update, @image) end |
#edit_image_profiles ⇒ Object
530 531 532 533 |
# File 'app/controllers/images_controller.rb', line 530 def edit_image_profiles @image = Image.find(params[:id]) (:update, @image) end |
#edit_multi ⇒ Object
613 614 615 616 617 618 619 620 |
# File 'app/controllers/images_controller.rb', line 613 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)
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'app/controllers/images_controller.rb', line 316 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 ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'app/controllers/images_controller.rb', line 298 def full_ai_analysis @image = Image.find(params[:id]) (:update, @image) return_path = (@image, target_id: '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.
970 971 972 973 974 975 976 977 |
# File 'app/controllers/images_controller.rb', line 970 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 ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'app/controllers/images_controller.rb', line 179 def generate_fingerprint @image = Image.find(params[:id]) (:update, @image) # Set return path for after job completes return_path = (@image, target_id: '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
715 716 717 718 719 720 721 722 |
# File 'app/controllers/images_controller.rb', line 715 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
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
# File 'app/controllers/images_controller.rb', line 725 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
8 9 10 11 12 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 |
# File 'app/controllers/images_controller.rb', line 8 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.
368 369 370 371 372 |
# File 'app/controllers/images_controller.rb', line 368 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
1029 1030 1031 1032 1033 |
# File 'app/controllers/images_controller.rb', line 1029 def lightbox_duplicates (:read, Image) @image = Image.includes(:items, :image_profiles).find(params[:id]) render layout: false end |
#make_primary_item_image ⇒ Object
895 896 897 898 899 900 901 902 903 904 |
# File 'app/controllers/images_controller.rb', line 895 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
1099 1100 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 1162 1163 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 |
# File 'app/controllers/images_controller.rb', line 1099 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
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 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 1092 1093 1094 1095 1096 |
# File 'app/controllers/images_controller.rb', line 1036 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
89 90 91 92 93 94 95 96 97 |
# File 'app/controllers/images_controller.rb', line 89 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 ⇒ Object
99 100 101 |
# File 'app/controllers/images_controller.rb', line 99 def new_multi (:create, Image) end |
#pagination ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/controllers/images_controller.rb', line 63 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 ⇒ Object
759 760 761 762 763 764 |
# File 'app/controllers/images_controller.rb', line 759 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 ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/controllers/images_controller.rb', line 121 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, target_id: '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, target_id: '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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'app/controllers/images_controller.rb', line 199 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 ⇒ Object
567 568 569 570 571 572 573 |
# File 'app/controllers/images_controller.rb', line 567 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 ⇒ Object
83 84 85 |
# File 'app/controllers/images_controller.rb', line 83 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
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'app/controllers/images_controller.rb', line 396 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 ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'app/controllers/images_controller.rb', line 112 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 ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'app/controllers/images_controller.rb', line 103 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 ⇒ Object
355 356 357 358 359 360 361 362 363 |
# File 'app/controllers/images_controller.rb', line 355 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 ⇒ Object
374 375 376 377 378 379 380 381 382 |
# File 'app/controllers/images_controller.rb', line 374 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 ⇒ Object
384 385 386 387 388 389 390 391 392 |
# File 'app/controllers/images_controller.rb', line 384 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 |
#transform ⇒ Object
705 706 707 708 709 710 711 712 |
# File 'app/controllers/images_controller.rb', line 705 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
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'app/controllers/images_controller.rb', line 678 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 ⇒ Object
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'app/controllers/images_controller.rb', line 535 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 ⇒ Object
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'app/controllers/images_controller.rb', line 622 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 ⇒ Object
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'app/controllers/images_controller.rb', line 575 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
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'app/controllers/images_controller.rb', line 419 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
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 |
# File 'app/controllers/images_controller.rb', line 769 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)
954 955 956 957 958 959 960 961 962 963 964 965 |
# File 'app/controllers/images_controller.rb', line 954 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 |