Skip to content

Commit

Permalink
Implement Imagery#update_size.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed May 15, 2011
1 parent ade38b5 commit ce3721a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
14 changes: 13 additions & 1 deletion lib/imagery.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/imagery/faking.rb
Expand Up @@ -79,6 +79,14 @@ def save(io, key = nil)
super
end

# 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
Expand Down
27 changes: 18 additions & 9 deletions lib/imagery/s3.rb
Expand Up @@ -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
Expand Down
Binary file added 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.
4 changes: 4 additions & 0 deletions test/helper.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/imagery.rb
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/s3.rb
Expand Up @@ -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
Expand Down

0 comments on commit ce3721a

Please sign in to comment.