How to reformat the mp3 filename which is saved through paperclip - ruby-on-rails

I upload/save mp3 files through Paperclip, it transforms the name with underscores when it saves it.
For example if I upload "Gould Stokowski 1.mp3" it saves into the the db as "Gould_Stokowski_1.mp3". How can I take out the underscores (replace them with spaces" when I retrieve the file and I want to display the name.

What does the program do with the characters that started out as underscores? If it does nothing, then there is no way to go back using just the file name. The names don't "round trip."
If you're not concerned with that, then your question really has nothing to do with Paperclip or MP3 files at all. You just need to know how to change all the underscores into spaces. You can use String#tr for that:
$ irb
>> "Gould_Stokowski_1.mp3".tr('_', ' ')
=> "Gould Stokowski 1.mp3"

Related

Writing an mp4 file in Ruby

I am trying to figure out how to take a string that is the file contents of a mp4 file and write a properly formatted mp4 file. Currently I am just throwing the string into a file and slapping a .mp4 extension on it, but this resulting file cannot be played by any video players (I am assuming because of all the missing meta data).
def write_mp4(mp4_string)
file = File.new('movie.mp4', 'w')
file.puts(mp4_string)
file.close
end
For context, I am doing this in a Ruby on Rails application, not sure if that changes anything. Please help thanks.
Use "wb" mode, which will suppress EOL conversions and set the encoding properly
Use write, not puts, as the latter inserts an extra EOL
You could use a shortcut: File.write('movie.mp4', mp4_string, mode: 'wb') - or even File.binwrite('movie.mp4', mp4_string).
Of course, make sure the string actually contains a correct file before - for example, if mp4_string.encoding doesn't return #<Encoding:ASCII-8BIT>, you probably done goofed somewhere before the writing step, too :)

CSV coming out as pipe-delimited

I created a Rails file outputting values separated by commas, called posts.csv.erb.
When I click on the link, it downloads as a CSV file, but the commas are now pipes. Also, Vim tries to auto-correct the commas into pipes for some reason (but I got around that, so they are actually commas). It works fine if I call the file posts.txt.erb - I see actual commas. But somehow it seems Rails or ERB wants to output pipes for a CSV file?

Paperclip - How to control which characters are escaped/changed to an underscore in an uploaded filename?

In a recent paperclip gem upgrade I noticed that the uploaded filenames are being modified and some characters are being changed to an underscore.
I don't not like the fact that it changes spaces to underscores. What's wrong with a space in a file name? The 90's are long over..
After some digging I found this undocumented option for has_attached_file:
:restricted_characters
The default value of this option seems to be this:
:restricted_characters => /[&$+,\/:;=?#<>\[\]\{\}\|\\\^~%# ]/,
The last one is a space. Remove it and you're good.

Given a Paperclip file, how to clean up the filename to be url friendly?

remove non-utf8 characters, downcase, removes spaces. Is there a builtin way in rails to make filenames friendly and safe before paperclip saves?

Rails 3 - Paperclip - Eliminating Spaces inside a filename

I have a rails 3 app using paperclip. Is there a way with paperclip to automatically rename all files as follows: All spaces converted to _ , for various app related reasons I want to eliminate spaces in file names.
Thanks
You can use before_post_process to alter the filename.
http://www.davesouth.org/stories/make-url-friendly-filenames-in-paperclip-attachments

Resources