From e8cd2ba4981bbc84350cae21b8461eba60dc2ac8 Mon Sep 17 00:00:00 2001 From: weijiuqiao <59040746+weijiuqiao@users.noreply.github.com> Date: Sat, 29 Oct 2022 23:45:52 +0800 Subject: [PATCH] WebDAV: fix bugs when start folder starts with "/" (#9688) --- frontend/apps/cloudstorage/webdav.lua | 2 +- frontend/apps/cloudstorage/webdavapi.lua | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/frontend/apps/cloudstorage/webdav.lua b/frontend/apps/cloudstorage/webdav.lua index 774dc6dc4..a613e649a 100644 --- a/frontend/apps/cloudstorage/webdav.lua +++ b/frontend/apps/cloudstorage/webdav.lua @@ -17,7 +17,7 @@ function WebDav:run(address, user, pass, path) end function WebDav:downloadFile(item, address, username, password, local_path, callback_close) - local code_response = WebDavApi:downloadFile(address .. WebDavApi:urlEncode( item.url ), username, password, local_path) + local code_response = WebDavApi:downloadFile(WebDavApi:getJoinedPath(address, item.url), username, password, local_path) if code_response == 200 then local __, filename = util.splitFilePathName(local_path) if G_reader_settings:isTrue("show_unsupported") and not DocumentRegistry:hasProvider(filename) then diff --git a/frontend/apps/cloudstorage/webdavapi.lua b/frontend/apps/cloudstorage/webdavapi.lua index 92381afbc..1d7570a91 100644 --- a/frontend/apps/cloudstorage/webdavapi.lua +++ b/frontend/apps/cloudstorage/webdavapi.lua @@ -11,6 +11,13 @@ local logger = require("logger") local WebDavApi = { } +function WebDavApi:getJoinedPath( address, path ) + local path_encoded = self:urlEncode( path ) or "" + local address_strip = address:sub(-1) == "/" and address:sub(1, -2) or address + local path_strip = path_encoded:sub(1, 1) == "/" and path_encoded:sub(2) or path_encoded + return address_strip .. "/" .. path_strip +end + function WebDavApi:isCurrentDirectory( current_item, address, path ) local is_home, is_parent local home_path @@ -32,6 +39,7 @@ function WebDavApi:isCurrentDirectory( current_item, address, path ) is_home = true else local temp_path = string.sub( item, string.len(home_path) + 1 ) + if string.sub( path, -1 ) == "/" then path = string.sub( path, 1, -2 ) end if temp_path == path then is_parent = true end @@ -61,10 +69,10 @@ function WebDavApi:listFolder(address, user, pass, folder_path) if string.sub( address, -1 ) == "/" then has_trailing_slash = true end if path == nil or path == "/" then path = "" - elseif string.sub( path, 1, 2 ) == "/" then + elseif string.sub( path, 1, 1 ) == "/" then if has_trailing_slash then -- too many slashes, remove one - path = string.sub( path, 1 ) + path = string.sub( path, 2 ) end has_leading_slash = true end @@ -72,7 +80,7 @@ function WebDavApi:listFolder(address, user, pass, folder_path) address = address .. "/" end local webdav_url = address .. path - if not has_trailing_slash then + if not string.sub(webdav_url, -1) == "/" then webdav_url = webdav_url .. "/" end @@ -116,7 +124,7 @@ function WebDavApi:listFolder(address, user, pass, folder_path) if string.sub( item_fullpath, -1 ) == "/" then item_fullpath = string.sub( item_fullpath, 1, -2 ) end - local is_current_dir = self:isCurrentDirectory( item_fullpath, address, path ) + local is_current_dir = self:isCurrentDirectory( util.urlDecode(item_fullpath), address, folder_path ) local item_name = util.urlDecode( FFIUtil.basename( item_fullpath ) ) item_name = util.htmlEntitiesToUtf8(item_name)