From 73b4dd4bb0232280e61d8a57d25b16f7bde35c50 Mon Sep 17 00:00:00 2001 From: Zeedif Date: Wed, 20 Aug 2025 08:52:32 -0600 Subject: [PATCH] fix(opds): ensure correct file extension on server-provided filenames (#14199) The `getServerFileName` function could result in a filename without an extension in two main scenarios. First, a server might provide a `content-disposition` header where the `filename` attribute is present but lacks a file extension. Second, the fallback logic of extracting the name from the URL path often fails for dynamic endpoints (e.g., `/download?id=123`) which do not contain a filename. This change improves the logic by checking if the extracted filename has an extension. If one is missing, the expected `filetype` (which is already known at this point) is appended. This ensures files are always saved with the correct extension (e.g., `.cbz`, `.epub`). --- plugins/opds.koplugin/opdsbrowser.lua | 39 +++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/plugins/opds.koplugin/opdsbrowser.lua b/plugins/opds.koplugin/opdsbrowser.lua index 46882a5cd..4eb94074c 100644 --- a/plugins/opds.koplugin/opdsbrowser.lua +++ b/plugins/opds.koplugin/opdsbrowser.lua @@ -461,19 +461,42 @@ function OPDSBrowser:parseFeed(item_url) end end -function OPDSBrowser:getServerFileName(item_url) +function OPDSBrowser:getServerFileName(item_url, filetype) local headers = self:fetchFeed(item_url, true) + local filename + if headers then logger.dbg("OPDSBrowser: server file headers", socketutil.redact_headers(headers)) - local header = headers["content-disposition"] - if header then - return header:match('filename="*([^"]+)"*') + local disposition = headers["content-disposition"] + if disposition then + -- Try to get filename inside quotes (can contain spaces) + filename = disposition:match('filename="([^"]+)"') + if not filename then + -- Fallback: try filename without quotes, until end or semicolon + filename = disposition:match('filename=([^;]+)') + end end - header = headers["location"] - if header then - return header:gsub(".*/", "") + + -- If not found, try from redirect URL (location) + if not filename and headers["location"] then + filename = headers["location"]:gsub(".*/", "") end end + + -- If still no filename, extract from original URL (remove path and query params) + if not filename then + filename = item_url:gsub(".*/", ""):gsub("?.*", "") + end + + if filename and filetype then + local current_suffix = util.getFileNameSuffix(filename) + -- Add extension if missing + if not current_suffix then + filename = filename .. "." .. filetype:lower() + end + end + + return filename end -- Generates link to search in catalog @@ -958,7 +981,7 @@ end function OPDSBrowser:getLocalDownloadPath(filename, filetype, remote_url) local download_dir = self:getCurrentDownloadDir() - filename = filename and filename .. "." .. filetype:lower() or self:getServerFileName(remote_url) + filename = filename and filename .. "." .. filetype:lower() or self:getServerFileName(remote_url, filetype) filename = util.getSafeFilename(filename, download_dir) filename = (download_dir ~= "/" and download_dir or "") .. '/' .. filename return util.fixUtf8(filename, "_")