@@ -16,7 +16,6 @@ class File < ApplicationRecord
16
16
17
17
after_initialize :set_default_values
18
18
before_validation :clear_weight , unless : :teacher_defined_assessment?
19
- before_validation :hash_content , if : :content_present?
20
19
before_validation :set_ancestor_values , if : :incomplete_descendent?
21
20
22
21
attr_writer :size
@@ -36,7 +35,7 @@ class File < ApplicationRecord
36
35
has_many :events_synchronized_editor , class_name : 'Event::SynchronizedEditor'
37
36
alias descendants files
38
37
39
- mount_uploader :native_file , FileUploader
38
+ has_one_attached :attachment
40
39
41
40
scope :editable , -> { where ( read_only : false ) }
42
41
scope :visible , -> { where ( hidden : false ) }
@@ -50,7 +49,6 @@ class File < ApplicationRecord
50
49
51
50
validates :feedback_message , if : :teacher_defined_assessment? , presence : true
52
51
validates :feedback_message , absence : true , unless : :teacher_defined_assessment?
53
- validates :hashed_content , if : :content_present? , presence : true
54
52
validates :hidden , inclusion : [ true , false ]
55
53
validates :hidden_feedback , inclusion : [ true , false ]
56
54
validates :name , presence : true
@@ -73,21 +71,13 @@ class File < ApplicationRecord
73
71
end
74
72
75
73
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
80
76
else
81
77
content
82
78
end
83
79
end
84
80
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
-
91
81
def ancestor_id
92
82
file_id || id
93
83
end
@@ -102,7 +92,7 @@ def teacher_defined_assessment?
102
92
end
103
93
104
94
def content_present?
105
- content? || native_file ?
95
+ content? || attachment . attached ?
106
96
end
107
97
private :content_present?
108
98
@@ -122,11 +112,6 @@ def filepath_without_extension
122
112
end
123
113
end
124
114
125
- def hash_content
126
- self . hashed_content = Digest ::MD5 . new . hexdigest ( read || '' )
127
- end
128
- private :hash_content
129
-
130
115
def incomplete_descendent?
131
116
file_id . present? && file_type_id . blank?
132
117
end
@@ -158,8 +143,8 @@ def visible?
158
143
end
159
144
160
145
def size
161
- @size ||= if native_file ?
162
- native_file . size
146
+ @size ||= if attachment . attached ?
147
+ attachment . byte_size
163
148
else
164
149
content . size
165
150
end
0 commit comments