Skip to content
Browse files

Implement Imagery#update_size.

  • Loading branch information...
1 parent ade38b5 commit ce3721a8b514624647be60ee09afe230a9b59c50 @rstacruz committed
Showing with 59 additions and 10 deletions.
  1. +13 −1 lib/imagery.rb
  2. +8 −0 lib/imagery/faking.rb
  3. +18 −9 lib/imagery/s3.rb
  4. BIN test/fixtures/r8-crop.jpg
  5. +4 −0 test/helper.rb
  6. +14 −0 test/imagery.rb
  7. +2 −0 test/s3.rb
View
14 lib/imagery.rb
@@ -75,7 +75,19 @@ def save(io, key = nil)
GM.convert root(ext(@original)), root(ext(size)), resize, extent
end
end
-
+
+ # Updates a certain size (`size`) with another file.
+ #
+ # The second parameter `io` can be a File IO, or a filename string.
+ def update_size(size, io)
+ resize, extent = sizes[size.to_sym]
+ filename = io.respond_to?(:path) ? io.path : io.to_s
+
+ if resize && File.exist?(filename)
+ GM.convert filename, root(ext(size)), resize, extent
+ end
+ end
+
# A very simple and destructive method. Deletes the entire folder
# for the current prefix/key combination.
def delete
View
8 lib/imagery/faking.rb
@@ -81,6 +81,14 @@ def save(io, key = nil)
# Implement the stubbed version of save and skips actual operation
# if Imagery::Model.mode == :fake
+ def update_size(size, io)
+ return true if self.class.mode == :fake
+
+ super
+ end
+
+ # Implement the stubbed version of save and skips actual operation
+ # if Imagery::Model.mode == :fake
def delete
return true if self.class.mode == :fake
View
27 lib/imagery/s3.rb
@@ -77,15 +77,24 @@ def delete
def save(io, key = nil)
super
- keys.each do |file|
- Gateway.store(s3_key(file),
- File.open(root(ext(file))),
- self.class.s3_bucket,
- :access => :public_read,
- :content_type => "image/jpeg",
- "Cache-Control" => "max-age=315360000"
- )
- end
+ keys.each { |file| store file }
+ end
+
+ def update_size(size, io)
+ super
+
+ store size
+ end
+
+ private
+ def store(file)
+ Gateway.store(s3_key(file),
+ File.open(root(ext(file))),
+ self.class.s3_bucket,
+ :access => :public_read,
+ :content_type => "image/jpeg",
+ "Cache-Control" => "max-age=315360000"
+ )
end
# Provides a convenience wrapper around AWS::S3::S3Object and
View
BIN test/fixtures/r8-crop.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
4 test/helper.rb
@@ -3,6 +3,10 @@
require "cutest"
require "imagery"
+def assert_equal(left, right)
+ assert left == right
+end
+
def fixture(filename)
File.expand_path("fixtures/#{filename}", File.dirname(__FILE__))
end
View
14 test/imagery.rb
@@ -112,6 +112,20 @@
# Like small.jpg, it will be resized to fit within 30x30
assert_equal "30x23", resolution(im.root("tiny.jpg"))
end
+
+ test "update a size" do |im, io|
+ assert im.save(io)
+
+ FileUtils.rm im.root("small.jpg")
+ assert ! File.exist?(im.root("small.jpg"))
+
+ thumb_io = File.open(fixture("r8-crop.jpg"), "rb")
+
+ im.update_size :small, thumb_io
+ assert File.exist?(im.root("small.jpg"))
+
+ assert_equal "100x75", resolution(im.root("small.jpg"))
+ end
end
# resizing with extent
View
2 test/s3.rb
@@ -68,12 +68,14 @@ class Imagery
test "saves all sizes to S3" do |im, io|
im.save(io)
+ im.update_size(:small, io)
cmds = Imagery::S3::Gateway.commands
assert_equal [:store, "avatar/1001/original.jpg", "buck"], cmds.shift
assert_equal [:store, "avatar/1001/small.jpg", "buck"], cmds.shift
assert_equal [:store, "avatar/1001/medium.jpg", "buck"], cmds.shift
+ assert_equal [:store, "avatar/1001/small.jpg", "buck"], cmds.shift
assert cmds.empty?
end

0 comments on commit ce3721a

Please sign in to comment.
Something went wrong with that request. Please try again.