Skip to content

Commit

Permalink
Tweaks to URI handling
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@196 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jyurek committed Aug 27, 2007
1 parent 439879e commit 0919884
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 12 additions & 5 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ def has_attached_file *attachment_names
attachments[attr] = (attachments[attr] || {:name => attr}).merge(options)

define_method "#{attr}=" do |uploaded_file|
return send("destroy_#{attr}") if uploaded_file.nil?
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

attachments[attr].merge!({
Expand Down Expand Up @@ -247,8 +247,6 @@ def has_attached_file *attachment_names
write_attachment attachments[attr] if attachments[attr][:files]
delete_attachment attachments[attr], attachments[attr][:complain_on_delete] if attachments[attr][:delete_on_save]
attachments[attr][:delete_on_save] = false
attachments[attr][:dirty] = false
attachments[attr][:files] = nil
end
end
private :"#{attr}_before_save"
Expand Down Expand Up @@ -285,7 +283,11 @@ def interpolate attachment, source, style
s.gsub!(/:class/, self.class.to_s.underscore.pluralize)
s.gsub!(/:style/, style.to_s)
s.gsub!(/:attachment/, attachment[:name].to_s.pluralize)
s.gsub!(/:name/, file_name) if file_name
if file_name
s.gsub!(/:name/, file_name)
s.gsub!(/:base/, file_name.split(".")[0..-2].join(".") )
s.gsub!(/:ext/, file_name.split(".").last )
end
end
end

Expand Down Expand Up @@ -322,6 +324,8 @@ def write_attachment attachment
file.write(atch.read)
end
end
attachment[:files] = nil
attachment[:dirty] = false
end

def delete_attachment attachment, complain = false
Expand Down Expand Up @@ -393,12 +397,12 @@ def geometry_for_crop geometry, orig_io
end

def fetch_uri uri
require 'open-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

Expand All @@ -410,6 +414,9 @@ def fetch_uri uri
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
Expand Down
4 changes: 0 additions & 4 deletions tasks/paperclip_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
# desc "Explaining what the task does"
# task :paperclip do
# # Task goes here
# end

0 comments on commit 0919884

Please sign in to comment.