echo_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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e65bbb9b899f8a3cd419761cf5166a932a57e24
4
- data.tar.gz: 86788f880e33a403a1be30d2af07b9c6d3e4a290
3
+ metadata.gz: b33b3e17496be452917ef4e82609a8fb39348ad4
4
+ data.tar.gz: 24efa6d8430db50e74d7079cf927ea2216a52917
5
5
  SHA512:
6
- metadata.gz: 2d08e267fced64cc44f0212d81a36e4b79a709f734cdf5f8568d2883fc1ae5cc636a8d80d5fcf4d968dd20f89329f8058ef02a6423a79b005b1c7cd014454f36
7
- data.tar.gz: 9a48ac94ec4d61ece3aae321ee88fe41bc09fe6a81112204d13919d4ded551756b9af34144673f9dd68c4ed1f048683a449dfb86a71e0ba41cc347020c531cc3
6
+ metadata.gz: d29afadb8644e6681922d70c5ebf9799cccff6cf1cd438e5596694a1304474e30189b4dd666b81a544e9890b58b9354e6a1bec1d4669a324fc8709e6c56d14f7
7
+ data.tar.gz: 10f158f2e2d911ab0f54490b7d8f216b720d8316692601c68dc1e5acfc017f5b89c2cbbd1a1716838cf962a4e9e096e38aa3e292526e35358c3561dc2b89eb29
@@ -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 file
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
- storage.write key, file
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.)
@@ -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).find(attr_data['id'])
36
- send("#{attr}_tmp_metadata=", meta)
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 and writer methods for the file attribute.
71
- attr_accessor attr
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.1
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-07 00:00:00.000000000 Z
11
+ date: 2014-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types