From 8faf6b8ef12d4b709fc3185fd53e404ccc1ca2dc Mon Sep 17 00:00:00 2001 From: Brendan LeFebvre Date: Mon, 10 Aug 2026 10:26:21 -0400 Subject: [PATCH] OPDS: fix duplicate PDF download button (#15850) The arxiv .pdf-suffix workaround ran unconditionally after the rel handling, so an acquisition link with type="application/pdf" and no .pdf suffix was added twice. Only apply it when the link produced no acquisition above. Fixes #15849 --- plugins/opds.koplugin/opdsbrowser.lua | 12 ++++--- spec/unit/opds_spec.lua | 45 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/plugins/opds.koplugin/opdsbrowser.lua b/plugins/opds.koplugin/opdsbrowser.lua index 9522faba3..f8a49e96c 100644 --- a/plugins/opds.koplugin/opdsbrowser.lua +++ b/plugins/opds.koplugin/opdsbrowser.lua @@ -523,9 +523,9 @@ function OPDSBrowser:getServerFileName(item_url, filetype) end if filename and filetype then + -- Add extension if missing or unusable. local current_suffix = util.getFileNameSuffix(filename) - -- Add extension if missing - if not current_suffix then + if not DocumentRegistry:hasProvider("dummy." .. current_suffix) then filename = filename .. "." .. filetype:lower() end end @@ -825,6 +825,7 @@ function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) -- a publication. Arxiv uses title. Specifically, it uses -- a title attribute that contains pdf. (title="pdf") if link.rel or link.title then + local acquisitions_nb = #item.acquisitions if link.rel == self.borrow_rel then table.insert(item.acquisitions, { type = "borrow", @@ -869,8 +870,11 @@ function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) end -- This statement grabs the catalog items that are -- indicated by title="pdf" or whose type is - -- "application/pdf" - if link.title == "pdf" or link.type == "application/pdf" + -- "application/pdf", and that have not already been + -- added as an acquisition above (which would result in + -- duplicate download buttons) + if #item.acquisitions == acquisitions_nb + and (link.title == "pdf" or link.type == "application/pdf") and link.rel ~= "subsection" then -- Check for the presence of the pdf suffix and add it -- if it's missing. diff --git a/spec/unit/opds_spec.lua b/spec/unit/opds_spec.lua index 3be87d2cf..bf1d90bd3 100644 --- a/spec/unit/opds_spec.lua +++ b/spec/unit/opds_spec.lua @@ -338,6 +338,22 @@ local pdf_query_sample = [[ ]] +local pdf_acquisition_sample = [[ + + + tag:root:pdfacquisition + PDF Acquisition Test + 2025-09-11T00:00:00Z + + Sample PDF Without Suffix + urn:pdf:no:suffix + 2025-09-11T00:00:00Z + A PDF whose download link carries no .pdf suffix. + + + +]] + describe("OPDS module", function() local socketutil local OPDSParser, OPDSBrowser @@ -555,5 +571,34 @@ describe("OPDS module", function() -- And must NOT have an extra .pdf appended after the query string. assert(not href:match("opds%.pdf$")) end) + + it("should not duplicate a PDF acquisition whose href has no .pdf suffix #internet", function() + local catalog = OPDSParser:parse(pdf_acquisition_sample) + local item_table = OPDSBrowser:genItemTableFromCatalog(catalog, "http://example.org/opds") + + assert.truthy(item_table) + assert.are.same(1, #item_table) + local acquisitions = item_table[1].acquisitions + assert.truthy(acquisitions) + -- The acquisition link is added once, by the rel handling above. + assert.are.same(1, #acquisitions) + assert.are.same("http://example.org/get/PDF/123/library", acquisitions[1].href) + end) + + it("should add the file extension to a server filename that lacks a usable one #internet", function() + local orig_fetchFeed = OPDSBrowser.fetchFeed + OPDSBrowser.fetchFeed = function() return nil end -- no headers: fall back to the URL + finally(function() OPDSBrowser.fetchFeed = orig_fetchFeed end) + + -- No suffix at all. + assert.are.same("library.pdf", + OPDSBrowser:getServerFileName("http://example.org/get/PDF/123/library", "pdf")) + -- A dotted tail that is not a known extension is not a suffix either. + assert.are.same("2509.07924v1.pdf", + OPDSBrowser:getServerFileName("http://arxiv.org/pdf/2509.07924v1", "pdf")) + -- An existing, usable extension is left alone. + assert.are.same("file.pdf", + OPDSBrowser:getServerFileName("http://example.org/books/file.pdf?opds", "pdf")) + end) end) end)