mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Cloudsync: dropbox token re-generation (#9862)
This commit is contained in:
@@ -3,6 +3,7 @@ local Font = require("ui/font")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local LuaSettings = require("luasettings")
|
||||
local Menu = require("ui/widget/menu")
|
||||
local NetworkMgr = require("ui/network/manager")
|
||||
local Notification = require("ui/widget/notification")
|
||||
local Screen = require("device").screen
|
||||
local UIManager = require("ui/uimanager")
|
||||
@@ -87,7 +88,11 @@ function SyncService.getReadablePath(server)
|
||||
local url = util.stringStartsWith(server.url, "/") and server.url:sub(2) or server.url
|
||||
url = util.urlDecode(url) or url
|
||||
url = util.stringEndsWith(url, "/") and url or url .. "/"
|
||||
url = (server.address:sub(-1) == "/" and server.address or server.address .. "/") .. url
|
||||
if server.type == "dropbox" then
|
||||
url = "/" .. url
|
||||
elseif server.type == "webdav" then
|
||||
url = (server.address:sub(-1) == "/" and server.address or server.address .. "/") .. url
|
||||
end
|
||||
if url:sub(-2) == "//" then url = url:sub(1, -2) end
|
||||
return url
|
||||
end
|
||||
@@ -113,6 +118,9 @@ end
|
||||
-- and renamed to replace the old cached file (thus the naming). The cached file stays (in the same folder) till being replaced
|
||||
-- in the next round.
|
||||
function SyncService.sync(server, file_path, sync_cb, is_silent)
|
||||
if NetworkMgr:willRerunWhenOnline(function() SyncService.sync(server, file_path, sync_cb, is_silent) end) then
|
||||
return
|
||||
end
|
||||
local file_name = ffiutil.basename(file_path)
|
||||
local income_file_path = file_path .. ".temp" -- file downloaded from server
|
||||
local cached_file_path = file_path .. ".sync" -- file uploaded to server last time
|
||||
@@ -132,11 +140,15 @@ function SyncService.sync(server, file_path, sync_cb, is_silent)
|
||||
local code_response = 412 -- If-Match header failed
|
||||
local etag
|
||||
local api = server.type == "dropbox" and require("apps/cloudstorage/dropboxapi") or require("apps/cloudstorage/webdavapi")
|
||||
local token = server.password
|
||||
if server.type == "dropbox" and not (server.address == nil or server.address == "") then
|
||||
token = api:getAccessToken(server.password, server.address)
|
||||
end
|
||||
while code_response == 412 do
|
||||
os.remove(income_file_path)
|
||||
if server.type == "dropbox" then
|
||||
local url_base = server.url:sub(-1) == "/" and server.url or server.url.."/"
|
||||
code_response, etag = api:downloadFile(url_base..file_name, server.password, income_file_path)
|
||||
code_response, etag = api:downloadFile(url_base..file_name, token, income_file_path)
|
||||
elseif server.type == "webdav" then
|
||||
local path = api:getJoinedPath(server.address, server.url)
|
||||
path = api:getJoinedPath(path, file_name)
|
||||
@@ -155,7 +167,7 @@ function SyncService.sync(server, file_path, sync_cb, is_silent)
|
||||
end
|
||||
if server.type == "dropbox" then
|
||||
local url_base = server.url == "/" and "" or server.url
|
||||
code_response = api:uploadFile(url_base, server.password, file_path, etag, true)
|
||||
code_response = api:uploadFile(url_base, token, file_path, etag, true)
|
||||
elseif server.type == "webdav" then
|
||||
local path = api:getJoinedPath(server.address, server.url)
|
||||
path = api:getJoinedPath(path, file_name)
|
||||
|
||||
@@ -1130,7 +1130,7 @@ The max value ensures a page you stay on for a long time (because you fell aslee
|
||||
SyncService.sync(self.settings.sync_server, db_location, self.onSync )
|
||||
end,
|
||||
enabled_func = function()
|
||||
return self.settings.sync_server ~= nil and self.settings.is_enabled and require("ui/network/manager"):isWifiOn()
|
||||
return self.settings.sync_server ~= nil and self.settings.is_enabled
|
||||
end,
|
||||
keep_menu_open = true,
|
||||
separator = true,
|
||||
|
||||
Reference in New Issue
Block a user