ungarbled encodes multibyte filename correctly for certain platform. Rails integration included.
You can't see filenames in your language correctly encoded? Please refer Extend Languages section.
Rails ActionController integration
Rails >= 4 is supported
Add below to Gemfile.
gem 'ungarbled'Then bundle install, and it's done!
Now, send_data (with :filename option) and send_file (with :filename option or without :url_based_filename option) will send filename encoded for specific browsers. To disable this automatic encode, try:
# config/initializers/ungarbled.rb
Rails.configuration.ungarbled.disable_auto_encode = trueEven with this setting, you can still use encode_filename method within Controllers.
class FilesController < ApplicationController
def download
send_file Rails.root.join('public', 'files', '日本語ファイル名.txt'),
filename: encode_filename('日本語ファイル名.txt')
end
endOptional
If you still can't see correct result, you may need encoding for specific language to be applied to filename, below configuration is required:
# config/initializers/ungarbled.rb
Rails.configuration.ungarbled.default_lang = :jaor, pass lang option to encode_filename.
class FilesController < ApplicationController
def download
send_file Rails.root.join('public', 'files', '日本語ファイル名.txt'),
# this overrides `defalunt_lang` config
filename: encode_filename('日本語ファイル名.txt', lang: :ja)
end
endZip Support
To encode Zip item filename, use encode_filename_for_zip_item with setting above config.
# Example with rubyzip
directory_to_zip = Rails.root.join('public', 'multibyte_name_files')
zipfile_name = Rails.root.join('tmp', 'multibyte_name_files.zip')
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
Dir[File.join(directory_to_zip, '**', '**')].each do |file|
# ungarble!!
zipfile.add(encode_filename_for_zip_item(file.sub(directory_to_zip, '')),
file
)
end
endRack Middleware
Rack Middleware parses response and encodes filename automatically.
This does not encode zip items' filenames
# `config.ru`
use Ungarbled::Middleware
# for specific language:
use Ungarbled::Middleware, lang: :jaRails
Rails ActionController integration is recommended.
# config/initializers/ungarbled.rb
Rails.configuration.middleware.use Ungarbled::Middleware
# for specific language:
Rails.configuration.middleware.use Ungarbled::Middleware, lang: :jaExtend Languages
ungarbled authors are Japanese native, so not sure about other languages. But if you want to fix garbled download filenames in your language, please help us extending supporting language. Pull Requests are welcome!
Please see lib/ungarbled/encoder/ja.rb for reference, and just add your encoder file in the same directory. You can use Browser instance with @browser for browser/platform detection. Test is also required to be added to test/encoder/yourlanguage_test.rb
Future Plan
- Middleware for Zip item
- Automatic Language Detection
Contributing
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Added some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
