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

file_uploader.rb 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  18. storage :fog
  19. end
  20. # Override the directory where uploaded files will be stored.
  21. # This is a sensible default for uploaders that are meant to be mounted:
  22. def store_dir
  23. "uploads/#{mounted_as}/#{model.id}"
  24. end
  25. def cache_dir
  26. " ./tmp/uploads/#{mounted_as}/#{model.id}"
  27. end
  28. # Provide a default URL as a default if there hasn't been a file uploaded:
  29. # def default_url
  30. # # For Rails 3.1+ asset pipeline compatibility:
  31. # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  32. #
  33. # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  34. # end
  35. # Process files as they are uploaded:
  36. # process :scale => [200, 300]
  37. #
  38. # def scale(width, height)
  39. # # do something
  40. # end
  41. # Create different versions of your uploaded files:
  42. version :thumb, :if => :image? do
  43. process :resize_to_fill => [300, 200, gravity = 'Center']
  44. end
  45. # Add a white list of extensions which are allowed to be uploaded.
  46. # For images you might use something like this:
  47. # def extension_white_list
  48. # %w(jpg jpeg gif png)
  49. # end
  50. # Override the filename of the uploaded files:
  51. # Avoid using model.id or version_name here, see uploader/store.rb for details.
  52. # def filename
  53. # "something.jpg" if original_filename
  54. # end
  55. protected
  56. def image?(new_file)
  57. new_file.content_type.start_with? 'image'
  58. end
  59. end