Class: FileUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
UploaderHelper
Defined in:
app/uploaders/file_uploader.rb

Constant Summary collapse

MARKDOWN_PATTERN =
%r{\!?\[.*?\]\(/uploads/(?<secret>[0-9a-f]{32})/(?<file>.*?)\)}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UploaderHelper

#file_storage?, #image?

Constructor Details

#initialize(project, secret = nil) ⇒ FileUploader

Returns a new instance of FileUploader


10
11
12
13
# File 'app/uploaders/file_uploader.rb', line 10

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

Instance Attribute Details

#projectObject

Returns the value of attribute project


8
9
10
# File 'app/uploaders/file_uploader.rb', line 8

def project
  @project
end

#secretObject

Returns the value of attribute secret


8
9
10
# File 'app/uploaders/file_uploader.rb', line 8

def secret
  @secret
end

Class Method Details

.generate_secretObject


50
51
52
# File 'app/uploaders/file_uploader.rb', line 50

def self.generate_secret
  SecureRandom.hex
end

Instance Method Details

#base_dirObject


15
16
17
# File 'app/uploaders/file_uploader.rb', line 15

def base_dir
  "uploads"
end

#cache_dirObject


23
24
25
# File 'app/uploaders/file_uploader.rb', line 23

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

#secure_urlObject


27
28
29
# File 'app/uploaders/file_uploader.rb', line 27

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

#store_dirObject


19
20
21
# File 'app/uploaders/file_uploader.rb', line 19

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

#to_hObject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/uploaders/file_uploader.rb', line 35

def to_h
  filename = image? ? self.file.basename : self.file.filename
  escaped_filename = filename.gsub("]", "\\]")

  markdown = "[#{escaped_filename}](#{self.secure_url})"
  markdown.prepend("!") if image?

  {
    alt:      filename,
    url:      self.secure_url,
    is_image: image?,
    markdown: markdown
  }
end

#to_markdownObject


31
32
33
# File 'app/uploaders/file_uploader.rb', line 31

def to_markdown
  to_h[:markdown]
end