@@ -3,10 +3,7 @@ class Document < ApplicationRecord
33 belongs_to :assistant , optional : true
44 belongs_to :message , optional : true
55
6- has_one_attached :file do |file |
7- file . variant :small , resize_to_limit : [ 650 , 490 ] , preprocessed : true
8- file . variant :large , resize_to_limit : [ 1200 , 900 ] , preprocessed : true
9- end
6+ has_one_attached :file
107
118 enum :purpose , %w[ fine-tune fine-tune-results assistants assistants_output ] . index_by ( &:to_sym )
129
@@ -20,11 +17,26 @@ class Document < ApplicationRecord
2017 validate :file_present
2118
2219 def has_image? ( variant = nil )
20+ return false unless file . attached? && file . content_type &.start_with? ( "image/" )
21+
2322 if variant . present?
2423 return has_file_variant_processed? ( variant )
2524 end
2625
27- file . attached?
26+ true
27+ end
28+
29+ def image_variant ( variant )
30+ return nil unless file . attached? && file . content_type &.start_with? ( "image/" )
31+
32+ case variant . to_sym
33+ when :small
34+ file . variant ( resize_to_limit : [ 650 , 490 ] )
35+ when :large
36+ file . variant ( resize_to_limit : [ 1200 , 900 ] )
37+ else
38+ file
39+ end
2840 end
2941
3042 def image_url ( variant , fallback : nil )
@@ -42,32 +54,80 @@ def image_url(variant, fallback: nil)
4254 end
4355
4456 def has_file_variant_processed? ( variant )
45- r = file . attached? &&
46- variant . present? &&
47- ( key = file . variant ( variant . to_sym ) . key ) &&
48- ActiveStorage ::Blob . service . exist? ( key )
57+ return false unless file . attached? && file . content_type &.start_with? ( "image/" )
4958
59+ variant_obj = image_variant ( variant )
60+ return false unless variant_obj
61+
62+ r = variant_obj . key && ActiveStorage ::Blob . service . exist? ( variant_obj . key )
5063 !!r
5164 end
5265
5366 def fully_processed_url ( variant )
54- file . attached? && variant . present? && file . representation ( variant . to_sym ) . processed . url
67+ return nil unless file . attached? && file . content_type &.start_with? ( "image/" )
68+
69+ variant_obj = image_variant ( variant )
70+ return nil unless variant_obj
71+
72+ variant_obj . processed . url
5573 end
5674
5775 def redirect_to_processed_path ( variant )
58- return nil unless file . attached? && variant . present?
76+ return nil unless file . attached? && file . content_type &.start_with? ( "image/" ) && variant . present?
77+
78+ variant_obj = image_variant ( variant )
79+ return nil unless variant_obj
5980
6081 Rails . application . routes . url_helpers . rails_representation_url (
61- file . representation ( variant . to_sym ) ,
82+ variant_obj ,
6283 only_path : true
6384 )
6485 end
6586
6687 def file_base64 ( variant = :large )
6788 return nil if !file . attached?
68- wait_for_file_variant_to_process! ( variant . to_sym )
69- file_contents = file . variant ( variant . to_sym ) . processed . download
70- base64 = Base64 . strict_encode64 ( file_contents )
89+
90+ if file . content_type &.start_with? ( "image/" )
91+ variant_obj = image_variant ( variant )
92+ return nil unless variant_obj
93+
94+ wait_for_file_variant_to_process! ( variant . to_sym )
95+ file_contents = variant_obj . processed . download
96+ else
97+ # For non-image files, just return the raw file content
98+ file_contents = file . download
99+ end
100+
101+ Base64 . strict_encode64 ( file_contents )
102+ end
103+
104+ def has_document_pdf?
105+ file . attached? && file . content_type == "application/pdf"
106+ end
107+
108+ def extract_pdf_text
109+ return nil unless has_document_pdf?
110+
111+ begin
112+ pdf_data = file . download
113+
114+ pdf_reader = PDF ::Reader . new ( StringIO . new ( pdf_data ) )
115+
116+ text_content = ""
117+ pdf_reader . pages . each_with_index do |page , index |
118+ page_text = page . text
119+ text_content += "--- Page #{ index + 1 } ---\n " if pdf_reader . pages . count > 1
120+ text_content += page_text + "\n \n "
121+ end
122+ text_content . strip
123+
124+ rescue PDF ::Reader ::MalformedPDFError => e
125+ Rails . logger . error "PDF file is corrupted or malformed: #{ e . message } "
126+ nil
127+ rescue => e
128+ Rails . logger . error "Error processing PDF: #{ e . message } "
129+ nil
130+ end
71131 end
72132
73133 private
@@ -93,6 +153,11 @@ def file_present
93153 end
94154
95155 def wait_for_file_variant_to_process! ( variant )
96- file && file . attached? && file . variant ( variant . to_sym ) . processed # this blocks until processing is done
156+ return false unless file &.attached? && file . content_type &.start_with? ( "image/" )
157+
158+ variant_obj = image_variant ( variant )
159+ return false unless variant_obj
160+
161+ variant_obj . processed # this blocks until processing is done
97162 end
98163end
0 commit comments