Module: UploadUtils

Included in:
AV, FilestackClient, FilestackCommon, FilestackFilelink
Defined in:
lib/filestack/utils/utils.rb

Overview

Includes general utility functions for the Filestack Ruby SDK

Instance Method Summary collapse

Instance Method Details

#get_url(base, handle: nil, path: nil, security: nil) ⇒ String

Generates the URL for a FilestackFilelink object

Parameters:

  • base (String)

    The base Filestack URL

  • handle (String)

    The FilestackFilelink handle (optional)

  • path (String)

    The specific API path (optional)

  • security (String)

    Security for the FilestackFilelink (optional)

Returns:

  • (String)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/filestack/utils/utils.rb', line 111

def get_url(base, handle: nil, path: nil, security: nil)
  url_components = [base]

  url_components.push(path) unless path.nil?
  url_components.push(handle) unless handle.nil?
  url = url_components.join('/')

  if security
    policy = security.policy
    signature = security.signature
    security_path = "policy=#{policy}&signature=#{signature}"
    url = "#{url}?#{security_path}"
  end
  url
end

#make_call(url, action, parameters: nil, headers: nil) ⇒ Typhoeus::Request

General request function

Parameters:

  • url (String)

    The URL being called

  • action (String)

    The specific HTTP action ('get', 'post', 'delete', 'put')

  • parameters (Hash)

    The query and/or body parameters

Returns:

  • (Typhoeus::Request)


56
57
58
59
60
61
62
63
64
65
# File 'lib/filestack/utils/utils.rb', line 56

def make_call(url, action, parameters: nil, headers: nil)
  headers = if headers
              headers.merge!(FilestackConfig::HEADERS)
            else
              FilestackConfig::HEADERS
            end
  Typhoeus.public_send(
    action, url, parameters: parameters, headers: headers
  )
end

#send_upload(apikey, filepath: nil, external_url: nil, security: nil, options: nil, storage: 'S3') ⇒ Typhoeus::Response

Uploads to v1 REST API (for external URLs or if multipart is turned off)

Parameters:

  • apikey (String)

    Filestack API key

  • filepath (String)

    Local path to file

  • external_url (String)

    External URL to be uploaded

  • security (FilestackSecurity)

    Security object with policy/signature

  • options (Hash)

    User-defined options for multipart uploads

  • storage (String)

    Storage destination (s3, rackspace, etc)

Returns:

  • (Typhoeus::Response)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/filestack/utils/utils.rb', line 79

def send_upload(apikey, filepath: nil, external_url: nil, security: nil, options: nil, storage: 'S3')
  data = if filepath
           { fileUpload: File.open(filepath) }
         else
           { url: external_url }
         end

  # adds any user-defined upload options to request payload
  data = data.merge!(options) unless options.nil?
  base = "#{FilestackConfig::API_URL}/store/#{storage}?key=#{apikey}"

  if security
    policy = security.policy
    signature = security.signature
    base = "#{base}&signature=#{signature}&policy=#{policy}"
  end

  response = make_call(base, 'post', parameters: data)
  if response.code == 200
    handle = response.body['url'].split('/').last
    return { 'handle' => handle }
  end
  raise response.body
end