Skip to content

Commit

Permalink
Initial file upload implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
superchilled committed Jul 11, 2023
1 parent e8c1c24 commit d7980db
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
4 changes: 0 additions & 4 deletions lib/vonage/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def initialize
self.api_secret = T.let(ENV['VONAGE_API_SECRET'], T.nilable(String))
self.application_id = ENV['VONAGE_APPLICATION_ID']
self.logger = (defined?(::Rails.logger) && ::Rails.logger) || Vonage::Logger.new(nil)
self.meetings_host = 'api-eu.vonage.com'
self.private_key = ENV['VONAGE_PRIVATE_KEY_PATH'] ? File.read(T.must(ENV['VONAGE_PRIVATE_KEY_PATH'])) : ENV['VONAGE_PRIVATE_KEY']
self.rest_host = 'rest.nexmo.com'
self.signature_secret = ENV['VONAGE_SIGNATURE_SECRET']
Expand Down Expand Up @@ -136,9 +135,6 @@ def logger=(logger)
@logger = T.let(Logger.new(logger), T.nilable(Vonage::Logger))
end

sig { returns(String) }
attr_accessor :meetings_host

# Returns the value of attribute private_key.
#
# @return [String]
Expand Down
74 changes: 66 additions & 8 deletions lib/vonage/meetings/themes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,72 @@ def list_rooms(theme_id:, **params)
# @see TODO: add docs link
#
# TODO: add type signature
def upload_logo(theme_id:)
# TODO: combine 3 steps to upload logo:
#
# 1. Get URLs that can be used to upload logos for a theme via a POST: https://nexmo-developer.herokuapp.com/api/meetings#getUploadUrlsForTheme
# 2. Make a POST request to the URL
# 3. Change given logo to be permanent: https://nexmo-developer.herokuapp.com/api/meetings#finalizeLogosForTheme
#
# See documentation for more info: https://developer.vonage.com/en/meetings/code-snippets/theme-management#uploading-icons-and-logos
def set_logo(theme_id:, filepath:, logo_type:)
pn = Pathname.new(filepath)
valid_logo_types = ['white', 'colored', 'favicon']
raise ArgumentError, ':filepath not for a file' unless pn.file?
raise ArgumentError, 'file at :filepath not readable' unless pn.readable?
raise ArgumentError, "logo_type: must be one of #{valid_logo_types}" unless valid_logo_types.include?(logo_type)

creds = get_logo_upload_credentials

filtered_creds = creds.select {|cred| cred.fields.logo_type == logo_type }.first

s3_upload_response = upload_logo_file(filepath: filepath, credentials: filtered_creds)

if s3_upload_response.http_response.code == '204'
finalize_logos(theme_id: theme_id, keys: [filtered_creds.fields.key])
else
raise ClientError, 'problem with upload'
end
end

def get_logo_upload_credentials
request("/beta/meetings/themes/logos-upload-urls", response_class: ListResponse)
end

def upload_logo_file(filepath:, credentials:)
pn = Pathname.new(filepath)

creds_key_map = {
content_type: "Content-Type",
logo_type: "logoType",
x_amz_algorithm: "X-Amz-Algorithm",
x_amz_credential: "X-Amz-Credential",
x_amz_date: "X-Amz-Date",
x_amz_security_token: "X-Amz-Security-Token",
policy: "Policy",
x_amz_signature: "X-Amz-Signature"
}

params = {}
credentials.fields.attributes.each do |k,v|
if creds_key_map.keys.include?(k)
params[creds_key_map[k]] = v
else
params[k.to_s] = v
end
end

multipart_post_request(
nil,
filepath: filepath,
file_name: pn.basename,
mime_type: credentials.fields.content_type,
params: params,
override_uri: credentials.url,
no_auth: true
)
end

def finalize_logos(theme_id:, keys: [])
request(
"/beta/meetings/themes/" + theme_id + "/finalizeLogos",
params: {
keys: keys
},
type: Put
)
end
end
end
16 changes: 10 additions & 6 deletions lib/vonage/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,29 @@ def request(path, params: nil, type: Get, response_class: Response, &block)
end
end

def multipart_post_request(path, filepath:, file_name:, mime_type:, response_class: Response, &block)
authentication = self.class.authentication.new(@config)
def multipart_post_request(path, filepath:, file_name:, mime_type:, params: {}, override_uri: nil, no_auth: false, response_class: Response, &block)
authentication = self.class.authentication.new(@config) unless no_auth

uri = override_uri ? URI(override_uri) : URI('https://' + @host + path)

uri = URI('https://' + @host + path)
http = override_uri ? Net::HTTP.new(uri.host, Net::HTTP.https_default_port, p_addr = nil) : @http
http.use_ssl = true
http.set_debug_output($stdout)

response = File.open(filepath) do |file|
request = Net::HTTP::Post::Multipart.new(
uri,
{file: Multipart::Post::UploadIO.new(file, mime_type, file_name)}
params.merge(file: Multipart::Post::UploadIO.new(file, mime_type, file_name))
)

request['User-Agent'] = UserAgent.string(@config.app_name, @config.app_version)

# Set BearerToken if needed
authentication.update(request)
authentication.update(request) unless no_auth

logger.log_request_info(request)

@http.request(request, &block)
http.request(request, &block)
end

logger.log_response_info(response, @host)
Expand Down

0 comments on commit d7980db

Please sign in to comment.