santiago.pastorino

Paperclip file rename 12

Posted by santiago.pastorino
on Tuesday, July 14

While developing an application with Sebastián that allow users to upload videos with some file name restrictions, meaning that it must contain only A-Z and 0-9 digits, underscores (_) as a valid component as well, and also the name must be preceded by it’s own #id, we came up with the need of applying this custom filter to each uploaded video. After doing some research on paperclip source code and internet tutorials, we suggest the following solution:

class Video < ActiveRecord::Base
  has_attached_file :video,
    :path => ":rails_root/public/system/:attachment/:id/:style/:normalized_video_file_name",
    :url => "/system/:attachment/:id/:style/:normalized_video_file_name" 

  Paperclip.interpolates :normalized_video_file_name do |attachment, style|
    attachment.instance.normalized_video_file_name
  end

  def normalized_video_file_name
    "#{self.id}-#{self.video_file_name.gsub( /[^a-zA-Z0-9_\.]/, '_')}" 
  end
end
What are we doing here? Easy, in has_attached_file we edit the way paperclip returns the path and url by default, the most relevant components when saving and loading the file in order to display it. Paperclip default values are:
path default => ":rails_root/public/system/:attachment/:id/:style/:filename" 
url default => "/system/:attachment/:id/:style/:filename"

Values preceded by ’:’ are the standard interpolations paperclip has. For further information on this visit http://wiki.github.com/thoughtbot/paperclip/interpolations.

What we did was change * with *:normalized_video_file_name in both path and url, being the second a custom interpolation and then added the ‘normalized_video_file_name’ method to video.rb.

By doing this we not only achieve a way for paperclip to handle the file by this normalized way, but also have a method to access the normalized file name, plus being able to access the original file name through paperclip video_file_name method.

So remember on video_file_name you have the uploaded filename and on normalized_video_file_name you have the server filename.

| |

If you have found this material to be useful, you might
consider recommending me on Working With Rails.

Comments

Leave a response

  1. Thomas Drevon Thomas DrevonOctober 29, 2010 @ 03:12 PM

    Brilliant! Exactly the solution I was looking for :-D

  2. Chrisrtian Kipke Chrisrtian KipkeFebruary 13, 2011 @ 12:07 PM

    Perfect!

  3. Fernando Reyes Fernando ReyesJune 03, 2011 @ 12:50 AM

    Justo lo que buscaba. Excelente aporte.

  4. Jarad Downing Jarad DowningJuly 12, 2011 @ 11:26 PM

    Thank you sir! Saved me a bunch of time trying to figure this problem out.

  5. aashish kiran aashish kiranAugust 07, 2011 @ 09:26 PM

    Excellent work. It was exactly what i am looking at.

    Thanks a lot.

  6. Shishir Paudyal Shishir PaudyalSeptember 15, 2011 @ 03:20 PM

    Thanks a lot! Excellent Work!

  7. John JohnSeptember 18, 2011 @ 06:59 PM

    This is great thanks, for the explanation.

    How would you permanently change the video_file_name value to the new normalized_video_file_name?

  8. Divsss DivsssSeptember 20, 2011 @ 02:30 PM

    Excellent Tutorial…. Thanx for sharing this….

  9. Nick NickOctober 25, 2011 @ 12:49 AM

    Thank you! I’ve been trying to solve this problem all day.

  10. Floyd FloydOctober 29, 2011 @ 09:16 PM

    Just be sure you don’t get crazy and use dynamic fields in your normalize method. Then when the record changes Paperclip won’t be able to find your file.

  11. uchenna uchennaNovember 05, 2011 @ 02:44 AM

    Thanks so much for this solution i have been up all night trying to solve this

  12. DeathHammer DeathHammerNovember 22, 2011 @ 06:52 AM

    Thank you, a life saver.

Comment