summaryrefslogtreecommitdiff
path: root/app/uploaders/file_uploader.rb
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2017-02-23 16:54:25 -0500
committerRobert Speicher <rspeicher@gmail.com>2017-02-24 16:41:27 -0500
commita8c62dfe5c01ed08f170c1d41a39d5167b09631b (patch)
tree9c25e207356ebc7b2b88f3c0a7cd0e9374f571c8 /app/uploaders/file_uploader.rb
parent1f8d6c790a8493a341f1786710aac36621e93bf6 (diff)
downloadgitlab-ce-a8c62dfe5c01ed08f170c1d41a39d5167b09631b.tar.gz
Minor refactoring of Uploaders
- Moves a duplicate `file_storage?` definition into the common `GitlabUploader` ancestor. - Get the `uploads` base directory from a class method rather than hard-coding it where it's needed. This will be used in a subsequent MR to store Uploads in the database. - Improves the specs for uploaders.
Diffstat (limited to 'app/uploaders/file_uploader.rb')
-rw-r--r--app/uploaders/file_uploader.rb25
1 files changed, 12 insertions, 13 deletions
diff --git a/app/uploaders/file_uploader.rb b/app/uploaders/file_uploader.rb
index 23b7318827c..0d2edaeff3b 100644
--- a/app/uploaders/file_uploader.rb
+++ b/app/uploaders/file_uploader.rb
@@ -4,15 +4,12 @@ class FileUploader < GitlabUploader
storage :file
- attr_accessor :project, :secret
+ attr_accessor :project
+ attr_reader :secret
def initialize(project, secret = nil)
@project = project
- @secret = secret || self.class.generate_secret
- end
-
- def base_dir
- "uploads"
+ @secret = secret || generate_secret
end
def store_dir
@@ -23,10 +20,6 @@ class FileUploader < GitlabUploader
File.join(base_dir, 'tmp', @project.path_with_namespace, @secret)
end
- def secure_url
- File.join("/uploads", @secret, file.filename)
- end
-
def to_markdown
to_h[:markdown]
end
@@ -35,17 +28,23 @@ class FileUploader < GitlabUploader
filename = image_or_video? ? self.file.basename : self.file.filename
escaped_filename = filename.gsub("]", "\\]")
- markdown = "[#{escaped_filename}](#{self.secure_url})"
+ markdown = "[#{escaped_filename}](#{secure_url})"
markdown.prepend("!") if image_or_video? || dangerous?
{
alt: filename,
- url: self.secure_url,
+ url: secure_url,
markdown: markdown
}
end
- def self.generate_secret
+ private
+
+ def generate_secret
SecureRandom.hex
end
+
+ def secure_url
+ File.join('/uploads', @secret, file.filename)
+ end
end