Commit 6043576c by jyurek

URIs work like real files.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@209 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent f0283612
require 'open-uri'
require File.join(File.dirname(__FILE__), "lib", "paperclip")
ActiveRecord::Base.extend( Thoughtbot::Paperclip::ClassMethods )
File.send :include, Thoughtbot::Paperclip::Upfile
URI.send :include, Thoughtbot::Paperclip::Upfile
\ No newline at end of file
......@@ -173,7 +173,6 @@ module Thoughtbot #:nodoc:
whine_about_columns_for attachments[attr]
define_method "#{attr}=" do |uploaded_file|
uploaded_file = fetch_uri(uploaded_file) if uploaded_file.is_a? URI
return send("destroy_#{attr}") if uploaded_file.nil?
return unless is_a_file? uploaded_file
......@@ -417,31 +416,8 @@ module Thoughtbot #:nodoc:
end
end
def fetch_uri uri
# I hate the fact that URI and open-uri can't handle file:// urls.
if uri.scheme == 'file'
path = url.gsub(%r{^file://}, '/')
image_data = open(path)
else
require 'open-uri'
image_data = open(uri.to_s)
end
image = StringIO.new(image_data.read)
image_data.close
image.instance_eval { class << self; attr_accessor :content_type, :original_filename; end }
image.content_type = image_data.content_type
image.original_filename = File.basename(uri.path)
image
rescue Exception => e
self.errors.add_to_base("Could not save #{uri.to_s}: #{e}")
nil
end
def is_a_file? data
[:size, :content_type, :original_filename, :read].map do |meth|
[:content_type, :original_filename, :read].map do |meth|
data.respond_to? meth
end.all?
end
......@@ -461,7 +437,7 @@ module Thoughtbot #:nodoc:
module Upfile
# Infer the MIME-type of the file from the extension.
def content_type
type = self.path.match(/\.(\w+)$/)[1]
type = self.path.match(/\.(\w+)$/)[1] || "data"
case type
when "jpg", "png", "gif" then "image/#{type}"
when "txt", "csv", "xml", "html", "htm" then "text/#{type}"
......
......@@ -92,16 +92,31 @@ class PaperclipImagesTest < Test::Unit::TestCase
end
def test_should_save_image_from_uri
uri = URI.parse("http://thoughtbot.com/assets/11/thoughtbot.jpg")
require 'webrick'
server = WEBrick::HTTPServer.new(:Port => 40404,
:DocumentRoot => File.dirname(__FILE__),
:AccessLog => [],
:Logger => WEBrick::Log.new(nil, WEBrick::Log::WARN))
Thread.new do
server.start
end
while server.status != :Running
sleep 0.1
print "!"; $stdout.flush
end
uri = URI.parse("http://127.0.0.1:40404/fixtures/test_image.jpg")
@foo.image = uri
@foo.save
@foo.image_valid?
assert File.exists?( @foo.image_file_name(:original) ), @foo.image_file_name(:original)
assert File.exists?( @foo.image_file_name(:medium) ), @foo.image_file_name(:medium)
assert File.exists?( @foo.image_file_name(:thumb) ), @foo.image_file_name(:thumb)
out = `identify '#{@foo.image_file_name(:original)}'`; assert out.match("350x100"); assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:medium)}'`; assert out.match("300x86"); assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:thumb)}'`; assert out.match("100x29"); assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:original)}'`; assert_match "405x375", out; assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:medium)}'`; assert_match "300x278", out; assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:thumb)}'`; assert_match "100x93", out; assert $?.exitstatus == 0
ensure
server.stop if server
end
def test_should_put_errors_on_object_if_convert_does_not_exist
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment