class Uploadcare::Entity::Uploader
This serializer lets user upload files by various means, and usually returns an array of files @see uploadcare.com/api-refs/upload-api/#tag/Upload
Public Class Methods
upload(object, **options)
click to toggle source
Upload file or group of files from array, File
, or url
@param object [Array], [String] or [File] @param [Hash] options options for upload @option options [Boolean] :store (false) whether to store file on servers.
# File lib/uploadcare/entity/uploader.rb, line 19 def self.upload(object, **options) if big_file?(object) upload_big_file(object, **options) elsif file?(object) upload_file(object, **options) elsif object.is_a?(Array) upload_files(object, **options) elsif object.is_a?(String) upload_from_url(object, **options) else raise ArgumentError, "Expected input to be a file/Array/URL, given: `#{object}`" end end
upload_big_file(file, **_options)
click to toggle source
upload file of size above 10mb (involves multipart upload)
# File lib/uploadcare/entity/uploader.rb, line 46 def self.upload_big_file(file, **_options) response = MultipartUploaderClient.new.upload(file) Uploadcare::Entity::File.new(response.success) end
upload_file(file, **options)
click to toggle source
upload single file
# File lib/uploadcare/entity/uploader.rb, line 34 def self.upload_file(file, **options) response = UploaderClient.new.upload_many([file], **options) Uploadcare::Entity::File.info(response.success.to_a.flatten[-1]) end
upload_files(arr, **options)
click to toggle source
upload multiple files
# File lib/uploadcare/entity/uploader.rb, line 40 def self.upload_files(arr, **options) response = UploaderClient.new.upload_many(arr, **options) response.success.map { |pair| Uploadcare::Entity::File.new(uuid: pair[1], original_filename: pair[0]) } end
upload_from_url(url, **options)
click to toggle source
upload files from url @param url [String]
# File lib/uploadcare/entity/uploader.rb, line 53 def self.upload_from_url(url, **options) response = UploaderClient.new.upload_from_url(url, **options) response.success[:files].map { |file_data| Uploadcare::Entity::File.new(file_data) } end
Private Class Methods
big_file?(object)
click to toggle source
check if object needs to be uploaded using multipart upload
# File lib/uploadcare/entity/uploader.rb, line 67 def big_file?(object) file?(object) && object.size >= Uploadcare.config.multipart_size_threshold end
file?(object)
click to toggle source
check if object is a file
# File lib/uploadcare/entity/uploader.rb, line 62 def file?(object) object.respond_to?(:path) && ::File.exist?(object.path) end