@@ -19,18 +19,26 @@ class Document < ApplicationRecord
1919 validates :purpose , :filename , :bytes , presence : true
2020 validate :file_present
2121
22- def file_data_url ( variant = :large )
23- return nil if !file . attached?
22+ def has_image? ( variant = nil )
23+ if variant . present?
24+ return has_file_variant_processed? ( variant )
25+ end
2426
25- "data: #{ file . blob . content_type } ;base64, #{ file_base64 ( variant ) } "
27+ file . attached?
2628 end
2729
28- def file_base64 ( variant = :large )
29- return nil if !file . attached?
30- wait_for_file_variant_to_process! ( variant . to_sym )
31-
32- file_contents = file . variant ( variant . to_sym ) . processed . download
33- base64 = Base64 . strict_encode64 ( file_contents )
30+ def image_url ( variant , fallback : nil )
31+ return nil unless has_image?
32+
33+ if Rails . application . config . x . app_url . blank?
34+ file_data_url ( variant )
35+ elsif has_file_variant_processed? ( variant )
36+ fully_processed_url ( variant )
37+ elsif fallback . nil?
38+ redirect_to_processed_path ( variant )
39+ else
40+ fallback
41+ end
3442 end
3543
3644 def has_file_variant_processed? ( variant )
@@ -57,6 +65,15 @@ def redirect_to_processed_path(variant)
5765
5866 private
5967
68+ def file_data_url ( variant = :large )
69+ wait_for_file_variant_to_process! ( variant )
70+
71+ file_contents = file . variant ( variant . to_sym ) . processed . download
72+ base64_data = Base64 . strict_encode64 ( file_contents )
73+
74+ "data:#{ file . blob . content_type } ;base64,#{ base64_data } "
75+ end
76+
6077 def set_default_user
6178 self . user ||= message . conversation . user
6279 end
0 commit comments