A website template with lots of features, built with ruby on rails.

file_uploader.rb 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # encoding: utf-8
  2. class FileUploader < CarrierWave::Uploader::Base
  3. # Include RMagick or MiniMagick support:
  4. # include CarrierWave::RMagick
  5. include CarrierWave::MiniMagick
  6. include CarrierWave::MimeTypes
  7. # Choose what kind of storage to use for this uploader:
  8. if Rails.env.test? or Rails.env.cucumber?
  9. storage :file
  10. end
  11. if Rails.env.development?
  12. storage :file
  13. end
  14. if Rails.env.production?
  15. # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility if using fog:
  16. # include Sprockets::Rails::Helper
  17. include Sprockets::Helpers::RailsHelper
  18. include Sprockets::Helpers::IsolatedHelper
  19. storage :fog
  20. end
  21. # Override the directory where uploaded files will be stored.
  22. # This is a sensible default for uploaders that are meant to be mounted:
  23. def store_dir
  24. "uploads/#{mounted_as}/#{model.id}"
  25. end
  26. def cache_dir
  27. " ./tmp/uploads/#{mounted_as}/#{model.id}"
  28. end
  29. # Provide a default URL as a default if there hasn't been a file uploaded:
  30. # def default_url
  31. # # For Rails 3.1+ asset pipeline compatibility:
  32. # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  33. #
  34. # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  35. # end
  36. # Process files as they are uploaded:
  37. # process :scale => [200, 300]
  38. #
  39. # def scale(width, height)
  40. # # do something
  41. # end
  42. # Create different versions of your uploaded files:
  43. version :thumb, :if => :image? do
  44. process :resize_to_fill => [300, 200, gravity = 'Center']
  45. end
  46. # Add a white list of extensions which are allowed to be uploaded.
  47. # For images you might use something like this:
  48. # def extension_white_list
  49. # %w(jpg jpeg gif png)
  50. # end
  51. # Override the filename of the uploaded files:
  52. # Avoid using model.id or version_name here, see uploader/store.rb for details.
  53. # def filename
  54. # "something.jpg" if original_filename
  55. # end
  56. protected
  57. def image?(new_file)
  58. new_file.content_type.start_with? 'image'
  59. end
  60. end