From ef787f9dedd4ebc88846fb17f319f319da154260 Mon Sep 17 00:00:00 2001 From: Volterxien Date: Tue, 10 Jun 2025 21:17:29 -0400 Subject: [PATCH] added force sync download --- plugins/opds.koplugin/main.lua | 25 +++++++++++++++-------- plugins/opds.koplugin/opdsbrowser.lua | 29 +++++++++------------------ 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/plugins/opds.koplugin/main.lua b/plugins/opds.koplugin/main.lua index 850ed8dc3..e713a65af 100644 --- a/plugins/opds.koplugin/main.lua +++ b/plugins/opds.koplugin/main.lua @@ -70,14 +70,14 @@ function OPDS:addToMainMenu(menu_items) text = _("OPDS"), sub_item_table = { { - text = _("OPDS catalog"), + text = _("Catalogs"), keep_menu_open = true, callback = function() self:onShowOPDSCatalog() end, }, { - text = _("Automatic OPDS download"), + text = _("Sync"), keep_menu_open = true, sub_item_table = self:getOPDSDownloadMenu(), }, @@ -100,7 +100,19 @@ function OPDS:getOPDSDownloadMenu() { text = _("Perform sync"), callback = function() - self:checkSyncDownload(self.servers) + self:checkSyncDownload(false) + end, + }, + { + text = _("Force sync"), + callback = function() + UIManager:show(ConfirmBox: new{ + text = "Are you sure you want to force sync? This may overwrite existing data.", + ok_text = "Force sync", + ok_callback = function() + self:checkSyncDownload(true) + end + }) end, }, { @@ -112,22 +124,19 @@ function OPDS:getOPDSDownloadMenu() } end - -function OPDS:checkSyncDownload() +function OPDS:checkSyncDownload(force) for _, item in ipairs(self.servers) do if item.sync then - local last_download = OPDSBrowser:syncDownload(item) + local last_download = OPDSBrowser:syncDownload(item, force) print(last_download) if last_download then logger.dbg("Updating opds last download for server " .. item.title) self:appendFieldToCatalog(item, "last_download", last_download) - else end end end end - function OPDS:appendFieldToCatalog(item, new_name, new_value) item[new_name] = new_value self.updated = true diff --git a/plugins/opds.koplugin/opdsbrowser.lua b/plugins/opds.koplugin/opdsbrowser.lua index 46381e5ef..07cd6baf9 100644 --- a/plugins/opds.koplugin/opdsbrowser.lua +++ b/plugins/opds.koplugin/opdsbrowser.lua @@ -661,7 +661,7 @@ function OPDSBrowser:showDownloads(item) callback = function() UIManager:close(self.download_dialog) local local_path = self:getLocalDownloadPath(filename, filetype, acquisition.href, false) - self:checkDownloadFile(local_path, acquisition.href, self.root_catalog_username, self.root_catalog_password, self.file_downloaded_callback) + self:checkDownloadFile(local_path, acquisition.href, self.root_catalog_username, self.root_catalog_password, self.file_downloaded_callback, false, false) end, hold_callback = function() UIManager:close(self.download_dialog) @@ -800,7 +800,7 @@ function OPDSBrowser:getLocalDownloadPath(filename, filetype, remote_url, sync) end -- Downloads a book (with "File already exists" dialog) -function OPDSBrowser:checkDownloadFile(local_path, remote_url, username, password, caller_callback, sync) +function OPDSBrowser:checkDownloadFile(local_path, remote_url, username, password, caller_callback, sync, force_sync) local function download() UIManager:scheduleIn(1, function() self:downloadFile(local_path, remote_url, username, password, caller_callback) @@ -819,10 +819,12 @@ function OPDSBrowser:checkDownloadFile(local_path, remote_url, username, passwor download() end, }) - else + elseif not force_sync then local file_name, file_extension = util.splitFileNameSuffix(local_path) local_path = file_name .. "_1." .. file_extension download() + else + download() end else download() @@ -1050,7 +1052,7 @@ function OPDSBrowser:showDownloadListItemDialog(item) self._manager.file_downloaded_callback(local_path) end NetworkMgr:runWhenConnected(function() - self._manager:checkDownloadFile(dl_item.file, dl_item.url, dl_item.username, dl_item.password, file_downloaded_callback, false) + self._manager:checkDownloadFile(dl_item.file, dl_item.url, dl_item.username, dl_item.password, file_downloaded_callback, false, false) end) end, }, @@ -1158,27 +1160,16 @@ function OPDSBrowser:downloadDownloadList() end end -function OPDSBrowser:syncDownload(server) +function OPDSBrowser:syncDownload(server, force) local table = {} local new_last_download = nil - local function dump(o) - if type(o) == 'table' then - local s = '{ ' - for k,v in pairs(o) do - if type(k) ~= 'number' then k = '"'..k..'"' end - s = s .. '['..k..'] = ' .. dump(v) .. ',' - end - return s .. '} ' - else - return tostring(o) - end - end + table = self:getSyncDownloadList(server) for i, entry in ipairs(table) do -- First entry in table is the newest -- If already downloaded, return for _, link in ipairs(entry.acquisitions) do - if link.href == server.last_download then + if link.href == server.last_download and not force then return new_last_download end if i == 1 then @@ -1194,7 +1185,7 @@ function OPDSBrowser:syncDownload(server) logger.dbg("Filetype for sync download is", filetype) local download_path = self:getLocalDownloadPath(entry.title, filetype, link.href, true) NetworkMgr:runWhenConnected(function() - OPDSBrowser:checkDownloadFile(download_path, link.href, server.username, server.password, nil, true) + OPDSBrowser:checkDownloadFile(download_path, link.href, server.username, server.password, nil, true, force) end) end end