diff options
author | Jarka Kadlecova <jarka@gitlab.com> | 2017-05-29 09:54:35 +0200 |
---|---|---|
committer | Jarka Kadlecova <jarka@gitlab.com> | 2017-06-07 07:52:41 +0200 |
commit | 2e311d9d1aac58bbd9c7d6c97c7cbcccf2715347 (patch) | |
tree | 04555ee940d5488ef6d44c5ad3afa0688cd6c1c5 /app/uploaders | |
parent | 4464c22d6d23d893494682d309aec3fb31c11ae3 (diff) | |
download | gitlab-ce-2e311d9d1aac58bbd9c7d6c97c7cbcccf2715347.tar.gz |
Support uploads for newly created personal snippets12910-snippets-description
Diffstat (limited to 'app/uploaders')
-rw-r--r-- | app/uploaders/file_mover.rb | 29 | ||||
-rw-r--r-- | app/uploaders/records_uploads.rb | 7 |
2 files changed, 26 insertions, 10 deletions
diff --git a/app/uploaders/file_mover.rb b/app/uploaders/file_mover.rb index 21e37a08a82..00c2888d224 100644 --- a/app/uploaders/file_mover.rb +++ b/app/uploaders/file_mover.rb @@ -1,33 +1,42 @@ class FileMover - attr_reader :secret, :file_name, :model + attr_reader :secret, :file_name, :model, :update_field def initialize(file_path, model, update_field = :description) @secret = File.split(File.dirname(file_path)).last @file_name = File.basename(file_path) @model = model + @update_field = update_field end def execute move - update_markdown + uploader.record_upload if update_markdown end private def move - FileUtils.mkdir_p(file_path) + FileUtils.mkdir_p(File.dirname(file_path)) FileUtils.move(temp_file_path, file_path) end - def update_markdown(field = :description) - updated_text = model.send(field).sub(temp_file_uploader.to_markdown, uploader.to_markdown) - model.update_attribute(field, updated_text) + def update_markdown + updated_text = model.read_attribute(update_field).gsub(temp_file_uploader.to_markdown, uploader.to_markdown) + model.update_attribute(update_field, updated_text) + + true + rescue + revert + + false end def temp_file_path + return @temp_file_path if @temp_file_path + temp_file_uploader.retrieve_from_store!(file_name) - temp_file_uploader.file.path + @temp_file_path = temp_file_uploader.file.path end def file_path @@ -45,4 +54,10 @@ class FileMover def temp_file_uploader @temp_file_uploader ||= PersonalFileUploader.new(nil, secret) end + + def revert + Rails.logger.warn("Markdown not updated, file move reverted for #{model}") + + FileUtils.move(file_path, temp_file_path) + end end diff --git a/app/uploaders/records_uploads.rb b/app/uploaders/records_uploads.rb index 4c127f29250..feb4f04d7b7 100644 --- a/app/uploaders/records_uploads.rb +++ b/app/uploaders/records_uploads.rb @@ -6,8 +6,6 @@ module RecordsUploads before :remove, :destroy_upload end - private - # After storing an attachment, create a corresponding Upload record # # NOTE: We're ignoring the argument passed to this callback because we want @@ -15,13 +13,16 @@ module RecordsUploads # `Tempfile` object the callback gets. # # Called `after :store` - def record_upload(_tempfile) + def record_upload(_tempfile = nil) + return unless model return unless file_storage? return unless file.exists? Upload.record(self) end + private + # Before removing an attachment, destroy any Upload records at the same path # # Called `before :remove` |