cached_uploads 0.0.1 → 0.0.2
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 +25 -5
- 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: fde9467cec8d08ea9ae8f1111c8f4fe2fed0bfef
|
4
|
+
data.tar.gz: 4a9b1e62c06d090618f95473dd5c5612618d5ab8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7df3f750b350485861da5cfea8971627863a7781c374c4b2e7308a8feecffd139919fd27911c5bf90a49e34cf5ac21e328e70a5a15337f4b0102dd97ab07abb
|
7
|
+
data.tar.gz: 2ec34cd5dc9c55080da960808afe587dbd67ccc0ec818113d26ebb5b9de59720b44d56d76df0b81cd59b07ee5b9651abeac7b6aa466caa2bd9006c9eeca5f076
|
data/lib/cached_uploads.rb
CHANGED
@@ -27,6 +27,16 @@ module CachedUploads
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
def write_hash(file_attr)
|
31
|
+
config = self.class.cached_uploads[file_attr.to_sym]
|
32
|
+
file = send file_attr
|
33
|
+
if file.present?
|
34
|
+
file.rewind
|
35
|
+
md5 = Digest::MD5.hexdigest(file.read)
|
36
|
+
send "#{config[:md5_attr]}=", md5
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
30
40
|
def write_permanent_file(file_attr)
|
31
41
|
config = self.class.cached_uploads[file_attr.to_sym]
|
32
42
|
uploaded_file = send file_attr
|
@@ -61,7 +71,7 @@ module CachedUploads
|
|
61
71
|
config = self.class.cached_uploads[file_attr.to_sym]
|
62
72
|
file = send file_attr
|
63
73
|
|
64
|
-
if file.present?
|
74
|
+
if file.present?
|
65
75
|
# Read the uploaded file, calc its MD5, and write the MD5 instance variable.
|
66
76
|
file.rewind
|
67
77
|
md5 = Digest::MD5.hexdigest(file.read)
|
@@ -163,7 +173,9 @@ module CachedUploads
|
|
163
173
|
# to +"#{file_attr}_ext"+.
|
164
174
|
#
|
165
175
|
# - +md5_attr+: Name of the instance attribute storing the file's MD5 hash. Defaults
|
166
|
-
# to +"
|
176
|
+
# to +"#{file_attr}_md5"+. It is often wise to make this attribute a database
|
177
|
+
# column. However, if you don't, you still need to define this attribute, so use
|
178
|
+
# #attr_accessor.
|
167
179
|
#
|
168
180
|
# - +no_prm:+ If set to true, permanent files won't be written to disk. You might
|
169
181
|
# want to use this if, for example, you're hosting uploaded files on an external
|
@@ -177,7 +189,7 @@ module CachedUploads
|
|
177
189
|
tmp_folder_method: "tmp_#{file_attr}_folder",
|
178
190
|
tmp_file_expiration: 48.hours,
|
179
191
|
ext_attr: "#{file_attr}_ext",
|
180
|
-
md5_attr: "
|
192
|
+
md5_attr: "#{file_attr}_md5"
|
181
193
|
)
|
182
194
|
|
183
195
|
# Initialize the configs hash.
|
@@ -197,8 +209,11 @@ module CachedUploads
|
|
197
209
|
end
|
198
210
|
)
|
199
211
|
|
200
|
-
# Define the accessor for the temporary file MD5 string.
|
201
|
-
|
212
|
+
# Define the accessor for the temporary file MD5 string. (Unless it's already
|
213
|
+
# defined, e.g. as a database column.)
|
214
|
+
unless method_defined? options[:md5_attr]
|
215
|
+
attr_accessor options[:md5_attr]
|
216
|
+
end
|
202
217
|
|
203
218
|
# Define the path methods, if given.
|
204
219
|
if options[:folder] and options[:filename]
|
@@ -241,6 +256,11 @@ module CachedUploads
|
|
241
256
|
obj.delete_permanent_file file_attr
|
242
257
|
end
|
243
258
|
end
|
259
|
+
|
260
|
+
# Register the hash writer callback.
|
261
|
+
before_save do |obj|
|
262
|
+
obj.write_hash file_attr
|
263
|
+
end
|
244
264
|
end
|
245
265
|
end
|
246
266
|
end
|