Last active
September 26, 2016 04:26
-
-
Save varun-raj/e50780defc29cf9154dd to your computer and use it in GitHub Desktop.
dropbox_import.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'dropbox_sdk' | |
| require 'fileutils' | |
| require 'sinatra' | |
| require 'sinatra/reloader' | |
| $client = DropboxClient.new("YOUR KEY HERE") | |
| $path = "/" | |
| $root_path = "/Users/skcripthq/Projects/DropBox" | |
| def donwload_file(file) | |
| # puts "Downloading " + file | |
| contents, metadata = $client.get_file_and_metadata(file) | |
| file_path = metadata['path'] | |
| filename = "/" + file.split("/")[-1] | |
| file_path = file_path.sub(filename, '') | |
| puts file_path | |
| if !File.directory?($root_path + file_path) | |
| FileUtils::mkdir_p $root_path + file_path | |
| end | |
| open($root_path + metadata['path'], 'w') {|f| f.puts contents } | |
| return true | |
| end | |
| def traverse(folder) | |
| folder_array = Array.new | |
| file_array = Array.new | |
| folder_metadata = $client.metadata(folder) | |
| folder_metadata["contents"].each do |file| | |
| if file["is_dir"] | |
| folder_name = file['path'].to_s | |
| folder_array.push(file['path'].to_s) | |
| else | |
| file_name = file['path'].to_s | |
| file_array.push(file['path'].to_s) | |
| end | |
| end | |
| return folder_array, file_array | |
| end | |
| get '/hi' do | |
| path = params[:folder] || "/" | |
| result = path | |
| result += "<h3>Folders</h3>" | |
| folder1_array = Array.new | |
| file1_array = Array.new | |
| folder1_array, file1_array = traverse(path) | |
| folder1_array.each do |folder| | |
| result +="<a href='/hi?folder=" + folder +"'>"+folder.split('/')[-1]+"</a>" | |
| result += "<br>" | |
| end | |
| result += "<h3>Files</h3>" | |
| file1_array.each do |file| | |
| result +="<a href='/download?filename=" + file +"'>"+file.split('/')[-1]+"</a>" | |
| result += "<br>" | |
| end | |
| result | |
| end | |
| get "/download" do | |
| if donwload_file(params[:filename]) | |
| "Success Fully Downloaded" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment