echo_uploads 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/echo_uploads/file.rb +15 -3
- data/lib/echo_uploads/model.rb +29 -4
- data/lib/echo_uploads/perm_file_saving.rb +1 -1
- data/lib/echo_uploads/temp_file_saving.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b33b3e17496be452917ef4e82609a8fb39348ad4
|
4
|
+
data.tar.gz: 24efa6d8430db50e74d7079cf927ea2216a52917
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d29afadb8644e6681922d70c5ebf9799cccff6cf1cd438e5596694a1304474e30189b4dd666b81a544e9890b58b9354e6a1bec1d4669a324fc8709e6c56d14f7
|
7
|
+
data.tar.gz: 10f158f2e2d911ab0f54490b7d8f216b720d8316692601c68dc1e5acfc017f5b89c2cbbd1a1716838cf962a4e9e096e38aa3e292526e35358c3561dc2b89eb29
|
data/lib/echo_uploads/file.rb
CHANGED
@@ -40,9 +40,12 @@ module EchoUploads
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# Pass in an attribute name, an ActionDispatch::UploadedFile, and an options hash.
|
43
|
-
def persist!(attr, file, options)
|
43
|
+
def persist!(attr, file, mapped_file, options)
|
44
|
+
# If .echo_uploads was called with the :map option, we need to use the mapped file.
|
45
|
+
file_to_write = mapped_file || file
|
46
|
+
|
44
47
|
# Configure and save the metadata object.
|
45
|
-
self.key = options[:key].call
|
48
|
+
self.key = options[:key].call file_to_write
|
46
49
|
self.owner_attr = attr
|
47
50
|
self.original_extension = ::File.extname(file.original_filename)
|
48
51
|
self.original_basename = ::File.basename(file.original_filename, original_extension)
|
@@ -51,7 +54,16 @@ module EchoUploads
|
|
51
54
|
save!
|
52
55
|
|
53
56
|
# Write the file to the filestore.
|
54
|
-
|
57
|
+
|
58
|
+
if file_to_write.is_a?(ActionDispatch::Http::UploadedFile)
|
59
|
+
storage.write key, file_to_write.tempfile
|
60
|
+
else
|
61
|
+
storage.write key, file_to_write
|
62
|
+
end
|
63
|
+
if file_to_write == mapped_file
|
64
|
+
mapped_file.close
|
65
|
+
#::File.delete mapped_file.path
|
66
|
+
end
|
55
67
|
|
56
68
|
# Prune any expired temporary files. (Unless automatic pruning was turned off in
|
57
69
|
# the app config.)
|
data/lib/echo_uploads/model.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'base64'
|
2
2
|
require 'json'
|
3
3
|
require 'fileutils'
|
4
|
+
require 'securerandom'
|
4
5
|
|
5
6
|
module EchoUploads
|
6
7
|
module Model
|
@@ -32,8 +33,9 @@ module EchoUploads
|
|
32
33
|
parsed.each do |attr, attr_data|
|
33
34
|
# Must verify that the metadata record is temporary. If not, an attacker could
|
34
35
|
# pass the ID of a permanent record and change its owner.
|
35
|
-
meta = ::EchoUploads::File.where(key: attr_data['key'], temporary: true).
|
36
|
-
|
36
|
+
if meta = ::EchoUploads::File.where(id: attr_data['id'], key: attr_data['key'], temporary: true).first
|
37
|
+
send("#{attr}_tmp_metadata=", meta)
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
@@ -53,6 +55,11 @@ module EchoUploads
|
|
53
55
|
# +1.day+.
|
54
56
|
# - +storage+: A class that persists uploaded files to disk, to the cloud, or to
|
55
57
|
# wherever else you want. Defaults to +EchoUploads::FilesystemStore+.
|
58
|
+
# - +map+: A Proc that accepts an ActionDispatch::Htttp::UploadedFile and a path to
|
59
|
+
# a temporary file. It should transform the file data (e.g. scaling an image). It
|
60
|
+
# should then write the transformed data to the temporary file path. Can also
|
61
|
+
# accept a symbol naming an an instance method that works the same way as the
|
62
|
+
# previously described Proc.
|
56
63
|
def echo_upload(attr, options = {})
|
57
64
|
options = {
|
58
65
|
expires: 1.day,
|
@@ -67,8 +74,26 @@ module EchoUploads
|
|
67
74
|
self.echo_uploads_config ||= {}
|
68
75
|
self.echo_uploads_config = echo_uploads_config.merge attr => {}
|
69
76
|
|
70
|
-
# Define reader
|
71
|
-
|
77
|
+
# Define reader method for the file attribute.
|
78
|
+
attr_reader attr
|
79
|
+
|
80
|
+
# Define the writer method for the file attribute.
|
81
|
+
define_method("#{attr}=") do |file|
|
82
|
+
if options[:map]
|
83
|
+
mapped_file_path = ::File.join Rails.root, 'tmp', SecureRandom.hex(15)
|
84
|
+
if options[:map].is_a? Proc
|
85
|
+
options[:map].call file, mapped_file_path
|
86
|
+
else
|
87
|
+
send(options[:map], file, mapped_file_path)
|
88
|
+
end
|
89
|
+
mapped_file = ::File.open mapped_file_path, 'rb'
|
90
|
+
send "mapped_#{attr}=", mapped_file
|
91
|
+
end
|
92
|
+
instance_variable_set "@#{attr}", file
|
93
|
+
end
|
94
|
+
|
95
|
+
# Define the accessor methods for the mapped version of the file.
|
96
|
+
attr_accessor "mapped_#{attr}"
|
72
97
|
|
73
98
|
# Define the path method. This method will raise if the given storage
|
74
99
|
# class doesn't support the #path method.
|
@@ -20,7 +20,7 @@ module EchoUploads
|
|
20
20
|
meta = ::EchoUploads::File.new(owner: model, temporary: false)
|
21
21
|
send("#{attr}_metadata=", meta)
|
22
22
|
end
|
23
|
-
meta.persist! attr, file, options
|
23
|
+
meta.persist! attr, file, send("mapped_#{attr}"), options
|
24
24
|
elsif meta = send("#{attr}_tmp_metadata") and meta.temporary
|
25
25
|
# A file has not been uploaded during this request cycle. However, the
|
26
26
|
# submitted form "remembered" a temporary metadata record that was previously
|
@@ -31,7 +31,7 @@ module EchoUploads
|
|
31
31
|
meta = ::EchoUploads::File.new(
|
32
32
|
owner: nil, temporary: true, expires_at: options[:expires].from_now
|
33
33
|
)
|
34
|
-
meta.persist! attr, file, options
|
34
|
+
meta.persist! attr, file, send("mapped_#{attr}"), options
|
35
35
|
send("#{attr}_tmp_metadata=", meta)
|
36
36
|
end
|
37
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: echo_uploads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarrett Colby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mime-types
|