# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'upload/UploadResponseBuilder.rb' require 'upload/Upload.rb' require 'upload/UploadFileType.rb' module App42 module Upload # # Uploads file on the cloud. Allows access to the files through url. # Its especially useful for Mobile/Device apps. It minimizes the App footprint # on the device. # # @see Upload # @see App42Response # class UploadService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "File Upload->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "upload" @version = "1.0" end # # Uploads file on the cloud. # # @param name # - The name for the file which has to be saved. It is used to retrieve the file # @param filePath # - The local path for the file # @param fileType # - The type of the file. File can be either Audio, Video, Image, Binary, Txt, xml, json, csv or other # Use the static constants e.g. Upload.AUDIO, Upload.XML etc. # @param description # - Description of the file to be uploaded. # # @return Upload object # # @raise App42Exception # def upload_file(name, filePath, fileType, description) puts "upload File Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(name, "Name"); util.throwExceptionIfNullOrBlank(filePath, "Filepath"); util.throwExceptionIfNullOrBlank(fileType, "fileType"); util.throwExceptionIfNullOrBlank(description, "Description"); if (FileTest.exists?(filePath) == false) raise App42Exception.new("The file with the name #{filePath} not found") end begin if (UploadFileType.new.isAvailable(fileType) == nil) raise App42NotFoundException.new("Upload File Type #{fileType} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc } params = query_params.clone post_params = Hash.new upload = UploadFileType.new() post_params.store("name", name); post_params.store("type", upload.enum(fileType)) post_params.store("description", description); params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.multipart(signature, resource_url, query_params, params, filePath) upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Uploads file on the cloud. # # @param name # - The name for the file which has to be saved. It is used to # retrieve the file # @param inputStream # - InputStream of the file to be uploaded. # @param fileType # - The type of the file. File can be either Audio, Video, Image, # Binary, Txt, xml, json, csv or other Use the static constants # e.g. Upload.AUDIO, Upload.XML etc. # @param description # - Description of the file to be uploaded. # # @return Upload object # # @raise App42Exception # def upload_file_by_stream(name, inputStream, fileType, description) puts "upload File Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(name, "Name"); util.throwExceptionIfNotValidExtension(name, "File Name"); util.throwExceptionIfNullOrBlank(fileType, "fileType"); util.throwExceptionIfNullOrBlank(description, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc } params = query_params.clone post_params = Hash.new upload = UploadFileType.new() post_params.store("name", name); post_params.store("type", upload.enum(fileType)) post_params.store("description", description); params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.multipartStream(signature, resource_url, query_params, params, inputStream) upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Uploads file on the cloud for given user. # # @param name # - The name for the file which has to be saved. It is used to # retrieve the file # @param userName # - The name for the user for which file has to be saved. # @param filePath # - The local path for the file # @param fileType # - The type of the file. File can be either Audio, Video, Image, # Binary, Txt, xml, json, csv or other Use the static constants # e.g. Upload.AUDIO, Upload.XML etc. # @param description # - Description of the file to be uploaded. # # @return Upload object # # @raise App42Exception # def upload_file_for_user(name, userName, filePath, fileType, description) puts "upload File for user Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(name, "File Name"); util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(filePath, "Filepath"); util.throwExceptionIfNullOrBlank(fileType, "fileType"); util.throwExceptionIfNullOrBlank(description, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc } params = query_params.clone post_params = Hash.new upload = UploadFileType.new() post_params.store("name", name); post_params.store("userName", userName); post_params.store("type", upload.enum(fileType)) post_params.store("description", description); params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}" response = connection.multipart(signature, resource_url, query_params, params, filePath) upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Uploads file on the cloud. # # @param name # - The name for the file which has to be saved. It is used to retrieve the file # @param userName # - The name for the user for which file has to be saved. # @param filePath # - The local path for the file # @param uploadFileType # - The type of the file. File can be either Audio, Video, Image, Binary, Txt, xml, json, csv or other # Use the static constants e.g. Upload.AUDIO, Upload.XML etc. # @param description # - Description of the file to be uploaded. # # @return Upload object # # @raise App42Exception # def upload_file_for_user_by_stream(name, userName, inputStream, fileType, description) puts "upload File Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(name, "File Name"); util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNotValidExtension(name, "File Name"); util.throwExceptionIfNullOrBlank(fileType, "fileType"); util.throwExceptionIfNullOrBlank(description, "Description"); if (FileTest.exists?(filePath) == false) raise App42Exception.new("The file with the name #{filePath} not found") end begin if (UploadFileType.new.isAvailable(fileType) == nil) raise App42NotFoundException.new("Upload File Type #{fileType} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc } params = query_params.clone post_params = Hash.new upload = UploadFileType.new() post_params.store("name", name); post_params.store("type", upload.enum(fileType)) post_params.store("description", description); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.multipartStream(signature, resource_url, query_params, params, inputStream) upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # Gets all the files By Paging for the App # # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Upload object # # @raise App42Exception # def get_all_files_by_paging(max, offset) puts "getAllFilesByPaging Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/paging/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) upload = UploadResponseBuilder.new uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Gets the file based on user name by Paging. # # @param userName # - The name of the user for which file has to be retrieved # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Upload object # # @raise App42Exception # def get_all_files_by_user_by_paging(userName, max, offset) puts "getAllFilesByPaging Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userName", userName); params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/user/#{userName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) upload = UploadResponseBuilder.new uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Get the files based on file type by Paging. # # @param uploadFileType # - Type of the file e.g. Upload.AUDIO, Upload.XML etc. # @param # - max Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Upload object # # @raise App42Exception # def get_files_by_type_by_paging(uploadFileType, max, offset) puts "get_files_by_type_by_paging Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(uploadFileType, "uploadFileType"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin if (UploadFileType.new.isAvailable(uploadFileType) == nil) raise App42NotFoundException.new("Upload File Type #{uploadFileType} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("type", uploadFileType) params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/type/#{uploadFileType}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) upload = UploadResponseBuilder.new uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Gets all the files for the App # # @return Upload object # # @raise App42Exception # def get_all_files() puts "Get All Files Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Gets the file based on user name. # # @param name # - The name of the file which has to be retrieved # @param userName # - The name of the user for which file has to be retrieved # # @return Upload object # # @raise App42Exception # def get_file_by_user(name, userName) puts "Get File By User Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(name, "File Name"); util.throwExceptionIfNullOrBlank(userName, "User Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("name", name); params.store("userName", userName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}/#{name}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Gets the file based on user name. # # @param userName # - The name of the user for which file has to be retrieved # # @return Upload object # # @raise App42Exception # def get_all_files_by_user(userName) puts "getAllFilesByUser Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "userName"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userName", userName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/user/#{userName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Gets the file based on file name. # # @param name # - The name of the file which has to be retrieved # # @return Upload object # # @raise App42Exception # def get_file_by_name(name) puts "Get File By Name Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(name, "File Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("name", name) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{name}" response = connection.get(signature, resource_url, query_params) upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Removes the file based on file name and user name. # # @param name # - The name of the file which has to be removed # @param userName # - The name of the user for which file has to be removed # # @return App42Response if deleted successfully # # @raise App42Exception # def remove_file_by_user(name, userName) puts "remove_file_by_user Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new() util = Util.new util.throwExceptionIfNullOrBlank(name, "File Name"); util.throwExceptionIfNullOrBlank(userName, "User Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("name", name); params.store("userName", userName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}/#{name}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Removes the file based on user name. # # @param userName # - The name of the user for which file has to be removed # # @return App42Response if deleted successfully # # @raise App42Exception # def remove_all_files_by_user(userName) puts "remove_all_files_by_user Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new() util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userName", userName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/user/#{userName}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Removes the file based on file name. # # @param name # - The name of the file which has to be removed # # @return App42Response if deleted successfully # # @raise App42Exception # def remove_file_by_name(name) puts "Remove File By Name Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(name, "Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("name", name) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{name}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Removes all the files for the App # # @return App42Response if deleted successfully # # @raise App42Exception # def remove_all_files() puts "Remove File By Name Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Get the files based on file type. # # @param uploadFileType # - Type of the file e.g. Upload.AUDIO, Upload.XML etc. # # @return Upload object # # @raise App42Exception # def get_files_by_type(uploadFileType) puts "get Files By Type Called " puts "Base url #{@base_url}" response = nil; uploadObj = nil; uploadObj = Upload.new util = Util.new util.throwExceptionIfNullOrBlank(uploadFileType, "uploadFileType"); begin if (UploadFileType.new.isAvailable(uploadFileType) == nil) raise App42NotFoundException.new("Upload File Type #{uploadFileType} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("type", uploadFileType) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/type/#{uploadFileType}" response = connection.get(signature, resource_url, query_params) upload = UploadResponseBuilder.new() uploadObj = upload.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return uploadObj end # # Gets count of all the files for the App # # @return App42Response object # # @raise App42Exception # def get_all_files_count() puts "get_all_files_count Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new util = Util.new begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = UploadResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # Gets the count of file based on user name. # # @param userName # - The name of the user for which count of the file has to be retrieved # # @return App42Response object # # @raise App42Exception # def get_all_files_count_by_user(userName) puts "get_all_files_count_by_user Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userName", userName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/user/#{userName}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = UploadResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Get the count of files based on file type. # # @param uploadFileType # - Type of the file e.g. Upload.AUDIO, Upload.XML etc. # # @return App42Response object # # @raise App42Exception # def get_files_count_by_type(uploadFileType) puts "getFilesCountByType Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(uploadFileType, "uploadFileType"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("type", uploadFileType); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/type/#{uploadFileType}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = UploadResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end end end end