mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
@@ -11,7 +11,6 @@ of storing it.
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local rapidjson = require("rapidjson")
|
||||
local logger = require("logger")
|
||||
local parser = require("parser")
|
||||
local util = require("util")
|
||||
local time = require("ui/time")
|
||||
|
||||
@@ -52,10 +51,6 @@ local function slim(book, is_search)
|
||||
return slim_book
|
||||
end
|
||||
|
||||
-- This is the max file size we attempt to decode using rapidjson.
|
||||
-- For larger files we use a sax parser to avoid OOM errors
|
||||
local MAX_JSON_FILESIZE = 50 * 1024 * 1024
|
||||
|
||||
--- find calibre files for a given dir
|
||||
local function findCalibreFiles(dir)
|
||||
local function existOrLast(file)
|
||||
@@ -120,15 +115,7 @@ function CalibreMetadata:loadBookList()
|
||||
logger.warn("File is invalid", self.metadata)
|
||||
return rapidjson.array({})
|
||||
end
|
||||
local books, err
|
||||
local impl = G_reader_settings:readSetting("calibre_json_parser") or attr.size > MAX_JSON_FILESIZE and "safe" or "fast"
|
||||
if impl == "fast" then
|
||||
books, err = rapidjson.load_calibre(self.metadata)
|
||||
elseif impl == "safe" then
|
||||
books, err = parser.parseFile(self.metadata)
|
||||
else
|
||||
books, err = rapidjson.load(self.metadata)
|
||||
end
|
||||
local books, err = rapidjson.load_calibre(self.metadata)
|
||||
if not books then
|
||||
logger.warn(string.format("Unable to load library from json file %s: \n%s",
|
||||
self.metadata, err))
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
-- parse "metadata.calibre" files
|
||||
local lj = require("lunajson")
|
||||
local rapidjson = require("rapidjson")
|
||||
|
||||
local array_fields = {
|
||||
authors = true,
|
||||
tags = true,
|
||||
series = true,
|
||||
}
|
||||
|
||||
local required_fields = {
|
||||
authors = true,
|
||||
last_modified = true,
|
||||
lpath = true,
|
||||
series = true,
|
||||
series_index = true,
|
||||
size = true,
|
||||
tags = true,
|
||||
title = true,
|
||||
uuid = true,
|
||||
}
|
||||
|
||||
local field
|
||||
local t = {}
|
||||
local function append(v)
|
||||
-- Some fields *may* be arrays, so check whether we ran through startarray first or not
|
||||
if t[field] then
|
||||
table.insert(t[field], v)
|
||||
else
|
||||
t[field] = v
|
||||
field = nil
|
||||
end
|
||||
end
|
||||
|
||||
local depth = 0
|
||||
local result = rapidjson.array({})
|
||||
local sax = {
|
||||
startobject = function()
|
||||
depth = depth + 1
|
||||
end,
|
||||
endobject = function()
|
||||
if depth == 1 then
|
||||
table.insert(result, rapidjson.object(t))
|
||||
t = {}
|
||||
end
|
||||
depth = depth - 1
|
||||
end,
|
||||
startarray = function()
|
||||
if array_fields[field] then
|
||||
t[field] = rapidjson.array({})
|
||||
end
|
||||
end,
|
||||
endarray = function()
|
||||
if field then
|
||||
field = nil
|
||||
end
|
||||
end,
|
||||
key = function(s)
|
||||
if required_fields[s] then
|
||||
field = s
|
||||
end
|
||||
end,
|
||||
string = function(s)
|
||||
if field then
|
||||
append(s)
|
||||
end
|
||||
end,
|
||||
number = function(n)
|
||||
if field then
|
||||
append(n)
|
||||
end
|
||||
end,
|
||||
boolean = function(b)
|
||||
if field then
|
||||
append(b)
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
local function parse_unsafe(path)
|
||||
local p = lj.newfileparser(path, sax)
|
||||
p.run()
|
||||
end
|
||||
|
||||
local parser = {}
|
||||
|
||||
function parser.parseFile(file)
|
||||
result = rapidjson.array({})
|
||||
local ok, err = pcall(parse_unsafe, file)
|
||||
field = nil
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
return parser
|
||||
Reference in New Issue
Block a user