diff options
author | Micaël Bergeron <mbergeron@gitlab.com> | 2018-01-30 00:06:17 +0300 |
---|---|---|
committer | Micaël Bergeron <mbergeron@gitlab.com> | 2018-02-02 17:28:15 +0300 |
commit | 74ddc80590053b04b90c35ae3e1f46bfbd9d0d15 (patch) | |
tree | 145eaf19ac0a06a7bb8e5a0d85b97606ccaf77eb /app/uploaders | |
parent | 54a575f1bbba44573ab92dc58a4242f1ee734c5d (diff) |
add the uploader context to the upload model
Diffstat (limited to 'app/uploaders')
-rw-r--r-- | app/uploaders/file_mover.rb | 4 | ||||
-rw-r--r-- | app/uploaders/file_uploader.rb | 28 | ||||
-rw-r--r-- | app/uploaders/gitlab_uploader.rb | 4 | ||||
-rw-r--r-- | app/uploaders/records_uploads.rb | 13 |
4 files changed, 34 insertions, 15 deletions
diff --git a/app/uploaders/file_mover.rb b/app/uploaders/file_mover.rb index e7af1483d23..8f56f09c9f7 100644 --- a/app/uploaders/file_mover.rb +++ b/app/uploaders/file_mover.rb @@ -49,11 +49,11 @@ class FileMover end def uploader - @uploader ||= PersonalFileUploader.new(model, secret) + @uploader ||= PersonalFileUploader.new(model, secret: secret) end def temp_file_uploader - @temp_file_uploader ||= PersonalFileUploader.new(nil, secret) + @temp_file_uploader ||= PersonalFileUploader.new(nil, secret: secret) end def revert diff --git a/app/uploaders/file_uploader.rb b/app/uploaders/file_uploader.rb index 85ae9863b13..cc4e7a38165 100644 --- a/app/uploaders/file_uploader.rb +++ b/app/uploaders/file_uploader.rb @@ -62,9 +62,11 @@ class FileUploader < GitlabUploader attr_accessor :model - def initialize(model, secret = nil) + def initialize(model, mounted_as = nil, **uploader_context) + super(model, nil, **uploader_context) + @model = model - @secret = secret + apply_context!(uploader_context) end def base_dir @@ -107,12 +109,12 @@ class FileUploader < GitlabUploader self.file.filename end - # the upload does not hold the secret, but holds the path - # which contains the secret: extract it def upload=(value) - if matches = DYNAMIC_PATH_PATTERN.match(value.path) - @secret = matches[:secret] - @identifier = matches[:identifier] + unless apply_context!(value.uploader_context) + if matches = DYNAMIC_PATH_PATTERN.match(value.path) + @secret = matches[:secret] + @identifier = matches[:identifier] + end end super @@ -124,6 +126,18 @@ class FileUploader < GitlabUploader private + def apply_context!(uploader_context) + @secret, @identifier = uploader_context.values_at(:secret, :identifier) + + !!(@secret && @identifier) + end + + def build_upload + super.tap do |upload| + upload.secret = secret + end + end + def markdown_name (image_or_video? ? File.basename(filename, File.extname(filename)) : filename).gsub("]", "\\]") end diff --git a/app/uploaders/gitlab_uploader.rb b/app/uploaders/gitlab_uploader.rb index b12829efe73..a9e5c028b03 100644 --- a/app/uploaders/gitlab_uploader.rb +++ b/app/uploaders/gitlab_uploader.rb @@ -29,6 +29,10 @@ class GitlabUploader < CarrierWave::Uploader::Base delegate :base_dir, :file_storage?, to: :class + def initialize(model, mounted_as = nil, **uploader_context) + super(model, mounted_as) + end + def file_cache_storage? cache_storage.is_a?(CarrierWave::Storage::File) end diff --git a/app/uploaders/records_uploads.rb b/app/uploaders/records_uploads.rb index dfb8dccec57..458928bc067 100644 --- a/app/uploaders/records_uploads.rb +++ b/app/uploaders/records_uploads.rb @@ -24,7 +24,7 @@ module RecordsUploads uploads.where(path: upload_path).delete_all upload.destroy! if upload - self.upload = build_upload_from_uploader(self) + self.upload = build_upload upload.save! end end @@ -39,12 +39,13 @@ module RecordsUploads Upload.order(id: :desc).where(uploader: self.class.to_s) end - def build_upload_from_uploader(uploader) + def build_upload Upload.new( - size: uploader.file.size, - path: uploader.upload_path, - model: uploader.model, - uploader: uploader.class.to_s + uploader: self.class.to_s, + size: file.size, + path: upload_path, + model: model, + mount_point: mounted_as ) end |