From d10a52e88a4ce01a4f9cf53a4f32915ec3c19184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Fern=C3=A1ndez?= <975883+pazos@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:00:19 +0100 Subject: [PATCH] calibre: always use custom fast parser (#12714) Fixes #12610 --- plugins/calibre.koplugin/metadata.lua | 15 +---- plugins/calibre.koplugin/parser.lua | 97 --------------------------- 2 files changed, 1 insertion(+), 111 deletions(-) delete mode 100644 plugins/calibre.koplugin/parser.lua diff --git a/plugins/calibre.koplugin/metadata.lua b/plugins/calibre.koplugin/metadata.lua index ec179a496..8ca7a9b0e 100644 --- a/plugins/calibre.koplugin/metadata.lua +++ b/plugins/calibre.koplugin/metadata.lua @@ -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)) diff --git a/plugins/calibre.koplugin/parser.lua b/plugins/calibre.koplugin/parser.lua deleted file mode 100644 index f067f38dc..000000000 --- a/plugins/calibre.koplugin/parser.lua +++ /dev/null @@ -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