BigW Consortium Gitlab

uploader_helper.rb 1.23 KB
Newer Older
1 2
# Extra methods for uploader
module UploaderHelper
3
  IMAGE_EXT = %w[png jpg jpeg gif bmp tiff].freeze
4 5
  # We recommend using the .mp4 format over .mov. Videos in .mov format can
  # still be used but you really need to make sure they are served with the
6 7
  # proper MIME type video/mp4 and not video/quicktime or your videos won't play
  # on IE >= 9.
8
  # http://archive.sublimevideo.info/20150912/docs.sublimevideo.net/troubleshooting.html
9
  VIDEO_EXT = %w[mp4 m4v mov webm ogv].freeze
10 11
  # These extension types can contain dangerous code and should only be embedded inline with
  # proper filtering. They should always be tagged as "Content-Disposition: attachment", not "inline".
12
  DANGEROUS_EXT = %w[svg].freeze
13

14
  def image?
15 16 17 18 19 20 21 22 23 24 25
    extension_match?(IMAGE_EXT)
  end

  def video?
    extension_match?(VIDEO_EXT)
  end

  def image_or_video?
    image? || video?
  end

26 27 28 29
  def dangerous?
    extension_match?(DANGEROUS_EXT)
  end

30 31
  private

32
  def extension_match?(extensions)
33 34
    return false unless file

35 36 37 38 39 40 41 42 43
    extension =
      if file.respond_to?(:extension)
        file.extension
      else
        # Not all CarrierWave storages respond to :extension
        File.extname(file.path).delete('.')
      end

    extensions.include?(extension.downcase)
44 45
  end
end