Class: DropboxUploader
- Inherits:
-
Object
- Object
- DropboxUploader
- Defined in:
- lib/dropbox_uploader.rb
Overview
About this library
A simple Dropbox ( www.dropbox.com/ ) library to upload files, with no other gem dependencies. This library has been completely written from scratch, but ideas and logics are inspired from Dropbox Uploader ( jaka.kubje.org/software/DropboxUploader/ ).
Example
require "dropbox_uploader"
dropbox = DropboxUploader.new("[email protected]", "MyPassword")
dropbox.upload("localfile.txt", "/")
Known bugs
-
error check and recovery
-
non-ASCII file/remotedir name support
Instance Method Summary collapse
-
#initialize(email, pass, capath = nil) ⇒ DropboxUploader
constructor
Create Dropbox instance and initialize it.
-
#login ⇒ Object
:nodoc:.
-
#upload(file, remote) ⇒ Object
Upload local file to Dropbox remote directory.
Constructor Details
#initialize(email, pass, capath = nil) ⇒ DropboxUploader
Create Dropbox instance and initialize it. email is your email registered at Dropobox. pass is your password, too. capath is the path of directory of CA files.
56 57 58 59 60 61 62 |
# File 'lib/dropbox_uploader.rb', line 56 def initialize(email, pass, capath = nil) @email = email @pass = pass @ca = capath @cookie = nil @login = false end |
Instance Method Details
#login ⇒ Object
:nodoc:
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dropbox_uploader.rb', line 65 def login html = send_request("https://www.dropbox.com/login").body token = extract_token(html, "/login") raise "token not found on /login" unless token res = send_request("https://www.dropbox.com/login", "login_email" => @email, "login_password" => @pass, "t" => token) if res["location"] == "/home" @login = true else raise "login failed #{res.code}:#{res.}" end end |
#upload(file, remote) ⇒ Object
Upload local file to Dropbox remote directory. file is a local file path. remote is the target remote directory.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dropbox_uploader.rb', line 82 def upload(file, remote) login unless @login html = send_request("https://www.dropbox.com/home?upload=1").body token = extract_token(html, "https://dl-web.dropbox.com/upload") raise "token not found on /upload" unless token stream = open(file, "rb") rawdata = stream.read filesize = rawdata.size boundary = generate_boundary(rawdata) rawdata = nil stream.rewind pre = "" {"dest" => remote, "t" => token}.each do |k,v| pre << "--#{boundary}\r\n" pre << %'Content-Disposition: form-data; name="#{k}"\r\n' pre << "\r\n" pre << %'#{v}\r\n' end pre << "--#{boundary}\r\n" pre << %'Content-Disposition: form-data; name="file"; filename="#{File.basename(file)}"\r\n' pre << "Content-Type: application/octet-stream\r\n" pre << "\r\n" post = "\r\n" post << "--#{boundary}--\r\n" data = PseudoIO.new(pre, [stream, filesize], post) res = send_request("https://dl-web.dropbox.com/upload", data, boundary) if res.code[0] != ?2 && res.code[0] != ?3 raise "upload failed #{res.code}:#{res.}" end true end |