summaryrefslogtreecommitdiff
path: root/app/uploaders/file_uploader.rb
blob: e82115858342f4af270fa2d474753a52ed2f2829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# encoding: utf-8
class FileUploader < CarrierWave::Uploader::Base
  storage :file

  attr_accessor :project, :secret

  def initialize(project, secret = self.class.generate_secret)
    @project = project
    @secret = secret
  end

  def base_dir
    "uploads"
  end

  def store_dir
    File.join(base_dir, @project.path_with_namespace, @secret)
  end

  def cache_dir
    File.join(base_dir, 'tmp', @project.path_with_namespace, @secret)
  end

  def self.generate_secret
    SecureRandom.hex
  end

  def secure_url
    File.join("/uploads", @secret, file.filename)
  end

  def file_storage?
    self.class.storage == CarrierWave::Storage::File
  end

  def image?
    img_ext = %w(png jpg jpeg gif bmp tiff)
    if file.respond_to?(:extension)
      img_ext.include?(file.extension.downcase)
    else
      # Not all CarrierWave storages respond to :extension
      ext = file.path.split('.').last.downcase
      img_ext.include?(ext)
    end
  rescue
    false
  end
end