DropBox: unify error messages (#10666)

reviewable/pr10674/r1
hius07 10 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 ButtonDialog = require("ui/widget/buttondialog")
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
local CheckButton = require("ui/widget/checkbutton")
local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage")
@ -19,7 +18,6 @@ local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local _ = require("gettext")
local N_ = _.ngettext
local Screen = require("device").screen
local T = require("ffi/util").template
local CloudStorage = Menu:extend{
@ -46,8 +44,6 @@ function CloudStorage:init()
else
self.item_table = self:genItemTableFromRoot()
end
self.width = Screen:getWidth()
self.height = Screen:getHeight()
self.title_bar_left_icon = "plus"
self.onLeftButtonTap = function() -- add new cloud storage
self:selectCloudType()
@ -123,7 +119,7 @@ function CloudStorage:selectCloudType()
},
})
end
self.cloud_dialog = ButtonDialogTitle:new{
self.cloud_dialog = ButtonDialog:new{
title = _("Add new cloud storage"),
title_align = "center",
buttons = buttons,
@ -231,7 +227,7 @@ function CloudStorage:downloadFile(item)
})
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"),
filename_orig, filename, BD.dirpath(path))
end
@ -247,7 +243,6 @@ function CloudStorage:downloadFile(item)
text = _("Choose folder"),
callback = function()
require("ui/downloadmgr"):new{
show_hidden = G_reader_settings:readSetting("show_hidden"),
onConfirm = function(path)
self.cs_settings:saveSetting("download_dir", path)
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),
buttons = buttons,
}
@ -352,7 +347,7 @@ function CloudStorage:onMenuHold(item)
local title = T(_("Choose this folder?\n\n%1"), BD.dirpath(item.url))
local onConfirm = self.onConfirm
local button_dialog
button_dialog = ButtonDialogTitle:new{
button_dialog = ButtonDialog:new{
title = title,
buttons = {
{
@ -532,7 +527,7 @@ function CloudStorage:synchronizeSettings(item)
local syn_dialog
local dropbox_sync_folder = item.sync_source_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_align = "center",
buttons = {

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

Loading…
Cancel
Save