7
7
-- https://github.com/JakobGreen/lua-requests/wiki
8
8
local requests = require ' requests'
9
9
local json = require ' cjson.safe'
10
+ -- https://github.com/jagt/pprint.lua
10
11
local pp = require ' pprint'
11
12
12
13
local M = {}
@@ -15,6 +16,45 @@ local M = {}
15
16
16
17
local DROPBOX_RPC_ENDPOINT = ' https://api.dropboxapi.com/2/'
17
18
local DROPBOX_CONTENT_ENDPOINT = ' https://content.dropboxapi.com/2/'
19
+ local RPC_CALLS = {
20
+ -- files
21
+ list_folder = ' files/list_folder' ,
22
+ create_folder = ' files/create_folder' ,
23
+ copy = ' files/copy' ,
24
+ delete = ' files/delete' ,
25
+ get_metadata = ' files/get_metadata' ,
26
+ list_revisions = ' files/list_revisions' ,
27
+ move = ' files/move' ,
28
+ permanently_delete = ' files/permanently_delete' ,
29
+ restore = ' files/restore' ,
30
+ search = ' files/search' ,
31
+ -- sharing
32
+ add_folder_member = ' sharing/add_folder_member' ,
33
+ check_job_status = ' sharing/check_job_status' ,
34
+ check_share_job_status = ' sharing/check_share_job_status' ,
35
+ create_shared_link_with_settings = ' sharing/create_shared_link_with_settings' ,
36
+ get_folder_metadata = ' sharing/get_folder_metadata' ,
37
+ get_shared_link_metadata = ' sharing/get_shared_link_metadata' ,
38
+ list_folder_members = ' sharing/list_folder_members' ,
39
+ list_folders = ' sharing/list_folders' ,
40
+ list_shared_links = ' sharing/list_shared_links' ,
41
+ modify_shared_link_settings = ' sharing/modify_shared_link_settings' ,
42
+ mount_folder = ' sharing/mount_folder' ,
43
+ relinquish_folder_membership = ' sharing/relinquish_folder_membership' ,
44
+ remove_folder_member = ' sharing/remove_folder_member' ,
45
+ revoke_shared_link = ' sharing/revoke_shared_link' ,
46
+ share_folder = ' sharing/share_folder' ,
47
+ transfer_folder = ' sharing/transfer_folder' ,
48
+ unmount_folder = ' sharing/unmount_folder' ,
49
+ unshare_folder = ' sharing/unshare_folder' ,
50
+ update_folder_member = ' sharing/update_folder_member' ,
51
+ update_folder_policy = ' sharing/update_folder_policy' ,
52
+ -- users
53
+ get_account = ' users/get_account' ,
54
+ get_account_batch = ' users/get_account_batch' ,
55
+ get_current_account = ' users/get_current_account' ,
56
+ get_space_usage = ' users/get_space_usage'
57
+ }
18
58
19
59
-- Module vars
20
60
@@ -23,7 +63,7 @@ local token
23
63
-- Private functions
24
64
25
65
local function add_auth (tbl )
26
- tbl [" Authorization" ] = " Bearer " .. token
66
+ tbl [" Authorization" ] = " Bearer " .. token
27
67
return tbl
28
68
end
29
69
@@ -32,24 +72,26 @@ local function json_resp(response)
32
72
if response .status_code >= 400 then
33
73
err = response .text
34
74
end
35
- return response .json (), err
75
+ local result = response .headers [' dropbox-api-result' ] and json .decode (response .headers [' dropbox-api-result' ]) or response .json ()
76
+ return result , err
36
77
end
37
78
38
79
local function make_rpc_request (reqfunc , endpoint , data )
39
- local headers = add_auth {
40
- [" Content-Type" ] = ' application/json'
41
- }
80
+ local headers = add_auth {}
81
+ if data then
82
+ headers [" Content-Type" ] = ' application/json'
83
+ end
42
84
local response = reqfunc (DROPBOX_RPC_ENDPOINT .. endpoint , {
43
85
headers = headers ,
44
86
data = data
45
87
})
46
88
return json_resp (response )
47
89
end
48
90
49
- local function make_content_request (reqfunc , endpoint , data , content )
91
+ local function make_content_upload_request (reqfunc , endpoint , data , content )
50
92
local headers = add_auth {
51
- [" Content-Type " ] = ' application/octet-stream ' ,
52
- [" Dropbox-API-Arg " ] = json . encode ( data )
93
+ [" Dropbox-API-Arg " ] = json . encode ( data ) ,
94
+ [" Content-Type " ] = ' application/octet-stream '
53
95
}
54
96
local response = reqfunc (DROPBOX_CONTENT_ENDPOINT .. endpoint , {
55
97
headers = headers ,
@@ -58,6 +100,32 @@ local function make_content_request(reqfunc, endpoint, data, content)
58
100
return json_resp (response )
59
101
end
60
102
103
+ local function make_content_download_request (reqfunc , endpoint , data )
104
+ local headers = add_auth {
105
+ [" Dropbox-API-Arg" ] = json .encode (data )
106
+ }
107
+ local response = reqfunc (DROPBOX_CONTENT_ENDPOINT .. endpoint , {
108
+ headers = headers
109
+ })
110
+ return response .text , json_resp (response )
111
+ end
112
+
113
+
114
+ local magic_calls_mt = {}
115
+ magic_calls_mt .__index = function (_ , w )
116
+ local call = RPC_CALLS [w ]
117
+ if not call then
118
+ error (" Unknown API method '" .. w .. " '" )
119
+ end
120
+ return function (args ) return make_rpc_request (requests .post , call , args ) end
121
+ end
122
+ setmetatable (M , magic_calls_mt )
123
+
124
+
125
+ local function list_folder_continue (cursor )
126
+ return make_rpc_request (requests .post , ' files/list_folder/continue' , {cursor = cursor })
127
+ end
128
+
61
129
-- Public functions
62
130
63
131
--- Set the access token for your Dropbox account.
@@ -67,44 +135,6 @@ function M.set_token(tk)
67
135
token = tk
68
136
end
69
137
70
- --- List contents of a folder.
71
- -- @string path Folder path, use empty string for ROOT level
72
- -- @bool recursive
73
- -- @bool include_media_info
74
- -- @bool include_deleted
75
- -- @see cursor
76
- -- @return Raw response, pass it to cursor() to iterate and paginate over the results
77
- -- @return Error message if error occured during operation
78
- -- otherwise you'll get only the first few hundred results
79
- function M .list_folder (path , recursive , include_media_info , include_deleted )
80
- -- https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
81
- local message = {
82
- path = path ,
83
- recursive = recursive ,
84
- include_media_info = include_media_info ,
85
- include_deleted = include_deleted
86
- }
87
- return make_rpc_request (requests .post , ' files/list_folder' , message )
88
- end
89
-
90
- local function list_folder_continue (cursor )
91
- -- https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue
92
- local message = {
93
- cursor = cursor
94
- }
95
- return make_rpc_request (requests .post , ' files/list_folder/continue' , message )
96
- end
97
-
98
- --- Create a folder.
99
- -- @string path Folder path and name, e.g. "/Homework/NewFolder"
100
- -- @return Raw response
101
- -- @return Error message if error occured during operation
102
- function M .create_folder (path )
103
- local message = {
104
- path = path
105
- }
106
- return make_rpc_request (requests .post , ' files/create_folder' , message )
107
- end
108
138
109
139
--- Iterate over a folder listing.
110
140
-- @param res raw response from list_folder
@@ -127,45 +157,60 @@ end
127
157
--- Upload (create) a file.
128
158
-- Should only be used for smaller files.
129
159
-- @string path Path of file
130
- -- @string data Raw contents of file, can be text or binary
160
+ -- @string data String or file handle
131
161
-- @return Raw response, contains meta info about created file
132
162
-- @return Error message if error occured during operation
133
- function M .upload (path , data )
134
- -- https://www.dropbox.com/developers/documentation/http/documentation#files-upload
135
- -- TODO This function should only be used for small files,
136
- -- Dropbox limit 150 MB
163
+ function M .upload (path , data , overwrite )
164
+ local body = data
137
165
local message = {
138
166
path = path ,
139
- mode = " add" ,
140
- autorename = true ,
167
+ mode = overwrite and " overwrite " or " add" ,
168
+ autorename = false ,
141
169
mute = false
142
170
}
143
- return make_content_request (requests .post , ' files/upload' , message , data )
171
+ if type (data ) == ' userdata' and io.type (data ) == ' file' then
172
+ body = data :read (" *a" )
173
+ end
174
+ local result = make_content_upload_request (requests .post , ' files/upload' , message , body )
175
+ return result
144
176
end
145
177
146
- --- Copy a file.
147
- -- @string from_path Source file path
148
- -- @string to_path Destination file path
149
- -- @return Raw response
150
- -- @return Error message if error occured during operation
151
- function M .copy (from_path , to_path )
178
+ function M .upload_session_start (chunk )
179
+ return make_content_upload_request (requests .post , ' files/upload_session/start' , nil , chunk )
180
+ end
181
+
182
+ function M .upload_session_append (session_id , offset , chunk )
152
183
local message = {
153
- from_path = from_path ,
154
- to_path = to_path
184
+ session_id = session_id ,
185
+ offset = offset
155
186
}
156
- return make_rpc_request (requests .post , ' files/copy ' , message )
187
+ return make_content_upload_request (requests .post , ' files/upload_session/append ' , message , chunk )
157
188
end
158
189
159
- --- Delete a file or folder.
160
- -- Will delete all the contents in a folder along with it.
161
- -- @string path Folder or file path, e.g. "/Homework/Folder"
162
- -- @return Raw response
163
- -- @return Error message if error occured during operation
164
- function M .delete (path )
190
+ function M .upload_session_finish (session_id , offset , path , overwrite , chunk )
165
191
local message = {
166
- path = path
192
+ cursor = {
193
+ session_id = session_id ,
194
+ offset = offset
195
+ },
196
+ commit = {
197
+ path = path ,
198
+ mode = overwrite and " overwrite" or " add" ,
199
+ autorename = false ,
200
+ mute = false
201
+ }
167
202
}
168
- return make_rpc_request (requests .post , ' files/delete' , message )
203
+ return make_content_upload_request (requests .post , ' files/upload_session/finish' , message , chunk )
204
+ end
205
+
206
+ --- Download file contents.
207
+ -- Should only be used for smaller files. Full file content is stores in a string.
208
+ -- @string path Path of file
209
+ -- @return File content
210
+ -- @return File meta info
211
+ -- @return Error message if error occured during operation
212
+ function M .download (path )
213
+ return make_content_download_request (requests .get , ' files/download' , path )
169
214
end
170
215
171
216
return M
0 commit comments