Commit 92b2647d authored by Shinya Maeda's avatar Shinya Maeda

Clean up patch

parent e35bc5f4
...@@ -6,23 +6,32 @@ module CarrierWave ...@@ -6,23 +6,32 @@ module CarrierWave
class Fog < Abstract class Fog < Abstract
class File class File
module MonkeyPatch module MonkeyPatch
##
# Read content of file from service
#
# === Returns
#
# [String] contents of file
def read def read
file_body = file.body file_body = file.body
return if file_body.nil? return if file_body.nil?
return file_body unless file_body.is_a?(::File) return file_body unless file_body.is_a?(::File)
begin # Fog::Storage::XXX::File#body could return the source file which was upoloaded to the remote server.
file_body = ::File.open(file_body.path) if file_body.closed? # Reopen if it's closed read_source_file(file_body) if ::File.exist?(file_body.path)
file_body.read
rescue Errno::ENOENT # If the source file doesn't exist, the remote content is read
@file = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables @file = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
file.body file.body
ensure
file_body.close
end
end end
##
# Write file to service
#
# === Returns
#
# [Boolean] true on success or raises error
def store(new_file) def store(new_file)
if new_file.is_a?(self.class) # rubocop:disable Cop/LineBreakAroundConditionalBlock if new_file.is_a?(self.class) # rubocop:disable Cop/LineBreakAroundConditionalBlock
new_file.copy_to(path) new_file.copy_to(path)
...@@ -39,6 +48,19 @@ module CarrierWave ...@@ -39,6 +48,19 @@ module CarrierWave
end end
true true
end 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 end
prepend MonkeyPatch prepend MonkeyPatch
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment