Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove question mark from Paperclip-generated files in Ruby on Rails 3.2.6

I'm using Paperclip-FFMEG to upload video files to my development environment (and, eventually, to a local server when my project goes into production).

When videos are uploaded, the filename is, by default, as follows:

/system/modelnames/paperclipnames/.../mynewfile.mp4?xxxxxxxxxx

I believe the 10 digit figure after the questionmark is a timestamp.

However, the player I will be using to play the videos doesn't like to have anything after the file attachment - so I would like to strip the questionmark, and the timestamp after it, before passing the URL into the player.

I tried to use the following Ruby (I think) strip function:

temp_variable = model.paperclipattribute.url(:blah).strip('?')[0]

However, Rails throws up an error:

wrong number of arguments(1 for 0)

I take it I'm doing this wrong? Any other solutions? I don't want to switch off timestamps entirely, as I only need to do so in this situation.

Thanks!

like image 885
Graeme Avatar asked Aug 03 '12 19:08

Graeme


3 Answers

If you want to do this everywhere for a given attachment and without the need to pass the extra parameter, you can set the use_timestamp option when calling the has_attached_file method in your model. So, to build on the example given in the Paperclip README:

has_attached_file :avatar,
  :styles => { :medium => "300x300>", :thumb => "100x100>" },
  :default_url => "/images/:style/missing.png",
  :use_timestamp => false
like image 92
douglasr Avatar answered Nov 07 '22 08:11

douglasr


Hope this is OK to put as an answer to my own question (as it may be useful for others who stumble across this post), but I've since discovered that an alternative (and more appropriate) way to deal with this issue is to add the false parameter to URL() as follows when displaying the content in your view:

model.paperclipattribute.url(:whateverstyle, false)

The timestamp will automatically be removed. I think this is better, as the split method I suggested might remove content you don't intend to remove - for example, if your file is called something like "Is_this_a_question_?_Yes_it_is.mp4?xxxxxx", then everything after the first question mark might be removed (i.e. the file will be read as "Is this a question_", thus corrupting the filename.

I haven't tested this, so I may be wrong.

like image 26
Graeme Avatar answered Nov 07 '22 08:11

Graeme


Globally default them to off, just put this in a config/initializers/paperclip.rb file.

Paperclip::Attachment.default_options[:use_timestamp] = false

like image 5
onurozgurozkan Avatar answered Nov 07 '22 08:11

onurozgurozkan