2020-08-05 05:10:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "carrierwave/storage/fog"
|
|
|
|
|
|
|
|
# This pulls in https://github.com/carrierwaveuploader/carrierwave/pull/2504 to support
|
|
|
|
# sending AWS S3 encryption headers when copying objects.
|
2020-08-20 02:10:17 -04:00
|
|
|
#
|
|
|
|
# This patch also incorporates
|
|
|
|
# https://github.com/carrierwaveuploader/carrierwave/pull/2375 to
|
2020-09-04 11:08:46 -04:00
|
|
|
# provide Azure support
|
|
|
|
# and https://github.com/carrierwaveuploader/carrierwave/pull/2397 to
|
|
|
|
# support custom expire_at. This is already in CarrierWave v2.1.x, but
|
2020-08-20 02:10:17 -04:00
|
|
|
# upgrading this gem is a significant task:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/216067
|
2020-08-05 05:10:01 -04:00
|
|
|
module CarrierWave
|
|
|
|
module Storage
|
|
|
|
class Fog < Abstract
|
|
|
|
class File
|
|
|
|
def copy_to(new_path)
|
2021-01-07 13:10:38 -05:00
|
|
|
# fog-aws needs multipart uploads to copy files above 5 GB,
|
|
|
|
# and it is currently the only Fog provider that supports
|
|
|
|
# multithreaded uploads (https://github.com/fog/fog-aws/pull/579).
|
|
|
|
# Multithreaded uploads are essential for copying large amounts of data
|
|
|
|
# within the request timeout.
|
2022-05-06 11:09:03 -04:00
|
|
|
if ::Feature.enabled?(:s3_multithreaded_uploads) && fog_provider == 'AWS'
|
2021-01-14 01:11:16 -05:00
|
|
|
# AWS SDK uses 10 threads by default and a multipart chunk size of 10 MB
|
|
|
|
file.concurrency = 10
|
|
|
|
file.multipart_chunk_size = 10485760
|
2021-01-07 13:10:38 -05:00
|
|
|
file.copy(@uploader.fog_directory, new_path, copy_to_options)
|
|
|
|
else
|
|
|
|
# Some Fog providers may issue a GET request (https://github.com/fog/fog-google/issues/512)
|
|
|
|
# instead of a HEAD request after the transfer completes,
|
|
|
|
# which might cause the file to be downloaded locally.
|
|
|
|
# We fallback to the original copy_object for non-AWS providers.
|
|
|
|
connection.copy_object(@uploader.fog_directory, file.key, @uploader.fog_directory, new_path, copy_to_options)
|
|
|
|
end
|
|
|
|
|
2020-08-05 05:10:01 -04:00
|
|
|
CarrierWave::Storage::Fog::File.new(@uploader, @base, new_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_to_options
|
|
|
|
acl_header.merge(@uploader.fog_attributes)
|
|
|
|
end
|
2020-08-20 02:10:17 -04:00
|
|
|
|
|
|
|
def authenticated_url(options = {})
|
|
|
|
if %w[AWS Google Rackspace OpenStack AzureRM].include?(@uploader.fog_credentials[:provider])
|
|
|
|
# avoid a get by using local references
|
|
|
|
local_directory = connection.directories.new(key: @uploader.fog_directory)
|
|
|
|
local_file = local_directory.files.new(key: path)
|
2020-09-04 11:08:46 -04:00
|
|
|
expire_at = options[:expire_at] || ::Fog::Time.now + @uploader.fog_authenticated_url_expiration
|
2020-08-20 02:10:17 -04:00
|
|
|
case @uploader.fog_credentials[:provider]
|
2021-09-23 08:11:29 -04:00
|
|
|
when 'AWS', 'Google', 'AzureRM'
|
|
|
|
local_file.url(expire_at, options)
|
2020-08-20 02:10:17 -04:00
|
|
|
when 'Rackspace'
|
|
|
|
connection.get_object_https_url(@uploader.fog_directory, path, expire_at, options)
|
|
|
|
when 'OpenStack'
|
|
|
|
connection.get_object_https_url(@uploader.fog_directory, path, expire_at)
|
|
|
|
else
|
|
|
|
local_file.url(expire_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-05 05:10:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|