Cloudsync: dropbox token re-generation (#9862)

reviewable/pr9905/r1
weijiuqiao 1 year ago committed by GitHub
parent a6f0c3a31c
commit 49d8ec2f4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,

Loading…
Cancel
Save