summaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2018-05-27 20:43:57 +0900
committerShinya Maeda <shinya@gitlab.com>2018-05-28 14:34:37 +0900
commit92b2647d003cb1126cb4786956a2887189da803c (patch)
tree4cfe623440876ca8352348a0a4ea3f08473c6de0 /config
parente35bc5f4bc36ceaeb8bc1e48e5edf8724849e028 (diff)
downloadgitlab-ce-92b2647d003cb1126cb4786956a2887189da803c.tar.gz
Clean up patch
Diffstat (limited to 'config')
-rw-r--r--config/initializers/carrierwave_monkey_patch.rb40
1 files changed, 31 insertions, 9 deletions
diff --git a/config/initializers/carrierwave_monkey_patch.rb b/config/initializers/carrierwave_monkey_patch.rb
index d6925d5f8dc..5b929bfb961 100644
--- a/config/initializers/carrierwave_monkey_patch.rb
+++ b/config/initializers/carrierwave_monkey_patch.rb
@@ -6,23 +6,32 @@ module CarrierWave
class Fog < Abstract
class File
module MonkeyPatch
+ ##
+ # Read content of file from service
+ #
+ # === Returns
+ #
+ # [String] contents of file
def read
file_body = file.body
return if file_body.nil?
return file_body unless file_body.is_a?(::File)
- begin
- file_body = ::File.open(file_body.path) if file_body.closed? # Reopen if it's closed
- file_body.read
- rescue Errno::ENOENT
- @file = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
- file.body
- ensure
- file_body.close
- end
+ # Fog::Storage::XXX::File#body could return the source file which was upoloaded to the remote server.
+ read_source_file(file_body) if ::File.exist?(file_body.path)
+
+ # If the source file doesn't exist, the remote content is read
+ @file = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
+ file.body
end
+ ##
+ # Write file to service
+ #
+ # === Returns
+ #
+ # [Boolean] true on success or raises error
def store(new_file)
if new_file.is_a?(self.class) # rubocop:disable Cop/LineBreakAroundConditionalBlock
new_file.copy_to(path)
@@ -39,6 +48,19 @@ module CarrierWave
end
true
end
+
+ private
+
+ def read_source_file(file_body)
+ return unless ::File.exist?(file_body.path)
+
+ begin
+ file_body = ::File.open(file_body.path) if file_body.closed? # Reopen if it's already closed
+ file_body.read
+ ensure
+ file_body.close
+ end
+ end
end
prepend MonkeyPatch