2018-05-28 01:41:53 -04:00
|
|
|
# This fixes the problem https://gitlab.com/gitlab-org/gitlab-ce/issues/46182 that carrierwave eagerly loads upoloading files into memory
|
|
|
|
# There is an PR https://github.com/carrierwaveuploader/carrierwave/pull/2314 which has the identical change.
|
2018-05-23 04:09:04 -04:00
|
|
|
module CarrierWave
|
|
|
|
module Storage
|
|
|
|
class Fog < Abstract
|
|
|
|
class File
|
2018-05-25 01:31:11 -04:00
|
|
|
module MonkeyPatch
|
2018-05-27 07:43:57 -04:00
|
|
|
##
|
|
|
|
# Read content of file from service
|
|
|
|
#
|
|
|
|
# === Returns
|
|
|
|
#
|
|
|
|
# [String] contents of file
|
2018-05-25 01:31:11 -04:00
|
|
|
def read
|
|
|
|
file_body = file.body
|
2018-05-23 04:09:04 -04:00
|
|
|
|
2018-05-25 01:31:11 -04:00
|
|
|
return if file_body.nil?
|
|
|
|
return file_body unless file_body.is_a?(::File)
|
2018-05-23 04:09:04 -04:00
|
|
|
|
2018-05-27 07:43:57 -04:00
|
|
|
# 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
|
2018-05-23 04:09:04 -04:00
|
|
|
end
|
|
|
|
|
2018-05-27 07:43:57 -04:00
|
|
|
##
|
|
|
|
# Write file to service
|
|
|
|
#
|
|
|
|
# === Returns
|
|
|
|
#
|
|
|
|
# [Boolean] true on success or raises error
|
2018-05-25 01:31:11 -04:00
|
|
|
def store(new_file)
|
2018-05-25 12:41:51 -04:00
|
|
|
if new_file.is_a?(self.class) # rubocop:disable Cop/LineBreakAroundConditionalBlock
|
2018-05-25 01:31:11 -04:00
|
|
|
new_file.copy_to(path)
|
|
|
|
else
|
|
|
|
fog_file = new_file.to_file
|
|
|
|
@content_type ||= new_file.content_type # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
@file = directory.files.create({ # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2018-05-25 12:41:51 -04:00
|
|
|
:body => fog_file ? fog_file : new_file.read, # rubocop:disable Style/HashSyntax
|
|
|
|
:content_type => @content_type, # rubocop:disable Style/HashSyntax,Gitlab/ModuleWithInstanceVariables
|
|
|
|
:key => path, # rubocop:disable Style/HashSyntax
|
|
|
|
:public => @uploader.fog_public # rubocop:disable Style/HashSyntax,Gitlab/ModuleWithInstanceVariables
|
2018-05-25 01:31:11 -04:00
|
|
|
}.merge(@uploader.fog_attributes)) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
fog_file.close if fog_file && !fog_file.closed?
|
|
|
|
end
|
|
|
|
true
|
2018-05-23 04:09:04 -04:00
|
|
|
end
|
2018-05-27 07:43:57 -04:00
|
|
|
|
|
|
|
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
|
2018-05-23 04:09:04 -04:00
|
|
|
end
|
2018-05-25 01:31:11 -04:00
|
|
|
|
|
|
|
prepend MonkeyPatch
|
2018-05-23 04:09:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|