summaryrefslogtreecommitdiff
path: root/app/uploaders/object_storage.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/uploaders/object_storage.rb')
-rw-r--r--app/uploaders/object_storage.rb46
1 files changed, 42 insertions, 4 deletions
diff --git a/app/uploaders/object_storage.rb b/app/uploaders/object_storage.rb
index ac1f022c63f..d4c74ce277f 100644
--- a/app/uploaders/object_storage.rb
+++ b/app/uploaders/object_storage.rb
@@ -30,6 +30,8 @@ module ObjectStorage
REMOTE = 2
end
+ SUPPORTED_STORES = [Store::LOCAL, Store::REMOTE].freeze
+
module Extension
# this extension is the glue between the ObjectStorage::Concern and RecordsUploads::Concern
module RecordsUploads
@@ -178,10 +180,14 @@ module ObjectStorage
end
def workhorse_authorize(has_length:, maximum_size: nil)
- if self.object_store_enabled? && self.direct_upload_enabled?
- { RemoteObject: workhorse_remote_upload_options(has_length: has_length, maximum_size: maximum_size) }
- else
- { TempPath: workhorse_local_upload_path }
+ {}.tap do |hash|
+ if self.object_store_enabled? && self.direct_upload_enabled?
+ hash[:RemoteObject] = workhorse_remote_upload_options(has_length: has_length, maximum_size: maximum_size)
+ else
+ hash[:TempPath] = workhorse_local_upload_path
+ end
+
+ hash[:MaximumSize] = maximum_size if maximum_size.present?
end
end
@@ -206,6 +212,20 @@ module ObjectStorage
end
end
+ class OpenFile
+ extend Forwardable
+
+ # Explicitly exclude :path, because rubyzip uses that to detect "real" files.
+ def_delegators :@file, *(Zip::File::IO_METHODS - [:path])
+
+ # Even though :size is not in IO_METHODS, we do need it.
+ def_delegators :@file, :size
+
+ def initialize(file)
+ @file = file
+ end
+ end
+
# allow to configure and overwrite the filename
def filename
@filename || super || file&.filename # rubocop:disable Gitlab/ModuleWithInstanceVariables
@@ -255,6 +275,24 @@ module ObjectStorage
end
end
+ def use_open_file(&blk)
+ Tempfile.open(path) do |file|
+ file.unlink
+ file.binmode
+
+ if file_storage?
+ IO.copy_stream(path, file)
+ else
+ streamer = lambda { |chunk, _, _| file.write(chunk) }
+ Excon.get(url, response_block: streamer)
+ end
+
+ file.seek(0, IO::SEEK_SET)
+
+ yield OpenFile.new(file)
+ end
+ end
+
#
# Move the file to another store
#