Skip to content

Commit

Permalink
Merge fbc3df5 into d5d97ba
Browse files Browse the repository at this point in the history
  • Loading branch information
gabifija committed Aug 27, 2018
2 parents d5d97ba + fbc3df5 commit c8a3d9e
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/filestack.rb
Expand Up @@ -3,3 +3,4 @@
require 'filestack/models/filestack_security'
require 'filestack/utils/utils'
require 'filestack/models/filestack_transform'
require 'filestack/core_ext/hash/keys'
20 changes: 20 additions & 0 deletions lib/filestack/core_ext/hash/keys.rb
@@ -0,0 +1,20 @@
module Filestack
# A helper class for the purpose of implementing a symbolize_keys method
# similar to ActiveSupport's symbolize_keys.
class Hash

# Convert a hash to use symbolized keys.
#
# @param [Hash] to_symbolize The hash which contains the keys to be
# symbolized
#
# @return [Hash]
def self.symbolize_keys(to_symbolize)
symbolized = {}
to_symbolize.each_key do |key|
symbolized[key.to_sym] = to_symbolize[key]
end
symbolized
end
end
end
6 changes: 4 additions & 2 deletions lib/filestack/mixins/filestack_common.rb
Expand Up @@ -2,6 +2,7 @@
require 'filestack/utils/utils'
require 'filestack/utils/multipart_upload_utils'
require 'mimemagic'
require 'json'

# Module is mixin for common functionalities that all Filestack
# objects can call.
Expand Down Expand Up @@ -88,7 +89,8 @@ def send_tags(task, handle, security)
signature = security.signature
url = "#{FilestackConfig::CDN_URL}/#{task}/"\
"security=signature:#{signature},policy:#{policy}/#{handle}"
UploadUtils.make_call(url, 'get').body[task]
response = UploadUtils.make_call(url, 'get')
JSON.parse(response.body)[task]
end

def send_metadata(handle, security = nil, params)
Expand All @@ -102,7 +104,7 @@ def send_metadata(handle, security = nil, params)
response = UploadUtils.make_call(url, 'get', parameters: params)

if response.code == 200
return response.body
return JSON.parse(response.body)
end
raise response.body
end
Expand Down
4 changes: 2 additions & 2 deletions lib/filestack/models/filelink.rb
Expand Up @@ -73,15 +73,15 @@ def tags

# Return metadata for file handle
#
# @return [Typhoeus::Response]
# @return [Hash]
def metadata(params = {})
send_metadata(@handle, @security, params)
end

# Return true (SFW) or false (NSFW)
#
# @return [Bool]
def sfw
def sfw
send_tags('sfw', @handle, @security)
end

Expand Down
10 changes: 7 additions & 3 deletions lib/filestack/models/filestack_av.rb
@@ -1,5 +1,6 @@
require 'filestack/models/filelink'
require 'filestack/utils/utils'
require 'json'

# Class for AV objects -- allows to check status
# and upgrade to filelink once completed
Expand All @@ -18,15 +19,18 @@ def initialize(url, apikey: nil, security: nil)
# @return [Filestack::FilestackFilelink]
def to_filelink
return 'Video conversion incomplete' unless status == 'completed'
response = UploadUtils.make_call(@url, 'get').body
handle = response['data']['url'].split('/').last
response = UploadUtils.make_call(@url, 'get')
response_body = JSON.parse(response.body)
handle = response_body['data']['url'].split('/').last
FilestackFilelink.new(handle, apikey: @apikey, security: @security)
end

# Checks the status of the video conversion
#
# @return [String]
def status
UploadUtils.make_call(@url, 'get').body['status']
response = UploadUtils.make_call(@url, 'get')
response_body = JSON.parse(response.body)
response_body['status']
end
end
3 changes: 2 additions & 1 deletion lib/filestack/models/filestack_security.rb
Expand Up @@ -43,7 +43,7 @@ def initialize(secret, options: {})
# @param [String] secret Your filestack security secret
# @param [Hash] options Hash of options - see constructor
def generate(secret, options)
policy_json = create_policy_string(options)
policy_json = create_policy_string(Filestack::Hash.symbolize_keys(options))
@policy = Base64.urlsafe_encode64(policy_json)
@signature = OpenSSL::HMAC.hexdigest('sha256', secret, policy)
end
Expand All @@ -62,6 +62,7 @@ def sign_url(url)
#
# Manage options and convert hash to json string
#

def create_policy_string(options)
options[:expiry] = expiry_timestamp(options)
options.to_json
Expand Down
9 changes: 6 additions & 3 deletions lib/filestack/models/filestack_transform.rb
@@ -1,5 +1,6 @@
require 'filestack/config'
require 'filestack/models/filestack_av'
require 'json'

# Class for creating transformation chains and storing them to Filestack
class Transform
Expand Down Expand Up @@ -64,12 +65,13 @@ def av_convert(options)

# Add debug parameter to get information on transformation image
#
# @return [Typhoeus::Response]
# @return [Hash]
def debug
@transform_tasks.push(
add_transform_task('debug')
)
UploadUtils.make_call(url, 'get').body
response = UploadUtils.make_call(url, 'get')
JSON.parse(response.body)
end

