diff --git a/plugins/opds.koplugin/opdsbrowser.lua b/plugins/opds.koplugin/opdsbrowser.lua index eb40c8213..1abfaa329 100644 --- a/plugins/opds.koplugin/opdsbrowser.lua +++ b/plugins/opds.koplugin/opdsbrowser.lua @@ -647,16 +647,29 @@ function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) and link.rel ~= "subsection" then -- Check for the presence of the pdf suffix and add it -- if it's missing. - local href = link.href + local original_href = link.href + local parsed = url.parse(original_href) + if not parsed then parsed = { path = original_href } end + local path = parsed.path or "" -- Calibre web OPDS download links end with "//" - if not util.stringEndsWith(href, "/pdf/") then - if util.getFileNameSuffix(href) ~= "pdf" then - href = href .. ".pdf" + if not util.stringEndsWith(path, "/pdf/") then + local appended = false + if util.getFileNameSuffix(path) ~= "pdf" then + if path == "" then + path = ".pdf" + else + path = path .. ".pdf" + end + appended = true + end + if appended then + parsed.path = path + local new_href = url.build(parsed) + table.insert(item.acquisitions, { + type = link.title, + href = build_href(new_href), + }) end - table.insert(item.acquisitions, { - type = link.title, - href = build_href(href), - }) end end end diff --git a/spec/unit/opds_spec.lua b/spec/unit/opds_spec.lua index f2c56239c..3387332b4 100644 --- a/spec/unit/opds_spec.lua +++ b/spec/unit/opds_spec.lua @@ -290,6 +290,22 @@ local facet_sample = [[ ]] +local pdf_query_sample = [[ + + + tag:root:pdfquery + PDF Query Test + 2025-09-11T00:00:00Z + + Sample PDF With Query + urn:pdf:with:query + 2025-09-11T00:00:00Z + A PDF that already has .pdf before a query parameter. + + + +]] + describe("OPDS module", function() local socketutil local OPDSParser, OPDSBrowser @@ -427,5 +443,23 @@ describe("OPDS module", function() assert.truthy(item_table) assert.are_not.same(item_table[1].image, "http://flibusta.is/opds/author/75357") end) + + it("should not append .pdf after query parameters or duplicate acquisition entries #14300 #internet", function() + local catalog = OPDSParser:parse(pdf_query_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) + -- Only one acquisition entry should be present (generic provider entry). + assert.are.same(1, #acquisitions) + local href = acquisitions[1].href + assert.truthy(href) + -- It must contain the original query parameter unchanged. + assert(href:match("file%.pdf%?opds$")) + -- And must NOT have an extra .pdf appended after the query string. + assert(not href:match("opds%.pdf$")) + end) end) end)