examples/api-samples/samples/sample18.rb in groupdocs-2.2.0 vs examples/api-samples/samples/sample18.rb in groupdocs-2.3.0

- old
+ new

@@ -1,190 +1,190 @@ -#GET request -get '/sample-18-how-to-convert-doc-to-docx-docx-to-doc-docx-to-pdf-and-ppt-to-pdf' do - haml :sample18 -end - -#POST request -post '/sample18/convert_callback' do - #Set download path - downloads_path = "#{File.dirname(__FILE__)}/../public/downloads" - - #Get callback request - data = JSON.parse(request.body.read) - begin - raise 'Empty params!' if data.empty? - source_id = nil - client_id = nil - private_key = nil - - #Get value of SourceId - data.each do |key, value| - if key == 'SourceId' - source_id = value - end - end - - #Get private key and client_id from file user_info.txt - if File.exist?("#{File.dirname(__FILE__)}/../public/user_info.txt") - contents = File.read("#{File.dirname(__FILE__)}/../public/user_info.txt") - contents = contents.split(' ') - client_id = contents.first - private_key = contents.last - end - - #Create Job instance - job = GroupDocs::Job.new({:id => source_id}) - - #Get document by job id - documents = job.documents!({:client_id => client_id, :private_key => private_key}) - - #Download converted file - documents[:inputs].first.outputs.first.download!(downloads_path, {:client_id => client_id, :private_key => private_key}) - - rescue Exception => e - err = e.message - end -end - - -#GET request -get '/sample18/check' do - - #Check is there download directory - unless File.directory?("#{File.dirname(__FILE__)}/../public/downloads") - return 'Directory was not found.' - end - - #Get file name from download directory - name = nil - Dir.entries("#{File.dirname(__FILE__)}/../public/downloads").each do |file| - name = file if file != '.' && file != '..' - end - - name -end - -#GET request -get '/sample18/downloads/:filename' do |filename| - # Send file with header to download it - send_file "#{File.dirname(__FILE__)}/../public/downloads/#{filename}", :filename => filename, :type => 'Application/octet-stream' -end - - -#POST request -post '/sample-18-how-to-convert-doc-to-docx-docx-to-doc-docx-to-pdf-and-ppt-to-pdf' do - #Set variables - set :client_id, params[:clientId] - set :private_key, params[:privateKey] - set :source, params[:source] - set :file_id, params[:fileId] - set :url, params[:url] - set :convert_type, params[:convertType] - set :callback, params[:callback] - set :base_path, params[:basePath] - - #Set download path - downloads_path = "#{File.dirname(__FILE__)}/../public/downloads" - - #Remove all files from download directory or create folder if it not there - if File.directory?(downloads_path) - Dir.foreach(downloads_path) { |f| fn = File.join(downloads_path, f); File.delete(fn) if f != '.' && f != '..' } - else - Dir::mkdir(downloads_path) - end - - begin - #Check required variables - raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty? - - #Prepare base path - if settings.base_path.empty? - base_path = 'https://api.groupdocs.com' - elsif settings.base_path.match('/v2.0') - base_path = settings.base_path.split('/v2.0')[0] - else - base_path = settings.base_path - end - - #Configure your access to API server - GroupDocs.configure do |groupdocs| - groupdocs.client_id = settings.client_id - groupdocs.private_key = settings.private_key - #Optionally specify API server and version - groupdocs.api_server = base_path # default is 'https://api.groupdocs.com' - end - - #Write client and private key to the file for callback job - if settings.callback[0] - out_file = File.new("#{File.dirname(__FILE__)}/../public/user_info.txt", 'w') - #White space is required - out_file.write("#{settings.client_id} ") - out_file.write("#{settings.private_key}") - out_file.close - end - - file = nil - - #Get document by file GUID - case settings.source - when 'guid' - #Create instance of File - file = GroupDocs::Storage::File.new({:guid => settings.file_id}) - when 'local' - #Construct path - file_path = "#{Dir.tmpdir}/#{params[:file][:filename]}" - #Open file - File.open(file_path, 'wb') { |f| f.write(params[:file][:tempfile].read) } - #Make a request to API using client_id and private_key - file = GroupDocs::Storage::File.upload!(file_path, {}) - when 'url' - #Upload file from defined url - file = GroupDocs::Storage::File.upload_web!(settings.url) - else - raise 'Wrong GUID source.' - end - - #Raise exception if something went wrong - raise 'No such file' unless file.is_a?(GroupDocs::Storage::File) - - #Make document from file - document = file.to_document - #Convert document - convert = document.convert!(settings.convert_type, {:callback => settings.callback}) - #Waite 15 seconds for while file converting - for i in 1..5 - i+=1 - sleep(2) - if GroupDocs::Job.get!(convert.id).status.to_s == 'archived' then break end - end - - - #Get array of changes in document from job - original_document = convert.documents!() - - #Get converted document GUID - guid = original_document[:inputs].first.outputs.first.guid - - #Prepare to sign url - iframe = "/document-viewer/embed/#{guid}" - # Construct result string - url = GroupDocs::Api::Request.new(:path => iframe).prepare_and_sign_url - #Generate iframe URL - case base_path - when 'https://stage-api-groupdocs.dynabic.com' - iframe = "https://stage-api-groupdocs.dynabic.com#{url}" - when 'https://dev-api-groupdocs.dynabic.com' - iframe = "https://dev-apps.groupdocs.com#{url}" - else - iframe = "https://apps.groupdocs.com#{url}" - end - - #Make iframe - iframe = "<iframe id='downloadframe' src='#{iframe}' width='800' height='1000'></iframe>" - - rescue Exception => e - err = e.message - end - - #Set variables for template - haml :sample18, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :callback => settings.callback, :fileId => file, :converted => guid, :iframe => iframe, :err => err} -end +#GET request +get '/sample-18-how-to-convert-doc-to-docx-docx-to-doc-docx-to-pdf-and-ppt-to-pdf' do + haml :sample18 +end + +#POST request +post '/sample18/convert_callback' do + #Set download path + downloads_path = "#{File.dirname(__FILE__)}/../public/downloads" + + #Get callback request + data = JSON.parse(request.body.read) + begin + raise 'Empty params!' if data.empty? + source_id = nil + client_id = nil + private_key = nil + + #Get value of SourceId + data.each do |key, value| + if key == 'SourceId' + source_id = value + end + end + + #Get private key and client_id from file user_info.txt + if File.exist?("#{File.dirname(__FILE__)}/../public/user_info.txt") + contents = File.read("#{File.dirname(__FILE__)}/../public/user_info.txt") + contents = contents.split(' ') + client_id = contents.first + private_key = contents.last + end + + #Create Job instance + job = GroupDocs::Job.new({:id => source_id}) + + #Get document by job id + documents = job.documents!({:client_id => client_id, :private_key => private_key}) + + #Download converted file + documents[:inputs].first.outputs.first.download!(downloads_path, {:client_id => client_id, :private_key => private_key}) + + rescue Exception => e + err = e.message + end +end + + +#GET request +get '/sample18/check' do + + #Check is there download directory + unless File.directory?("#{File.dirname(__FILE__)}/../public/downloads") + return 'Directory was not found.' + end + + #Get file name from download directory + name = nil + Dir.entries("#{File.dirname(__FILE__)}/../public/downloads").each do |file| + name = file if file != '.' && file != '..' + end + + name +end + +#GET request +get '/sample18/downloads/:filename' do |filename| + # Send file with header to download it + send_file "#{File.dirname(__FILE__)}/../public/downloads/#{filename}", :filename => filename, :type => 'Application/octet-stream' +end + + +#POST request +post '/sample-18-how-to-convert-doc-to-docx-docx-to-doc-docx-to-pdf-and-ppt-to-pdf' do + #Set variables + set :client_id, params[:clientId] + set :private_key, params[:privateKey] + set :source, params[:source] + set :file_id, params[:fileId] + set :url, params[:url] + set :convert_type, params[:convertType] + set :callback, params[:callback] + set :base_path, params[:basePath] + + #Set download path + downloads_path = "#{File.dirname(__FILE__)}/../public/downloads" + + #Remove all files from download directory or create folder if it not there + if File.directory?(downloads_path) + Dir.foreach(downloads_path) { |f| fn = File.join(downloads_path, f); File.delete(fn) if f != '.' && f != '..' } + else + Dir::mkdir(downloads_path) + end + + begin + #Check required variables + raise 'Please enter all required parameters' if settings.client_id.empty? or settings.private_key.empty? + + #Prepare base path + if settings.base_path.empty? + base_path = 'https://api.groupdocs.com' + elsif settings.base_path.match('/v2.0') + base_path = settings.base_path.split('/v2.0')[0] + else + base_path = settings.base_path + end + + #Configure your access to API server + GroupDocs.configure do |groupdocs| + groupdocs.client_id = settings.client_id + groupdocs.private_key = settings.private_key + #Optionally specify API server and version + groupdocs.api_server = base_path # default is 'https://api.groupdocs.com' + end + + #Write client and private key to the file for callback job + if settings.callback[0] + out_file = File.new("#{File.dirname(__FILE__)}/../public/user_info.txt", 'w') + #White space is required + out_file.write("#{settings.client_id} ") + out_file.write("#{settings.private_key}") + out_file.close + end + + file = nil + + #Get document by file GUID + case settings.source + when 'guid' + #Create instance of File + file = GroupDocs::Storage::File.new({:guid => settings.file_id}) + when 'local' + #Construct path + file_path = "#{Dir.tmpdir}/#{params[:file][:filename]}" + #Open file + File.open(file_path, 'wb') { |f| f.write(params[:file][:tempfile].read) } + #Make a request to API using client_id and private_key + file = GroupDocs::Storage::File.upload!(file_path, {}) + when 'url' + #Upload file from defined url + file = GroupDocs::Storage::File.upload_web!(settings.url) + else + raise 'Wrong GUID source.' + end + + #Raise exception if something went wrong + raise 'No such file' unless file.is_a?(GroupDocs::Storage::File) + + #Make document from file + document = file.to_document + #Convert document + convert = document.convert!(settings.convert_type, {:callback => settings.callback}) + #Waite 15 seconds for while file converting + for i in 1..5 + i+=1 + sleep(2) + if GroupDocs::Job.get!(convert.id).status.to_s == 'archived' then break end + end + + + #Get array of changes in document from job + original_document = convert.documents!() + + #Get converted document GUID + guid = original_document[:inputs].first.outputs.first.guid + + #Prepare to sign url + iframe = "/document-viewer/embed/#{guid}" + # Construct result string + url = GroupDocs::Api::Request.new(:path => iframe).prepare_and_sign_url + #Generate iframe URL + case base_path + when 'https://stage-api-groupdocs.dynabic.com' + iframe = "https://stage-api-groupdocs.dynabic.com#{url}" + when 'https://dev-api-groupdocs.dynabic.com' + iframe = "https://dev-apps.groupdocs.com#{url}" + else + iframe = "https://apps.groupdocs.com#{url}" + end + + #Make iframe + iframe = "<iframe id='downloadframe' src='#{iframe}' width='800' height='1000'></iframe>" + + rescue Exception => e + err = e.message + end + + #Set variables for template + haml :sample18, :locals => {:userId => settings.client_id, :privateKey => settings.private_key, :callback => settings.callback, :fileId => file, :converted => guid, :iframe => iframe, :err => err} +end