From 20813cab53349bf0c2876b138e37ceb29fb57d4d Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Wed, 13 Mar 2024 12:47:13 +0100 Subject: [PATCH] Wallabag: properly deal with mimetype actually being content-type (#11532) A typo snuck in #11492, which should've read `not type(article.mimetype) == "string" or type(article.mimetype) == "string" and not article.mimetype:find("^text/html")`. But in most cases the behavior would've been identically broken because of the same underlying issue: Wallabag mimetype is actually HTTP content-type. Fixes #11528. Also introduces a new setting associated with the behavior in case people have different preferences. --- plugins/wallabag.koplugin/main.lua | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/plugins/wallabag.koplugin/main.lua b/plugins/wallabag.koplugin/main.lua index 6c5c984f8..f79f5cb08 100644 --- a/plugins/wallabag.koplugin/main.lua +++ b/plugins/wallabag.koplugin/main.lua @@ -99,6 +99,7 @@ function Wallabag:init() self.articles_per_sync = self.wb_settings.data.wallabag.articles_per_sync end self.remove_finished_from_history = self.wb_settings.data.wallabag.remove_finished_from_history or false + self.download_original_document = self.wb_settings.data.wallabag.download_original_document self.download_queue = self.wb_settings.data.wallabag.download_queue or {} -- workaround for dateparser only available if newsdownloader is active @@ -340,6 +341,18 @@ function Wallabag:addToMainMenu(menu_items) end, separator = true, }, + { + text = _("Prefer original non-HTML document"), + keep_menu_open = true, + checked_func = function() + return self.download_original_document + end, + callback = function() + self.download_original_document = not self.download_original_document + self:saveSettings() + end, + separator = true, + }, { text = _("Help"), keep_menu_open = true, @@ -551,17 +564,21 @@ function Wallabag:download(article) local title = util.getSafeFilename(article.title, self.directory, 230, 0) local file_ext = ".epub" local item_url = "/api/entries/" .. article.id .. "/export.epub" + -- The mimetype is actually an HTTP Content-Type, so it can include a semicolon with stuff after it. + -- Just in case we also trim it, though that shouldn't be necessary. + -- A function represents `null` in our JSON.decode, because `nil` would just disappear. + -- We can simplify that to not a string. + local mimetype = type(article.mimetype) == string and util.trim(article.mimetype:match("^[^;]*")) or nil -- If the article links to a supported file, we will download it directly. -- All webpages are HTML. Ignore them since we want the Wallabag EPUB instead! - if type(article.mimetype) == "string" and article.mimetype:find("^text/html") then - logger.dbg("Wallabag: ignoring EPUB in favor of mimetype: ", article.mimetype) - if DocumentRegistry:hasProvider(nil, article.mimetype) then + if self.download_original_document then + if mimetype ~= "text/html" and DocumentRegistry:hasProvider(nil, mimetype) then + logger.dbg("Wallabag: ignoring EPUB in favor of mimetype: ", mimetype) file_ext = "."..DocumentRegistry:mimeToExt(article.mimetype) item_url = article.url - -- A function represents `null` in our JSON.decode, because `nil` would just disappear. - -- In that case, fall back to the file extension. - elseif type(article.mimetype) == "function" and DocumentRegistry:hasProvider(article.url) then + elseif mimetype == nil and DocumentRegistry:hasProvider(article.url) then + logger.dbg("Wallabag: ignoring EPUB in favor of original: ", article.url) file_ext = "."..util.getFileNameSuffix(article.url) item_url = article.url end @@ -1140,6 +1157,7 @@ function Wallabag:saveSettings() send_review_as_tags = self.send_review_as_tags, remove_finished_from_history = self.remove_finished_from_history, remove_read_from_history = self.remove_read_from_history, + download_original_document = self.download_original_document, download_queue = self.download_queue, } self.wb_settings:saveSetting("wallabag", tempsettings)