paperdragon 0.0.6 → 0.0.7

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: 7ae7a952a0d1b965849bfd22d565fe9e8a839935
4
- data.tar.gz: 9b4f84cd5d10ecc7cc66ad4e407c0f0e2396956f
3
+ metadata.gz: 00a917cff82896af01efe02f6217157cea6eb144
4
+ data.tar.gz: cd5588ff1dcb1dc713086d57b37b2ce5a12ffe8b
5
5
  SHA512:
6
- metadata.gz: 0e2550a838b86707593ba45bc64d71748bc1828099439e39cc81fd0b298f29d04460fe854619554bdd47c39a5167dbd8ec39a9835acc42a2e244bc99f803bade
7
- data.tar.gz: c1278828b15d425f0237667ec2643cdc358796f19a5e571c584b3b07dc1f87970ca51fb972b5f3f3b99240d24ac44fd29540f5929f9abbbd1249b48784b42030
6
+ metadata.gz: e2944453772c81e767aa30ead6e175ca76beb8febf43c985d6446a1ab5ae4672d1647a0e1dde5c6c020fd7857b2dccbb0a1d2ceae9bef02177726937d7a30596
7
+ data.tar.gz: a5b2009a183835567b9d5e9217416287d9659ad86f02912a5592b0b073bd53c651ef09a993236fa5d6f85d9294d93152eaa7faa85046957bdfc749921d2d774f
data/README.md CHANGED
@@ -77,7 +77,7 @@ For a better understanding and to see how simple it is, go and check out the `im
77
77
 
78
78
  ```ruby
79
79
  user.image_meta_data #=> {original: {uid: "original-logo.jpg", width: 240, height: 800},
80
- # thumb: {uid: "thumb-logo.jpg", width: 48, height: 48},
80
+ # thumb: {uid: "thumb-logo.jpg", width: 140, height: 140},
81
81
  # ..and so on..
82
82
  # }
83
83
  ```
@@ -91,7 +91,7 @@ After processing, you may want to render those image versions in your app.
91
91
  user.image[:thumb].url
92
92
  ```
93
93
 
94
- This is all you need to retrieve the URL/path for a stored image. Use this for your image tags
94
+ This is all you need to retrieve the URL/path for a stored image. Use this for your image tags.
95
95
 
96
96
  ```haml
97
97
  = img_tag user.image[:thumb].url
@@ -174,6 +174,29 @@ end
174
174
  This will also remove the associated metadata from the model.
175
175
 
176
176
 
177
+ ## Replacing Images
178
+
179
+ It's ok to run `#process!` again on a model with an existing attachment.
180
+
181
+ ```ruby
182
+ user.image_meta_data #=> {original: {uid: "original-logo-1234567890.jpg", ..},
183
+ ```
184
+
185
+ Processing here will overwrite the existing attachment.
186
+
187
+ ```ruby
188
+ user.image(new_file) do |v|
189
+ v.process!(:original) # overwrites the existing, deletes old.
190
+ end
191
+ ```
192
+
193
+ ```ruby
194
+ user.image_meta_data #=> {original: {uid: "original-new-file01.jpg", ..},
195
+ ```
196
+
197
+ While replacing the old with the new upload, the old file also gets deleted.
198
+
199
+
177
200
  ## Fingerprints
178
201
 
179
202
  Paperdragon comes with a very simple built-in file naming.
@@ -223,7 +246,7 @@ class Attachment < Paperdragon::Attachment
223
246
  # def build_uid and the other code from above..
224
247
 
225
248
  def rebuild_uid(file, fingerprint)
226
- file.uid.sub("logo.png", "logo-#{fingerprint".png)
249
+ file.uid.sub("logo.png", "logo-#{fingerprint}.png")
227
250
  end
228
251
  end
229
252
  ```
@@ -311,4 +334,4 @@ class User < ActiveRecord::Base
311
334
  include Paperdragon::Paperclip::Model
312
335
  ```
313
336
 
314
- This will allow both APIs for a smooth transition.
337
+ This will allow both APIs for a smooth transition.
@@ -0,0 +1,3 @@
1
+ # 0.0.7
2
+
3
+ * `Task#process!` (and the delegated `File#process!`) now delete the original version of an attachment if `process!` is used to replace the latter.
@@ -36,6 +36,11 @@ module Paperdragon
36
36
  task.metadata_hash
37
37
  end
38
38
 
