DropBox: unify error messages (#10666)

reviewable/pr10674/r1
hius07 11 months ago committed by GitHub
parent b33971b92d
commit b1c90260e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,5 @@
local BD = require("ui/bidi") local BD = require("ui/bidi")
local ButtonDialog = require("ui/widget/buttondialog") local ButtonDialog = require("ui/widget/buttondialog")
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
local CheckButton = require("ui/widget/checkbutton") local CheckButton = require("ui/widget/checkbutton")
local ConfirmBox = require("ui/widget/confirmbox") local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
@ -19,7 +18,6 @@ local lfs = require("libs/libkoreader-lfs")
local logger = require("logger") local logger = require("logger")
local _ = require("gettext") local _ = require("gettext")
local N_ = _.ngettext local N_ = _.ngettext
local Screen = require("device").screen
local T = require("ffi/util").template local T = require("ffi/util").template
local CloudStorage = Menu:extend{ local CloudStorage = Menu:extend{
@ -46,8 +44,6 @@ function CloudStorage:init()
else else
self.item_table = self:genItemTableFromRoot() self.item_table = self:genItemTableFromRoot()
end end
self.width = Screen:getWidth()
self.height = Screen:getHeight()
self.title_bar_left_icon = "plus" self.title_bar_left_icon = "plus"
self.onLeftButtonTap = function() -- add new cloud storage self.onLeftButtonTap = function() -- add new cloud storage
self:selectCloudType() self:selectCloudType()
@ -123,7 +119,7 @@ function CloudStorage:selectCloudType()
}, },
}) })
end end
self.cloud_dialog = ButtonDialogTitle:new{ self.cloud_dialog = ButtonDialog:new{
title = _("Add new cloud storage"), title = _("Add new cloud storage"),
title_align = "center", title_align = "center",
buttons = buttons, buttons = buttons,
@ -231,7 +227,7 @@ function CloudStorage:downloadFile(item)
}) })
end end
local function createTitle(filename_orig, filename, path) -- title for ButtonDialogTitle local function createTitle(filename_orig, filename, path) -- title for ButtonDialog
return T(_("Filename:\n%1\n\nDownload filename:\n%2\n\nDownload folder:\n%3"), return T(_("Filename:\n%1\n\nDownload filename:\n%2\n\nDownload folder:\n%3"),
filename_orig, filename, BD.dirpath(path)) filename_orig, filename, BD.dirpath(path))
end end
@ -247,7 +243,6 @@ function CloudStorage:downloadFile(item)
text = _("Choose folder"), text = _("Choose folder"),
callback = function() callback = function()
require("ui/downloadmgr"):new{ require("ui/downloadmgr"):new{
show_hidden = G_reader_settings:readSetting("show_hidden"),
onConfirm = function(path) onConfirm = function(path)
self.cs_settings:saveSetting("download_dir", path) self.cs_settings:saveSetting("download_dir", path)
self.cs_settings:flush() self.cs_settings:flush()
@ -322,7 +317,7 @@ function CloudStorage:downloadFile(item)
}, },
} }
self.download_dialog = ButtonDialogTitle:new{ self.download_dialog = ButtonDialog:new{
title = createTitle(filename_orig, filename, download_dir), title = createTitle(filename_orig, filename, download_dir),
buttons = buttons, buttons = buttons,
} }
@ -352,7 +347,7 @@ function CloudStorage:onMenuHold(item)
local title = T(_("Choose this folder?\n\n%1"), BD.dirpath(item.url)) local title = T(_("Choose this folder?\n\n%1"), BD.dirpath(item.url))
local onConfirm = self.onConfirm local onConfirm = self.onConfirm
local button_dialog local button_dialog
button_dialog = ButtonDialogTitle:new{ button_dialog = ButtonDialog:new{
title = title, title = title,
buttons = { buttons = {
{ {
@ -532,7 +527,7 @@ function CloudStorage:synchronizeSettings(item)
local syn_dialog local syn_dialog
local dropbox_sync_folder = item.sync_source_folder or "not set" local dropbox_sync_folder = item.sync_source_folder or "not set"
local local_sync_folder = item.sync_dest_folder or "not set" local local_sync_folder = item.sync_dest_folder or "not set"
syn_dialog = ButtonDialogTitle:new { syn_dialog = ButtonDialog:new {
title = T(_("Dropbox folder:\n%1\nLocal folder:\n%2"), BD.dirpath(dropbox_sync_folder), BD.dirpath(local_sync_folder)), title = T(_("Dropbox folder:\n%1\nLocal folder:\n%2"), BD.dirpath(dropbox_sync_folder), BD.dirpath(local_sync_folder)),
title_align = "center", title_align = "center",
buttons = { buttons = {

@ -13,14 +13,14 @@ local _ = require("gettext")
local DropBoxApi = { local DropBoxApi = {
} }
local API_TOKEN = "https://api.dropbox.com/oauth2/token" local API_TOKEN = "https://api.dropbox.com/oauth2/token"
local API_URL_INFO = "https://api.dropboxapi.com/2/users/get_current_account" local API_URL_INFO = "https://api.dropboxapi.com/2/users/get_current_account"
local API_GET_SPACE_USAGE = "https://api.dropboxapi.com/2/users/get_space_usage" local API_GET_SPACE_USAGE = "https://api.dropboxapi.com/2/users/get_space_usage"
local API_LIST_FOLDER = "https://api.dropboxapi.com/2/files/list_folder" 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_UPLOAD_FILE = "https://content.dropboxapi.com/2/files/upload"
local API_CREATE_FOLDER = "https://api.dropboxapi.com/2/files/create_folder_v2"
local API_LIST_ADD_FOLDER = "https://api.dropboxapi.com/2/files/list_folder/continue" local API_LIST_ADD_FOLDER = "https://api.dropboxapi.com/2/files/list_folder/continue"
local API_CREATE_FOLDER = "https://api.dropboxapi.com/2/files/create_folder_v2"
local API_DOWNLOAD_FILE = "https://content.dropboxapi.com/2/files/download"
local API_UPLOAD_FILE = "https://content.dropboxapi.com/2/files/upload"
function DropBoxApi:getAccessToken(refresh_token, app_key_colon_secret) function DropBoxApi:getAccessToken(refresh_token, app_key_colon_secret)
local sink = {} local sink = {}
@ -29,24 +29,23 @@ function DropBoxApi:getAccessToken(refresh_token, app_key_colon_secret)
url = API_TOKEN, url = API_TOKEN,
method = "POST", method = "POST",
headers = { headers = {
["Authorization"] = "Basic " .. require("ffi/sha2").bin_to_base64(app_key_colon_secret), ["Authorization"] = "Basic " .. require("ffi/sha2").bin_to_base64(app_key_colon_secret),
["Content-Type"] = "application/x-www-form-urlencoded", ["Content-Type"] = "application/x-www-form-urlencoded",
["Content-Length"] = string.len(data), ["Content-Length"] = string.len(data),
}, },
source = ltn12.source.string(data), source = ltn12.source.string(data),
sink = ltn12.sink.table(sink), sink = ltn12.sink.table(sink),
} }
socketutil:set_timeout() socketutil:set_timeout()
local code = socket.skip(1, http.request(request)) local code, _, status = socket.skip(1, http.request(request))
socketutil:reset_timeout() socketutil:reset_timeout()
if code == 200 then local result_response = table.concat(sink)
local headers = table.concat(sink) if code == 200 and result_response ~= "" then
if headers ~= "" then local _, result = pcall(JSON.decode, result_response)
local _, result = pcall(JSON.decode, headers) return result["access_token"]
return result["access_token"]
end
end end
logger.info("Dropbox: cannot get access token") logger.warn("DropBoxApi: cannot get access token:", status or code)
logger.warn("DropBoxApi: error:", result_response)
end end
function DropBoxApi:fetchInfo(token, space_usage) function DropBoxApi:fetchInfo(token, space_usage)
@ -61,16 +60,15 @@ function DropBoxApi:fetchInfo(token, space_usage)
sink = ltn12.sink.table(sink), sink = ltn12.sink.table(sink),
} }
socketutil:set_timeout() socketutil:set_timeout()
local code = socket.skip(1, http.request(request)) local code, _, status = socket.skip(1, http.request(request))
socketutil:reset_timeout() socketutil:reset_timeout()
if code == 200 then local result_response = table.concat(sink)
local headers = table.concat(sink) if code == 200 and result_response ~= "" then
if headers ~= "" then local _, result = pcall(JSON.decode, result_response)
local _, result = pcall(JSON.decode, headers) return result
return result
end
end end
logger.info("Dropbox: cannot get account info") logger.warn("DropBoxApi: cannot get account info:", status or code)
logger.warn("DropBoxApi: error:", result_response)
end end
function DropBoxApi:fetchListFolders(path, token) function DropBoxApi:fetchListFolders(path, token)
@ -78,7 +76,6 @@ function DropBoxApi:fetchListFolders(path, token)
local data = "{\"path\": \"" .. path .. "\",\"recursive\": false,\"include_media_info\": false,".. local data = "{\"path\": \"" .. path .. "\",\"recursive\": false,\"include_media_info\": false,"..
"\"include_deleted\": false,\"include_has_explicit_shared_members\": false}" "\"include_deleted\": false,\"include_has_explicit_shared_members\": false}"
local sink = {} local sink = {}
socketutil:set_timeout()
local request = { local request = {
url = API_LIST_FOLDER, url = API_LIST_FOLDER,
method = "POST", method = "POST",
@ -90,28 +87,23 @@ function DropBoxApi:fetchListFolders(path, token)
source = ltn12.source.string(data), source = ltn12.source.string(data),
sink = ltn12.sink.table(sink), sink = ltn12.sink.table(sink),
} }
local headers_request = socket.skip(1, http.request(request)) socketutil:set_timeout()
local code, _, status = socket.skip(1, http.request(request))
socketutil:reset_timeout() socketutil:reset_timeout()
if headers_request == nil then
return nil
end
local result_response = table.concat(sink) local result_response = table.concat(sink)
if result_response ~= "" then if code == 200 and result_response ~= "" then
local ret, result = pcall(JSON.decode, result_response) local ret, result = pcall(JSON.decode, result_response)
if ret then if ret then
-- Check if more results, and then get them -- Check if more results, and then get them
if result.has_more then if result.has_more then
logger.dbg("Found additional files") logger.dbg("DropBoxApi: found additional files")
result = self:fetchAdditionalFolders(result, token) result = self:fetchAdditionalFolders(result, token)
end end
return result return result
else
return nil
end end
else
return nil
end end
logger.warn("DropBoxApi: cannot get folder content:", status or code)
logger.warn("DropBoxApi: error:", result_response)
end end
function DropBoxApi:downloadFile(path, token, local_path) function DropBoxApi:downloadFile(path, token, local_path)
@ -128,14 +120,16 @@ function DropBoxApi:downloadFile(path, token, local_path)
}) })
socketutil:reset_timeout() socketutil:reset_timeout()
if code ~= 200 then if code ~= 200 then
logger.warn("DropBoxApi: Download failure:", status or code or "network unreachable") logger.warn("DropBoxApi: cannot download file:", status or code)
end end
return code, (headers or {}).etag return code, (headers or {}).etag
end end
function DropBoxApi:uploadFile(path, token, file_path, etag, overwrite) function DropBoxApi:uploadFile(path, token, file_path, etag, overwrite)
local data = "{\"path\": \"" .. path .. "/" .. BaseUtil.basename(file_path) .. local data = "{\"path\": \"" .. path .. "/" .. BaseUtil.basename(file_path) ..
"\",\"mode\":" .. (overwrite and "\"overwrite\"" or "\"add\"") .. ",\"autorename\": " .. (overwrite and "false" or "true") .. ",\"mute\": false,\"strict_conflict\": false}" "\",\"mode\":" .. (overwrite and "\"overwrite\"" or "\"add\"") ..
",\"autorename\": " .. (overwrite and "false" or "true") ..
",\"mute\": false,\"strict_conflict\": false}"
socketutil:set_timeout(socketutil.FILE_BLOCK_TIMEOUT, socketutil.FILE_TOTAL_TIMEOUT) socketutil:set_timeout(socketutil.FILE_BLOCK_TIMEOUT, socketutil.FILE_TOTAL_TIMEOUT)
local code, _, status = socket.skip(1, http.request{ local code, _, status = socket.skip(1, http.request{
url = API_UPLOAD_FILE, url = API_UPLOAD_FILE,
@ -143,15 +137,15 @@ function DropBoxApi:uploadFile(path, token, file_path, etag, overwrite)
headers = { headers = {
["Authorization"] = "Bearer ".. token, ["Authorization"] = "Bearer ".. token,
["Dropbox-API-Arg"] = data, ["Dropbox-API-Arg"] = data,
["Content-Type"] = "application/octet-stream", ["Content-Type"] = "application/octet-stream",
["Content-Length"] = lfs.attributes(file_path, "size"), ["Content-Length"] = lfs.attributes(file_path, "size"),
["If-Match"] = etag, ["If-Match"] = etag,
}, },
source = ltn12.source.file(io.open(file_path, "r")), source = ltn12.source.file(io.open(file_path, "r")),
}) })
socketutil:reset_timeout() socketutil:reset_timeout()
if code ~= 200 then if code ~= 200 then
logger.warn("DropBoxApi: Upload failure:", status or code or "network unreachable") logger.warn("DropBoxApi: cannot upload file:", status or code)
end end
return code return code
end end
@ -163,15 +157,15 @@ function DropBoxApi:createFolder(path, token, folder_name)
url = API_CREATE_FOLDER, url = API_CREATE_FOLDER,
method = "POST", method = "POST",
headers = { headers = {
["Authorization"] = "Bearer ".. token, ["Authorization"] = "Bearer ".. token,
["Content-Type"] = "application/json", ["Content-Type"] = "application/json",
["Content-Length"] = #data, ["Content-Length"] = #data,
}, },
source = ltn12.source.string(data), source = ltn12.source.string(data),
}) })
socketutil:reset_timeout() socketutil:reset_timeout()
if code ~= 200 then if code ~= 200 then
logger.warn("DropBoxApi: Folder creation failure:", status or code or "network unreachable") logger.warn("DropBoxApi: cannot create folder:", status or code)
end end
return code return code
end end
@ -295,4 +289,5 @@ function DropBoxApi:fetchAdditionalFolders(response, token)
return out return out
end end
return DropBoxApi return DropBoxApi

Loading…
Cancel
Save