issue error directly when doc is malformated

This should popup a message saying "No reader engine for this file"
instead of a crash when document file is malformated.

This should fix #868.
pull/870/head
chrox 10 years ago
parent 233f847954
commit 2f2d9f1bf7

@ -76,7 +76,6 @@ function CreDocument:init()
self:engineInit() self:engineInit()
self.configurable:loadDefaults(self.options) self.configurable:loadDefaults(self.options)
local ok
local file_type = string.lower(string.match(self.file, ".+%.([^.]+)")) local file_type = string.lower(string.match(self.file, ".+%.([^.]+)"))
if file_type == "zip" then if file_type == "zip" then
-- NuPogodi, 20.05.12: read the content of zip-file -- NuPogodi, 20.05.12: read the content of zip-file
@ -95,12 +94,12 @@ function CreDocument:init()
-- @TODO check the default view_mode to a global user configurable -- @TODO check the default view_mode to a global user configurable
-- variable 22.12 2012 (houqp) -- variable 22.12 2012 (houqp)
local ok
ok, self._document = pcall(cre.newDocView, ok, self._document = pcall(cre.newDocView,
Screen:getWidth(), Screen:getHeight(), self.PAGE_VIEW_MODE Screen:getWidth(), Screen:getHeight(), self.PAGE_VIEW_MODE
) )
if not ok then if not ok then
self.error_message = self._document -- will contain error message error(self._document) -- will contain error message
return
end end
-- adjust font sizes according to screen dpi -- adjust font sizes according to screen dpi

@ -30,15 +30,13 @@ function DjvuDocument:init()
self.koptinterface = require("document/koptinterface") self.koptinterface = require("document/koptinterface")
self.configurable:loadDefaults(self.options) self.configurable:loadDefaults(self.options)
if not validDjvuFile(self.file) then if not validDjvuFile(self.file) then
self.error_message = "Not a valid DjVu file" error("Not a valid DjVu file")
return
end end
local ok local ok
ok, self._document = pcall(djvu.openDocument, self.file, self.djvulibre_cache_size) ok, self._document = pcall(djvu.openDocument, self.file, self.djvulibre_cache_size)
if not ok then if not ok then
self.error_message = self._document -- will contain error message error(self._document) -- will contain error message
return
end end
self.is_open = true self.is_open = true
self.info.has_pages = true self.info.has_pages = true

@ -24,7 +24,6 @@ local Document = {
-- flag to show whether the document was opened successfully -- flag to show whether the document was opened successfully
is_open = false, is_open = false,
error_message = nil,
-- flag to show that the document needs to be unlocked by a password -- flag to show that the document needs to be unlocked by a password
is_locked = false, is_locked = false,

@ -1,3 +1,5 @@
local DEBUG = require("dbg")
--[[ --[[
This is a registry for document providers This is a registry for document providers
]]-- ]]--
@ -24,15 +26,22 @@ function DocumentRegistry:openDocument(file)
if not self.registry[file] then if not self.registry[file] then
local provider = self:getProvider(file) local provider = self:getProvider(file)
if provider ~= nil then if provider ~= nil then
self.registry[file] = { local ok, doc = pcall(provider.new, provider, {file = file})
doc = provider:new{file = file}, if ok then
refs = 1, self.registry[file] = {
} doc = doc,
refs = 1,
}
else
DEBUG("cannot open document", file, doc)
end
end end
else else
self.registry[file].refs = self.registry[file].refs + 1 self.registry[file].refs = self.registry[file].refs + 1
end end
return self.registry[file].doc if self.registry[file] then
return self.registry[file].doc
end
end end
function DocumentRegistry:closeDocument(file) function DocumentRegistry:closeDocument(file)

@ -56,8 +56,7 @@ function PdfDocument:init()
local ok local ok
ok, self._document = pcall(pdf.openDocument, self.file, self.mupdf_cache_size) ok, self._document = pcall(pdf.openDocument, self.file, self.mupdf_cache_size)
if not ok then if not ok then
self.error_message = self._document -- will contain error message error(self._document) -- will contain error message
return
end end
self.is_open = true self.is_open = true
self.info.has_pages = true self.info.has_pages = true
@ -67,6 +66,9 @@ function PdfDocument:init()
else else
self:_readMetadata() self:_readMetadata()
end end
if not (self.info.number_of_pages > 0) then
error("No page found in PDF file")
end
end end
function PdfDocument:unlock(password) function PdfDocument:unlock(password)

@ -11,8 +11,7 @@ function PicDocument:init()
if not pic then pic = require("ffi/pic") end if not pic then pic = require("ffi/pic") end
ok, self._document = pcall(pic.openDocument, self.file) ok, self._document = pcall(pic.openDocument, self.file)
if not ok then if not ok then
self.error_message = "failed to open jpeg image" error("Failed to open jpeg image")
return
end end
self.info.has_pages = true self.info.has_pages = true

Loading…
Cancel
Save