39
+ # Computes UID when File doesn't have one, yet. Called in #initialize.
40
+ def uid_from(*args)
41
+ build_uid(*args)
42
+ end
43
+
39
44
  # Per default, paperdragon tries to increment the fingerprint in the file name, identified by
40
45
  # the pattern <tt>/-\d{10}/</tt> just before the filename extension (.png).
41
46
  def rebuild_uid(file, fingerprint=nil) # the signature of this method is to be considered semi-private.
@@ -57,11 +62,6 @@ module Paperdragon
57
62
  private
58
63
  attr_reader :options
59
64
 
60
- # Computes UID when File doesn't have one, yet. Called in #initialize.
61
- def uid_from(*args)
62
- build_uid(*args)
63
- end
64
-
65
65
  def build_uid(style, file)
66
66
  # can we use Dragonfly's API here?
67
67
  "#{style}-#{Dragonfly::TempObject.new(file).original_filename}"
@@ -2,14 +2,28 @@ module Paperdragon
2
2
  class File
3
3
  # DISCUSS: allow the metadata passing here or not?
4
4
  module Process
5
- def process!(file, metadata={})
5
+ def process!(file, new_uid=nil, metadata={})
6
6
  job = Dragonfly.app.new_job(file)
7
7
 
8
8
  yield job if block_given?
9
9
 
10
+ old_uid = uid
11
+ uid!(new_uid) if new_uid # set new uid if this is a replace.
12
+
13
+ upload!(job, old_uid, new_uid, metadata)
14
+ end
15
+
16
+ private
17
+ # Upload file, delete old file if there is one.
18
+ def upload!(job, old_uid, new_uid, metadata)
10
19
  puts "........................STORE (process): #{uid}"
11
20
  job.store(path: uid, :headers => {'x-amz-acl' => 'public-read', "Content-Type" => "image/jpeg"})
12
21
 
22
+ if new_uid # new uid means delete old one.
23
+ puts "........................DELETE (reprocess): #{old_uid}"
24
+ Dragonfly.app.destroy(old_uid)
25
+ end
26
+
13
27
  @data = nil
14
28
  metadata_for(job, metadata)
15
29
  end
@@ -25,22 +39,15 @@ module Paperdragon
25
39
 
26
40
 
27
41
  module Reprocess
28
- def reprocess!(fingerprint, original, metadata={})
42
+ def reprocess!(new_uid, original, metadata={})
29
43
  job = Dragonfly.app.new_job(original.data) # inheritance here somehow?
30
44
 
31
45
  yield job if block_given?
32
46
 
33
47
  old_uid = uid
34
- uid!(fingerprint) # new UID is computed and set.
48
+ uid!(new_uid) # new UID is already computed and set.
35
49
 
36
- puts "........................STORE (reprocess): #{uid}"
37
- job.store(path: uid, headers: {'x-amz-acl' => 'public-read', "Content-Type" => "image/jpeg"}) # store with thumb url.
38
-
39
- puts "........................DELETE (reprocess): #{old_uid}"
40
- Dragonfly.app.destroy(old_uid)
41
-
42
- @data = nil
43
- metadata_for(job, metadata)
50
+ upload!(job, old_uid, new_uid, metadata)
44
51
  end
45
52
  end
46
53
 
@@ -19,7 +19,10 @@ module Paperdragon
19
19
  # metadata = version.process!(upload, &block)
20
20
  # merge! {style => metadata}
21
21
  def process!(style, &block)
22
- @metadata.merge!(style => file(style, upload).process!(upload, &block))
22
+ version = file(style, upload)
23
+ new_uid = new_uid_for(style, version) # new uid when overwriting existing attachment.
24
+
25
+ @metadata.merge!(style => version.process!(upload, new_uid, &block))
23
26
  end
24
27
 
25
28
  # fingerprint optional => filename is gonna remain the same
@@ -47,5 +50,11 @@ module Paperdragon
47
50
  def upload
48
51
  @upload or raise MissingUploadError.new("You called #process! but didn't pass an uploaded file to Attachment#task.")
49
52
  end
53
+
54
+ # Returns new UID for new file when overriding an existing attachment with #process!.
55
+ def new_uid_for(style, version)
56
+ # check if UID is present in existing metadata.
57
+ @attachment.metadata[style][:uid] ? @attachment.uid_from(style, upload) : nil # DISCUSS: move to Attachment?
58
+ end
50
59
  end
51
60
  end
@@ -1,3 +1,3 @@
1
1
  module Paperdragon
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -56,7 +56,7 @@ class PaperdragonFileTest < MiniTest::Spec
56
56
 
57
57
  # additional metadata
