|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
|
|
|
|
local JSON = require("json")
|
|
|
|
|
local http = require("socket.http")
|
|
|
|
|
local logger = require("logger")
|
|
|
|
|
local ltn12 = require("ltn12")
|
|
|
|
|
local socket = require("socket")
|
|
|
|
|
local socketutil = require("socketutil")
|
|
|
|
@ -12,6 +13,7 @@ local DropBoxApi = {
|
|
|
|
|
local API_URL_INFO = "https://api.dropboxapi.com/2/users/get_current_account"
|
|
|
|
|
local API_LIST_FOLDER = "https://api.dropboxapi.com/2/files/list_folder"
|
|
|
|
|
local API_DOWNLOAD_FILE = "https://content.dropboxapi.com/2/files/download"
|
|
|
|
|
local API_LIST_ADD_FOLDER = "https://api.dropboxapi.com/2/files/list_folder/continue"
|
|
|
|
|
|
|
|
|
|
function DropBoxApi:fetchInfo(token)
|
|
|
|
|
local sink = {}
|
|
|
|
@ -64,6 +66,12 @@ function DropBoxApi:fetchListFolders(path, token)
|
|
|
|
|
if result_response ~= "" then
|
|
|
|
|
local ret, result = pcall(JSON.decode, result_response)
|
|
|
|
|
if ret then
|
|
|
|
|
-- Check if more results, and then get them
|
|
|
|
|
if result.has_more then
|
|
|
|
|
logger.dbg("Found additional files")
|
|
|
|
|
result = self:fetchAdditionalFolders(result, token)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
else
|
|
|
|
|
return nil
|
|
|
|
@ -162,4 +170,48 @@ function DropBoxApi:showFiles(path, token)
|
|
|
|
|
return dropbox_files
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function DropBoxApi:fetchAdditionalFolders(response, token)
|
|
|
|
|
local out = response
|
|
|
|
|
local cursor = response.cursor
|
|
|
|
|
|
|
|
|
|
repeat
|
|
|
|
|
local data = "{\"cursor\": \"" .. cursor .. "\"}"
|
|
|
|
|
|
|
|
|
|
local sink = {}
|
|
|
|
|
socketutil:set_timeout()
|
|
|
|
|
local request = {
|
|
|
|
|
url = API_LIST_ADD_FOLDER,
|
|
|
|
|
method = "POST",
|
|
|
|
|
headers = {
|
|
|
|
|
["Authorization"] = "Bearer ".. token,
|
|
|
|
|
["Content-Type"] = "application/json",
|
|
|
|
|
["Content-Length"] = #data,
|
|
|
|
|
},
|
|
|
|
|
source = ltn12.source.string(data),
|
|
|
|
|
sink = ltn12.sink.table(sink),
|
|
|
|
|
}
|
|
|
|
|
local headers_request = socket.skip(1, http.request(request))
|
|
|
|
|
socketutil:reset_timeout()
|
|
|
|
|
if headers_request == nil then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local result_response = table.concat(sink)
|
|
|
|
|
local ret, result = pcall(JSON.decode, result_response)
|
|
|
|
|
|
|
|
|
|
if not ret then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
for __, v in ipairs(result.entries) do
|
|
|
|
|
table.insert(out.entries, v)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if result.has_more then
|
|
|
|
|
cursor = result.cursor
|
|
|
|
|
end
|
|
|
|
|
until not result.has_more
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
end
|
|
|
|
|
return DropBoxApi
|
|
|
|
|