Commit c8ad2e9c by jyurek

Updates for URI saving and thumbnails refreshing

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@216 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 5e7f9458
...@@ -173,6 +173,7 @@ module Thoughtbot #:nodoc: ...@@ -173,6 +173,7 @@ module Thoughtbot #:nodoc:
whine_about_columns_for attachments[attr] whine_about_columns_for attachments[attr]
define_method "#{attr}=" do |uploaded_file| 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 send("destroy_#{attr}") if uploaded_file.nil?
return unless is_a_file? uploaded_file return unless is_a_file? uploaded_file
...@@ -432,6 +433,29 @@ module Thoughtbot #:nodoc: ...@@ -432,6 +433,29 @@ module Thoughtbot #:nodoc:
end end
end end
def fetch_uri uri
image = if uri.scheme == 'file'
path = url.gsub(%r{^file://}, '/')
open(path)
else
require 'open-uri'
uri
end
begin
data = StringIO.new(image.read)
uri.extend(Upfile)
class << data
attr_accessor :original_filename, :content_type
end
data.original_filename = uri.original_filename
data.content_type = uri.content_type
data
rescue OpenURI::HTTPError => e
$stderr.puts "#{e.message}: #{uri.to_s}"
return nil
end
end
def is_a_file? data def is_a_file? data
[:content_type, :original_filename, :read].map do |meth| [:content_type, :original_filename, :read].map do |meth|
data.respond_to? meth data.respond_to? meth
......
...@@ -5,7 +5,7 @@ end ...@@ -5,7 +5,7 @@ end
def obtain_attachments def obtain_attachments
name = ENV['ATTACHMENT'] || ENV['attachment'] name = ENV['ATTACHMENT'] || ENV['attachment']
if name.blank? || @klass.attachment_names.include?(name) if !name.blank? && @klass.attachment_names.include?(name)
[ name ] [ name ]
else else
@klass.attachment_names @klass.attachment_names
...@@ -21,7 +21,10 @@ namespace :paperclip do ...@@ -21,7 +21,10 @@ namespace :paperclip do
puts "Regenerating thumbnails for #{instances.length} instances:" puts "Regenerating thumbnails for #{instances.length} instances:"
instances.each do |instance| instances.each do |instance|
names.each do |names| names.each do |name|
original_file = instance.send("#{name}_file_name", :original)
next if original_file.blank?
instance.send("#{name}=", File.new(original_file))
instance.send("process_#{name}_thumbnails") instance.send("process_#{name}_thumbnails")
end end
print instance.save ? "." : "x"; $stdout.flush print instance.save ? "." : "x"; $stdout.flush
......
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