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

file_uploader.rb 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. include ::CarrierWave::Backgrounder::Delay
  8. # Choose what kind of storage to use for this uploader:
  9. if Rails.env.test? or Rails.env.cucumber?
  10. storage :file
  11. end
  12. if Rails.env.development?
  13. storage :file
  14. end
  15. if Rails.env.production?
  16. # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility if using fog:
  17. # include Sprockets::Rails::Helper
  18. include Sprockets::Helpers
  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 root
  27. "#{Rails.root}/public"
  28. end
  29. def cache_dir
  30. " ./tmp/uploads/#{mounted_as}/"
  31. end
  32. # Provide a default URL as a default if there hasn't been a file uploaded:
  33. # def default_url
  34. # # For Rails 3.1+ asset pipeline compatibility:
  35. # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  36. #
  37. # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  38. # end
  39. # Process files as they are uploaded:
  40. # process :scale => [200, 300]
  41. #
  42. # def scale(width, height)
  43. # # do something
  44. # end
  45. # Create different versions of your uploaded files:
  46. version :thumb, :if => :image? do
  47. process :resize_to_fill => [300, 200, gravity = 'Center']
  48. end
  49. # Add a white list of extensions which are allowed to be uploaded.
  50. # For images you might use something like this:
  51. # def extension_white_list
  52. # %w(jpg jpeg gif png)
  53. # end
  54. # Override the filename of the uploaded files:
  55. # Avoid using model.id or version_name here, see uploader/store.rb for details.
  56. # def filename
  57. # "something.jpg" if original_filename
  58. # end
  59. protected
  60. def image?(new_file)
  61. new_file.content_type.start_with? 'image'
  62. end
  63. end