@@ -16,7 +16,6 @@ class File < ApplicationRecord
1616
1717 after_initialize :set_default_values
1818 before_validation :clear_weight , unless : :teacher_defined_assessment?
19- before_validation :hash_content , if : :content_present?
2019 before_validation :set_ancestor_values , if : :incomplete_descendent?
2120
2221 attr_writer :size
@@ -36,7 +35,7 @@ class File < ApplicationRecord
3635 has_many :events_synchronized_editor , class_name : 'Event::SynchronizedEditor'
3736 alias descendants files
3837
39- mount_uploader :native_file , FileUploader
38+ has_one_attached :attachment
4039
4140 scope :editable , -> { where ( read_only : false ) }
4241 scope :visible , -> { where ( hidden : false ) }
@@ -50,7 +49,6 @@ class File < ApplicationRecord
5049
5150 validates :feedback_message , if : :teacher_defined_assessment? , presence : true
5251 validates :feedback_message , absence : true , unless : :teacher_defined_assessment?
53- validates :hashed_content , if : :content_present? , presence : true
5452 validates :hidden , inclusion : [ true , false ]
5553 validates :hidden_feedback , inclusion : [ true , false ]
5654 validates :name , presence : true
@@ -73,21 +71,13 @@ class File < ApplicationRecord
7371 end
7472
7573 def read
76- if native_file?
77- return nil unless native_file_location_valid?
78-
79- native_file . read
74+ if attachment . attached?
75+ attachment . download
8076 else
8177 content
8278 end
8379 end
8480
85- def native_file_location_valid?
86- real_location = Pathname ( native_file . current_path ) . realpath
87- upload_location = Pathname ( ::File . join ( native_file . root , 'uploads' ) ) . realpath
88- real_location . fnmatch? ::File . join ( upload_location . to_s , '**' )
89- end
90-
9181 def ancestor_id
9282 file_id || id
9383 end
@@ -102,7 +92,7 @@ def teacher_defined_assessment?
10292 end
10393
10494 def content_present?
105- content? || native_file ?
95+ content? || attachment . attached ?
10696 end
10797 private :content_present?
10898
@@ -122,11 +112,6 @@ def filepath_without_extension
122112 end
123113 end
124114
125- def hash_content
126- self . hashed_content = Digest ::MD5 . new . hexdigest ( read || '' )
127- end
128- private :hash_content
129-
130115 def incomplete_descendent?
131116 file_id . present? && file_type_id . blank?
132117 end
@@ -158,8 +143,8 @@ def visible?
158143 end
159144
160145 def size
161- @size ||= if native_file ?
162- native_file . size
146+ @size ||= if attachment . attached ?
147+ attachment . byte_size
163148 else
164149 content . size
165150 end
0 commit comments