58
58
  it do
59
- file.process!(logo, :cropping => "16x16") do |job|
59
+ file.process!(logo, nil, :cropping => "16x16") do |job|
60
60
  job.thumb!("16x16")
61
61
  end.must_equal({:width=>16, :height=>5, :uid=>uid, :cropping=>"16x16"})
62
62
  end
Binary file
@@ -37,7 +37,24 @@ class TaskSpec < MiniTest::Spec
37
37
  subject.process!(:original)
38
38
  subject.process!(:thumb) { |j| j.thumb!("16x16") }
39
39
 
40
- subject.metadata_hash.must_equal({:original=>{:width=>216, :height=>63, :uid=>"original-apotomo.png"}, :thumb=>{:width=>16, :height=>5, :uid=>"thumb-apotomo.png"}})
40
+ original_metadata = subject.metadata_hash
41
+ original_metadata.must_equal({:original=>{:width=>216, :height=>63, :uid=>"original-apotomo.png"}, :thumb=>{:width=>16, :height=>5, :uid=>"thumb-apotomo.png"}})
42
+ # processed files must exist.
43
+ exists?(original_metadata[:original][:uid]).must_equal true
44
+ exists?(original_metadata[:thumb][:uid]).must_equal true
45
+
46
+ # calling #process! again, with existing metadata, means OVERWRITING the existing image.
47
+ task = Attachment.new(subject.metadata_hash).task(Pathname("test/fixtures/trb.png"))
48
+ task.process!(:thumb) { |j| j.thumb!("48x48") }
49
+
50
+ overwritten_metadata = task.metadata_hash
51
+ overwritten_metadata.must_equal({:original=>{:width=>216, :height=>63, :uid=>"original-apotomo.png"}, :thumb=>{:width=>48, :height=>48, :uid=>"thumb-trb.png"}})
52
+ # original must still exist.
53
+ exists?(overwritten_metadata[:original][:uid]).must_equal true
54
+ # old thumb must be deleted.
55
+ exists?(original_metadata[:thumb][:uid]).must_equal false
56
+ # new thumb must be existing
57
+ exists?(overwritten_metadata[:thumb][:uid]).must_equal true
41
58
  end
42
59
 
43
60
  it do
@@ -45,6 +62,14 @@ class TaskSpec < MiniTest::Spec
45
62
  Attachment.new(nil).task.process!(:original)
46
63
  end
47
64
  end
65
+
66
+ it do
67
+ # after uploading "new", only delete when uid was in metadata.
68
+ existing_metadata = {processing: true}
69
+ metadata = Attachment.new(existing_metadata).task(logo).process!(:thumb)
70
+ metadata.must_equal({:processing=>true, :thumb=>{:width=>216, :height=>63, :uid=>"thumb-apotomo.png"}})
71
+ exists?("thumb-apotomo.png").must_equal true
72
+ end
48
73
  end
49
74
 
50
75
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperdragon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-27 00:00:00.000000000 Z
11
+ date: 2014-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dragonfly
@@ -93,6 +93,7 @@ files:
93
93
  - README.md
94
94
  - Rakefile
95
95
  - lib/paperdragon.rb
96
+ - lib/paperdragon/CHANGES.md
96
97
  - lib/paperdragon/attachment.rb
97
98
  - lib/paperdragon/file.rb
98
99
  - lib/paperdragon/file/operations.rb
@@ -106,6 +107,7 @@ files:
106
107
  - test/attachment_test.rb
107
108
  - test/file_test.rb
108
109
  - test/fixtures/apotomo.png
110
+ - test/fixtures/trb.png
109
111
  - test/metadata_test.rb
110
112
  - test/model_test.rb
111
113
  - test/paperclip_uid_test.rb
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
133
  version: '0'
132
134
  requirements: []
133
135
  rubyforge_project:
134
- rubygems_version: 2.2.1
136
+ rubygems_version: 2.2.2
135
137
  signing_key:
136
138
  specification_version: 4
137
139
  summary: Explicit image processing based on Dragonfly with Paperclip compatibility.
@@ -139,9 +141,9 @@ test_files:
139
141
  - test/attachment_test.rb
140
142
  - test/file_test.rb
141
143
  - test/fixtures/apotomo.png
144
+ - test/fixtures/trb.png
142
145
  - test/metadata_test.rb
143
146
  - test/model_test.rb
144
147
  - test/paperclip_uid_test.rb
145
148
  - test/task_test.rb
146
149
  - test/test_helper.rb
147
- has_rdoc: