Fix crash when downloading books with unknown characters in title (#2628)

pull/2650/head
Robert 7 years ago committed by Qingping Hou
parent d206abc724
commit dccbf4cf96

@ -13,7 +13,7 @@ local url = require('socket.url')
local T = require("ffi/util").template local T = require("ffi/util").template
local Cache = require("cache") local Cache = require("cache")
local logger = require("logger") local logger = require("logger")
local _ = require("gettext") local gettext = require("gettext")
local socket = require('socket') local socket = require('socket')
local http = require('socket.http') local http = require('socket.http')
@ -35,7 +35,7 @@ local CatalogCache = Cache:new{
local OPDSBrowser = Menu:extend{ local OPDSBrowser = Menu:extend{
opds_servers = {}, opds_servers = {},
calibre_name = _("Local calibre catalog"), calibre_name = gettext("Local calibre catalog"),
catalog_type = "application/atom%+xml", catalog_type = "application/atom%+xml",
search_type = "application/opensearchdescription%+xml", search_type = "application/opensearchdescription%+xml",
@ -111,28 +111,28 @@ end
function OPDSBrowser:addNewCatalog() function OPDSBrowser:addNewCatalog()
self.add_server_dialog = MultiInputDialog:new{ self.add_server_dialog = MultiInputDialog:new{
title = _("Add OPDS catalog"), title = gettext("Add OPDS catalog"),
fields = { fields = {
{ {
text = "", text = "",
hint = _("Catalog name"), hint = gettext("Catalog name"),
}, },
{ {
text = "", text = "",
hint = _("Catalog URL"), hint = gettext("Catalog URL"),
}, },
}, },
buttons = { buttons = {
{ {
{ {
text = _("Cancel"), text = gettext("Cancel"),
callback = function() callback = function()
self.add_server_dialog:onClose() self.add_server_dialog:onClose()
UIManager:close(self.add_server_dialog) UIManager:close(self.add_server_dialog)
end end
}, },
{ {
text = _("Add"), text = gettext("Add"),
callback = function() callback = function()
self.add_server_dialog:onClose() self.add_server_dialog:onClose()
UIManager:close(self.add_server_dialog) UIManager:close(self.add_server_dialog)
@ -151,29 +151,29 @@ end
function OPDSBrowser:editCalibreServer() function OPDSBrowser:editCalibreServer()
local calibre = G_reader_settings:readSetting("calibre_opds") or {} local calibre = G_reader_settings:readSetting("calibre_opds") or {}
self.add_server_dialog = MultiInputDialog:new{ self.add_server_dialog = MultiInputDialog:new{
title = _("Edit local calibre host and port"), title = gettext("Edit local calibre host and port"),
fields = { fields = {
{ {
-- TODO: get IP address of current device -- TODO: get IP address of current device
text = calibre.host or "192.168.1.1", text = calibre.host or "192.168.1.1",
hint = _("calibre host"), hint = gettext("calibre host"),
}, },
{ {
text = calibre.port and tostring(calibre.port) or "8080", text = calibre.port and tostring(calibre.port) or "8080",
hint = _("calibre port"), hint = gettext("calibre port"),
}, },
}, },
buttons = { buttons = {
{ {
{ {
text = _("Cancel"), text = gettext("Cancel"),
callback = function() callback = function()
self.add_server_dialog:onClose() self.add_server_dialog:onClose()
UIManager:close(self.add_server_dialog) UIManager:close(self.add_server_dialog)
end end
}, },
{ {
text = _("Apply"), text = gettext("Apply"),
callback = function() callback = function()
self.add_server_dialog:onClose() self.add_server_dialog:onClose()
UIManager:close(self.add_server_dialog) UIManager:close(self.add_server_dialog)
@ -220,7 +220,7 @@ function OPDSBrowser:genItemTableFromRoot()
}) })
end end
table.insert(item_table, { table.insert(item_table, {
text = _("Add new OPDS catalog"), text = gettext("Add new OPDS catalog"),
callback = function() callback = function()
self:addNewCatalog() self:addNewCatalog()
end, end,
@ -288,19 +288,19 @@ end
function OPDSBrowser:fetchWithLogin(host, callback) function OPDSBrowser:fetchWithLogin(host, callback)
self.login_dialog = LoginDialog:new{ self.login_dialog = LoginDialog:new{
title = _("Login to OPDS server"), title = gettext("Login to OPDS server"),
username = "", username = "",
buttons = { buttons = {
{ {
{ {
text = _("Cancel"), text = gettext("Cancel"),
enabled = true, enabled = true,
callback = function() callback = function()
self:closeDialog() self:closeDialog()
end, end,
}, },
{ {
text = _("Login"), text = gettext("Login"),
enabled = true, enabled = true,
callback = function() callback = function()
local username, password = self:getCredential() local username, password = self:getCredential()
@ -361,7 +361,7 @@ function OPDSBrowser:getCatalog(feed_url)
logger.warn("cannot get catalog info from", feed_url, catalog) logger.warn("cannot get catalog info from", feed_url, catalog)
UIManager:show(InfoMessage:new{ UIManager:show(InfoMessage:new{
text = T( text = T(
_("Cannot get catalog info from %1"), gettext("Cannot get catalog info from %1"),
(feed_url or "") (feed_url or "")
), ),
}) })
@ -504,7 +504,7 @@ function OPDSBrowser:downloadFile(item, format, remote_url)
logger.dbg("downloading file", local_path, "from", remote_url) logger.dbg("downloading file", local_path, "from", remote_url)
local parsed = url.parse(remote_url) local parsed = url.parse(remote_url)
http.TIMEOUT, https.TIMEOUT = 10, 10 http.TIMEOUT, https.TIMEOUT = 20, 20
local httpRequest = parsed.scheme == 'http' and http.request or https.request local httpRequest = parsed.scheme == 'http' and http.request or https.request
local _, c, _ = httpRequest{ local _, c, _ = httpRequest{
url = remote_url, url = remote_url,
@ -519,7 +519,7 @@ function OPDSBrowser:downloadFile(item, format, remote_url)
end end
else else
UIManager:show(InfoMessage:new{ UIManager:show(InfoMessage:new{
text = _("Could not save file to:\n") .. local_path, text = gettext("Could not save file to:\n") .. local_path,
timeout = 3, timeout = 3,
}) })
end end
@ -527,7 +527,7 @@ end
function OPDSBrowser:createNewDownloadDialog(path, buttons) function OPDSBrowser:createNewDownloadDialog(path, buttons)
self.download_dialog = ButtonDialogTitle:new{ self.download_dialog = ButtonDialogTitle:new{
title = T(_("Download directory:\n%1\n\nDownload file type:"), path), title = T(gettext("Download directory:\n%1\n\nDownload file type:"), path),
buttons = buttons buttons = buttons
} }
end end
@ -554,7 +554,7 @@ function OPDSBrowser:showDownloads(item)
end) end)
UIManager:close(self.download_dialog) UIManager:close(self.download_dialog)
UIManager:show(InfoMessage:new{ UIManager:show(InfoMessage:new{
text = _("Downloading may take several minutes…"), text = gettext("Downloading may take several minutes…"),
timeout = 1, timeout = 1,
}) })
end end
@ -570,10 +570,10 @@ function OPDSBrowser:showDownloads(item)
-- set download directory button -- set download directory button
table.insert(buttons, { table.insert(buttons, {
{ {
text = _("Set download directory"), text = gettext("Set download directory"),
callback = function() callback = function()
require("ui/downloadmgr"):new{ require("ui/downloadmgr"):new{
title = _("Choose download directory"), title = gettext("Choose download directory"),
onConfirm = function(path) onConfirm = function(path)
logger.dbg("set download directory to", path) logger.dbg("set download directory to", path)
G_reader_settings:saveSetting("download_dir", path) G_reader_settings:saveSetting("download_dir", path)
@ -629,28 +629,28 @@ end
function OPDSBrowser:editOPDSServer(item) function OPDSBrowser:editOPDSServer(item)
logger.dbg("edit", item) logger.dbg("edit", item)
self.edit_server_dialog = MultiInputDialog:new{ self.edit_server_dialog = MultiInputDialog:new{
title = _("Edit OPDS catalog"), title = gettext("Edit OPDS catalog"),
fields = { fields = {
{ {
text = item.text or "", text = item.text or "",
hint = _("Catalog Name"), hint = gettext("Catalog Name"),
}, },
{ {
text = item.url or "", text = item.url or "",
hint = _("Catalog URL"), hint = gettext("Catalog URL"),
}, },
}, },
buttons = { buttons = {
{ {
{ {
text = _("Cancel"), text = gettext("Cancel"),
callback = function() callback = function()
self.edit_server_dialog:onClose() self.edit_server_dialog:onClose()
UIManager:close(self.edit_server_dialog) UIManager:close(self.edit_server_dialog)
end end
}, },
{ {
text = _("Apply"), text = gettext("Apply"),
callback = function() callback = function()
self.edit_server_dialog:onClose() self.edit_server_dialog:onClose()
UIManager:close(self.edit_server_dialog) UIManager:close(self.edit_server_dialog)
@ -684,7 +684,7 @@ function OPDSBrowser:onMenuHold(item)
buttons = { buttons = {
{ {
{ {
text = _("Edit"), text = gettext("Edit"),
enabled = item.editable, enabled = item.editable,
callback = function() callback = function()
UIManager:close(self.opds_server_dialog) UIManager:close(self.opds_server_dialog)
@ -696,7 +696,7 @@ function OPDSBrowser:onMenuHold(item)
end end
}, },
{ {
text = _("Delete"), text = gettext("Delete"),
enabled = item.deletable, enabled = item.deletable,
callback = function() callback = function()
UIManager:close(self.opds_server_dialog) UIManager:close(self.opds_server_dialog)

@ -256,11 +256,15 @@ function util.getFilesystemType(path)
end end
function util.replaceInvalidChars(str) function util.replaceInvalidChars(str)
return str:gsub('[\\,%/,:,%*,%?,%",%<,%>,%|]','_') if str then
return str:gsub('[\\,%/,:,%*,%?,%",%<,%>,%|]','_'):gsub("([\224-\244]+)",'_')
end
end end
function util.replaceSlashChar(str) function util.replaceSlashChar(str)
return str:gsub('%/','_') if str then
return str:gsub('%/','_'):gsub("([\224-\244]+)",'_')
end
end end
-- Split a file into its path and name -- Split a file into its path and name

Loading…
Cancel
Save