# Stores a transformation URL and returns a filelink
Expand All @@ -80,7 +82,8 @@ def store
add_transform_task('store', {})
)
response = UploadUtils.make_call(url, 'get')
handle = response.body['url'].split('/').last
response_body = JSON.parse(response.body)
handle = response_body['url'].split('/').last
FilestackFilelink.new(handle, apikey: @apikey, security: @security)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/filestack/utils/multipart_upload_utils.rb
Expand Up @@ -234,7 +234,7 @@ def multipart_complete(apikey, filename, filesize, mimetype, start_response, par
# @param [Hash] options User-defined options for
# multipart uploads
#
# @return [Typhoeus::Response]
# @return [Hash]
def multipart_upload(apikey, filepath, security, options, timeout, intelligent: false)
filename, filesize, mimetype = get_file_info(filepath)
start_response = multipart_start(
Expand Down Expand Up @@ -272,6 +272,6 @@ def multipart_upload(apikey, filepath, security, options, timeout, intelligent:
rescue
raise "Upload timed out upon completion. Please try again later"
end
response_complete.body
JSON.parse(response_complete.body)
end
end
7 changes: 4 additions & 3 deletions lib/filestack/utils/utils.rb
Expand Up @@ -73,7 +73,7 @@ def make_call(url, action, parameters: nil, headers: nil)
# multipart uploads
# @param [String] storage Storage destination
# (s3, rackspace, etc)
# @return [Typhoeus::Response]
# @return [Hash]
def send_upload(apikey, filepath: nil, external_url: nil, security: nil, options: nil, storage: 'S3')
data = if filepath
{ fileUpload: File.open(filepath) }
Expand All @@ -93,7 +93,8 @@ def send_upload(apikey, filepath: nil, external_url: nil, security: nil, options

response = make_call(base, 'post', parameters: data)
if response.code == 200
handle = response.body['url'].split('/').last
response_body = JSON.parse(response.body)
handle = response_body['url'].split('/').last
return { 'handle' => handle }
end
raise response.body
Expand Down Expand Up @@ -390,7 +391,7 @@ def upload_chunk_intelligently(job, state, apikey, filepath, options)
rescue
raise 'BACKEND_NETWORK'
end
fs_response = fs_response.body
fs_response = JSON.parse(fs_response.body)

# PUT to S3
begin
Expand Down
29 changes: 15 additions & 14 deletions spec/filestack/ruby_spec.rb
Expand Up @@ -34,7 +34,7 @@ class GeneralResponse

def initialize(body_content, error_number = 200)
@code = error_number
@body = body_content
@body = body_content.to_json
end

def code
Expand Down Expand Up @@ -98,12 +98,13 @@ def code
allow(Base64).to receive(:urlsafe_encode64).and_return(mock_hash)
allow(OpenSSL::HMAC).to receive(:hexdigest).and_return(mock_signature)

options = { 'expiry' => 3600 }
security = FilestackSecurity.new(@test_secret)
security.generate(@test_secret, options)
[{ 'expiry' => 3600 }, { expiry: 3600 }].each do |options|
security = FilestackSecurity.new(@test_secret)
security.generate(@test_secret, options)

expect(security.policy).to eq(mock_hash)
expect(security.signature).to eq(mock_signature)
expect(security.policy).to eq(mock_hash)
expect(security.signature).to eq(mock_signature)
end
end

it 'Filesecurity.sign_url called successfully' do
Expand All @@ -127,7 +128,7 @@ def code
end

def body
{'url' => 'https://cdn.filestackcontent.com/somehandle'}
{'url' => 'https://cdn.filestackcontent.com/somehandle'}.to_json
end
end
allow(Typhoeus).to receive(:post)
Expand All @@ -143,7 +144,7 @@ def code
end

def body
{'url' => 'https://cdn.filestackcontent.com/somehandle'}
{'url' => 'https://cdn.filestackcontent.com/somehandle'}.to_json
end
end
allow(Typhoeus).to receive(:post)
Expand Down Expand Up @@ -204,7 +205,7 @@ def body
{
url: 'someurl',
headers: 'seomheaders'
}
}.to_json
end
end
allow(Typhoeus).to receive(:post).and_return(FilestackResponse.new)
Expand Down Expand Up @@ -443,7 +444,7 @@ def body
{
url: 'someurl',
headers: 'someheaders'
}
}.to_json
end

def code
Expand Down Expand Up @@ -472,7 +473,7 @@ def body
{
url: 'someurl',
headers: 'someheaders'
}
}.to_json
end

def code
Expand Down Expand Up @@ -544,14 +545,14 @@ def code
allow(UploadUtils).to receive(:make_call)
.and_return(GeneralResponse.new({data: 'data'}))
metadata = @test_filelink.metadata
expect(metadata[:data]).to eq('data')
expect(metadata['data']).to eq('data')
end

it 'gets metadata with security' do
allow(UploadUtils).to receive(:make_call)
.and_return(GeneralResponse.new({data: 'data'}))
metadata = @test_secure_filelink.metadata
expect(metadata[:data]).to eq('data')
expect(metadata['data']).to eq('data')
end

###################
Expand All @@ -576,7 +577,7 @@ def body
{ 'status' => 'completed',
'data' => {
'url' => 'https://cdn.filestackcontent.com/somehandle'
} }
} }.to_json
end
end
allow(Typhoeus).to receive(:post).and_return(@response)
Expand Down

0 comments on commit c8a3d9e

Please sign in to comment.