Class: Uploadcare::Entity::Uploader

Inherits:
Entity
  • Object
show all
Defined in:
lib/uploadcare/entity/uploader.rb

Overview

This serializer lets user upload files by various means, and usually returns an array of files

Class Method Summary collapse

Class Method Details

.file_info(uuid) ⇒ Object

Get information about an uploaded file (without the secret key)

Parameters:

  • uuid (String)


74
75
76
# File 'lib/uploadcare/entity/uploader.rb', line 74

def self.file_info(uuid)
  UploaderClient.new.file_info(uuid)
end

.get_upload_from_url_status(token) ⇒ Object

gets a status of upload from url

Parameters:

  • url (String)


68
69
70
# File 'lib/uploadcare/entity/uploader.rb', line 68

def self.get_upload_from_url_status(token)
  UploaderClient.new.get_upload_from_url_status(token)
end

.multipart_upload(file, options = {}, &block) ⇒ Object

upload file of size above 10mb (involves multipart upload)



52
53
54
55
# File 'lib/uploadcare/entity/uploader.rb', line 52

def self.multipart_upload(file, options = {}, &block)
  response = MultipartUploaderClient.new.upload(file, options, &block)
  Uploadcare::Entity::File.new(response.success)
end

.upload(object, options = {}) ⇒ Object

Upload file or group of files from array, File, or url

Parameters:

  • object (Array)

    , [String] or [File]

  • options (Hash) (defaults to: {})

    options for upload

Options Hash (options):

  • :store (Boolean)

    whether to store file on servers.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/uploadcare/entity/uploader.rb', line 19

def self.upload(object, options = {})
  if big_file?(object)
    multipart_upload(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_file(file, options = {}) ⇒ Object

upload single file



34
35
36
37
38
39
40
41
42
43
# File 'lib/uploadcare/entity/uploader.rb', line 34

def self.upload_file(file, options = {})
  response = UploaderClient.new.upload_many([file], options)
  uuid = response.success.values.first
  if Uploadcare.config.secret_key.nil?
    Uploadcare::Entity::File.new(file_info(uuid).success)
  else
    # we can get more info about the uploaded file
    Uploadcare::Entity::File.info(uuid)
  end
end

.upload_files(arr, options = {}) ⇒ Object

upload multiple files



46
47
48
49
# File 'lib/uploadcare/entity/uploader.rb', line 46

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 = {}) ⇒ Object

upload files from url

Parameters:

  • url (String)


59
60
61
62
63
64
# File 'lib/uploadcare/entity/uploader.rb', line 59

def self.upload_from_url(url, options = {})
  response = UploaderClient.new.upload_from_url(url, options)
  return response.success[:token] unless response.success[:files]

  response.success[:files].map { |file_data| Uploadcare::Entity::File.new(file_data) }
end