<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: attachment_fu code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:21:12 GMT</pubDate>
    <description>DZone Snippets: attachment_fu code</description>
    <item>
      <title>Move acts_as_attachment / attachment_fu images</title>
      <link>http://snippets.dzone.com/posts/show/5468</link>
      <description>Use this to move files from the old directory structure (before 0000 format) to new one&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Dir.new(File.dirname($PROGRAM_NAME)).each do |f|&lt;br /&gt;  next if f == '0000' || f == 'move.rb' || f == '..' || f == '.'&lt;br /&gt;  &lt;br /&gt;  new_name = sprintf("%04d", f.to_i)&lt;br /&gt;  &lt;br /&gt;  puts "Moving #{f} to #{new_name}"&lt;br /&gt;  puts `mv #{f} 0000/#{name}`&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 06 May 2008 14:39:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5468</guid>
      <author>mloughran ()</author>
    </item>
    <item>
      <title>Make a remote URL work like a file upload (in Rails)</title>
      <link>http://snippets.dzone.com/posts/show/3994</link>
      <description>Want to load a remote URL into an acts_as_attachment/attachment_fu model?  Use this little utility class.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  class UrlUpload&lt;br /&gt;    EXTENSIONS = {&lt;br /&gt;      "image/jpeg" =&gt; ["jpg", "jpeg", "jpe"],&lt;br /&gt;      "image/gif" =&gt; ["gif"],&lt;br /&gt;      "image/png" =&gt; ["png"]&lt;br /&gt;    }&lt;br /&gt;    attr_reader :original_filename, :attachment_data&lt;br /&gt;    def initialize(url)&lt;br /&gt;      @attachment_data = open(url)&lt;br /&gt;      @original_filename = determine_filename&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # Pass things like size, content_type, path on to the downloaded file&lt;br /&gt;    def method_missing(symbol, *args)&lt;br /&gt;      if self.attachment_data.respond_to? symbol&lt;br /&gt;        self.attachment_data.send symbol, *args&lt;br /&gt;      else&lt;br /&gt;        super&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    private&lt;br /&gt;      def determine_filename&lt;br /&gt;        # Grab the path - even though it could be a script and not an actual file&lt;br /&gt;        path = self.attachment_data.base_uri.path&lt;br /&gt;        # Get the filename from the path, make it lowercase to handle those&lt;br /&gt;        # crazy Win32 servers with all-caps extensions&lt;br /&gt;        filename = File.basename(path).downcase&lt;br /&gt;        # If the file extension doesn't match the content type, add it to the end, changing any existing .'s to _&lt;br /&gt;        filename = [filename.gsub(/\./, "_"), EXTENSIONS[self.content_type].first].join(".") unless EXTENSIONS[self.content_type].any? {|ext| filename.ends_with?("." + ext) }&lt;br /&gt;        # Return the result&lt;br /&gt;        filename&lt;br /&gt;      end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now when you have the URL you want to load, do something like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;@model.uploaded_data = UrlUpload.new(url)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Or better yet, make a pseudo-accessor on your aaa/attachment_fu model so you can stay "model-heavy".&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def url=(value)&lt;br /&gt;  self.uploaded_data = UrlUpload.new(value)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 11 May 2007 18:04:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3994</guid>
      <author>seancribbs (Sean Cribbs)</author>
    </item>
  </channel>
</rss>
