cached_uploads 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cached_uploads.rb +21 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37755d8531f052e5a14e8336dec883291e46921d
|
4
|
+
data.tar.gz: e5034fece7ace248880a90234fb09cc87ad52854
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f4e68a60c40926b88675c1d76ed840f75d7cba545674014ace5651bb4f03e30c45721c62ef8e31834d49167eccc5fbef4f180855d81795e2678621401264d37
|
7
|
+
data.tar.gz: bf37b9d143c8ca6417fb05c3bcc4bd38ad098c6558837f20ab072ec50f7fc0aa10702b474824122f106fa586a558597f5384c867189db5f67e585494e792d1a8
|
data/lib/cached_uploads.rb
CHANGED
@@ -27,13 +27,14 @@ module CachedUploads
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
30
|
+
def write_permanent_file_md5(file_attr)
|
31
31
|
config = self.class.cached_uploads[file_attr.to_sym]
|
32
32
|
file = send file_attr
|
33
|
-
|
33
|
+
method = "#{config[:prm_md5_attr]}="
|
34
|
+
if file.present? and respond_to?(method)
|
34
35
|
file.rewind
|
35
36
|
md5 = Digest::MD5.hexdigest(file.read)
|
36
|
-
send
|
37
|
+
send method, md5
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
@@ -52,7 +53,7 @@ module CachedUploads
|
|
52
53
|
uploaded_file.rewind
|
53
54
|
out_file.write uploaded_file.read
|
54
55
|
end
|
55
|
-
elsif send(config[:
|
56
|
+
elsif send(config[:tmp_md5_attr]).present?
|
56
57
|
# This executes if we've set the temporary file MD5 attribute instead of the file
|
57
58
|
# attribute. This is invoked when the user has submitted invalid data at least once.
|
58
59
|
# In which case we've saved the uploaded data to a tempfile on the server. Now the
|
@@ -75,7 +76,7 @@ module CachedUploads
|
|
75
76
|
# Read the uploaded file, calc its MD5, and write the MD5 instance variable.
|
76
77
|
file.rewind
|
77
78
|
md5 = Digest::MD5.hexdigest(file.read)
|
78
|
-
send "#{config[:
|
79
|
+
send "#{config[:tmp_md5_attr]}=", md5
|
79
80
|
|
80
81
|
# Write the temporary file, using its MD5 hash to generate the filename.
|
81
82
|
file.rewind
|
@@ -173,10 +174,14 @@ module CachedUploads
|
|
173
174
|
# to +"#{file_attr}_ext"+. CachedUploads does not define this method for you.
|
174
175
|
# Typically, this attribute would be a database column.
|
175
176
|
#
|
176
|
-
# - +
|
177
|
-
# to +"#{file_attr}_md5"+.
|
178
|
-
# column
|
179
|
-
#
|
177
|
+
# - +prm_md5_attr+: Name of the instance attribute storing the permanent file's MD5
|
178
|
+
# hash. Defaults to +"#{file_attr}_md5"+. If this attribute exists, it should be a
|
179
|
+
# database column, but it need not exist at all.
|
180
|
+
#
|
181
|
+
# - +tmp_md5_attr+: Name of the instance attribute storing the temporary file's MD5
|
182
|
+
# hash. Defaults to +"tmp_#{file_attr}_md5"+. This attribute is typically not a
|
183
|
+
# database column. If you don't define it yourself, CachedUploads will define it
|
184
|
+
# for you.
|
180
185
|
#
|
181
186
|
# - +no_prm:+ If set to true, permanent files won't be written to disk. You might
|
182
187
|
# want to use this if, for example, you're hosting uploaded files on an external
|
@@ -190,7 +195,8 @@ module CachedUploads
|
|
190
195
|
tmp_folder_method: "tmp_#{file_attr}_folder",
|
191
196
|
tmp_file_expiration: 48.hours,
|
192
197
|
ext_attr: "#{file_attr}_ext",
|
193
|
-
|
198
|
+
prm_md5_attr: "#{file_attr}_md5",
|
199
|
+
tmp_md5_attr: "tmp_#{file_attr}_md5"
|
194
200
|
)
|
195
201
|
|
196
202
|
# Initialize the configs hash.
|
@@ -210,13 +216,10 @@ module CachedUploads
|
|
210
216
|
end
|
211
217
|
)
|
212
218
|
|
213
|
-
# Define the accessor for the temporary file MD5 string.
|
214
|
-
#
|
215
|
-
unless
|
216
|
-
|
217
|
-
(respond_to? :columns and columns.map { |c| c.name.to_sym }.include?(options[:md5_attr].to_sym))
|
218
|
-
)
|
219
|
-
attr_accessor options[:md5_attr]
|
219
|
+
# Define the accessor for the temporary file MD5 string. This should never be a
|
220
|
+
# database column.
|
221
|
+
unless method_defined? options[:tmp_md5_attr]
|
222
|
+
attr_accessor options[:tmp_md5_attr]
|
220
223
|
end
|
221
224
|
|
222
225
|
# Define the path methods, if given.
|
@@ -263,7 +266,7 @@ module CachedUploads
|
|
263
266
|
|
264
267
|
# Register the hash writer callback.
|
265
268
|
before_save do |obj|
|
266
|
-
obj.
|
269
|
+
obj.write_permanent_file_md5 file_attr
|
267
270
|
end
|
268
271
|
end
|
269
272
|
end
|