@@ -19,18 +19,26 @@ class Document < ApplicationRecord
19
19
validates :purpose , :filename , :bytes , presence : true
20
20
validate :file_present
21
21
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
24
26
25
- "data: #{ file . blob . content_type } ;base64, #{ file_base64 ( variant ) } "
27
+ file . attached?
26
28
end
27
29
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
34
42
end
35
43
36
44
def has_file_variant_processed? ( variant )
@@ -57,6 +65,15 @@ def redirect_to_processed_path(variant)
57
65
58
66
private
59
67
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
+
60
77
def set_default_user
61
78
self . user ||= message . conversation . user
62
79
end
0 commit comments