migrate calibre plugin to persist

reviewable/pr7138/r1
Martín Fdez 3 years ago committed by Frans de Jonge
parent b8f0dc3752
commit b27cf0d168

@ -127,7 +127,7 @@ function Calibre:getSearchMenuTable()
sub_item_table_func = function()
local result = {}
-- append previous scanned dirs to the list.
local cache = LuaSettings:open(CalibreSearch.user_libraries)
local cache = LuaSettings:open(CalibreSearch.cache_libs.path)
for path, _ in pairs(cache.data) do
table.insert(result, {
text = path,

@ -13,32 +13,20 @@ local InputDialog = require("ui/widget/inputdialog")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local Menu = require("ui/widget/menu")
local Persist = require("persist")
local Screen = require("device").screen
local UIManager = require("ui/uimanager")
local logger = require("logger")
local socket = require("socket")
local util = require("util")
local _ = require("gettext")
local T = require("ffi/util").template
-- cache files
local libraries_file = "calibre-libraries.lua"
local metadata_file = "calibre-books.lua"
-- loads a table from disk
local function loadTable(path)
local ok, data = pcall(dofile, path)
if ok then
return data
else
return nil, data
end
end
-- get root dir for disk scans
local function getDefaultRootDir()
if Device:isCervantes() or Device:isKobo() then
return "/mnt"
elseif Device:isEmulator() then
return lfs.currentdir()
else
return Device.home_dir or lfs.currentdir()
end
@ -183,8 +171,15 @@ local CalibreSearch = InputContainer:new{
"find_by_authors",
"find_by_path",
},
user_libraries = DataStorage:getDataDir() .. "/cache/" .. libraries_file,
user_book_cache = DataStorage:getDataDir() .. "/cache/" .. metadata_file,
cache_libs = Persist:new{
path = DataStorage:getDataDir() .. "/cache/calibre-libraries.lua",
},
cache_books = Persist:new{
path = DataStorage:getDataDir() .. "/cache/calibre-books.dat",
codec = "bitser",
},
}
function CalibreSearch:ShowSearch()
@ -311,7 +306,7 @@ function CalibreSearch:find(option)
end
if #self.libraries == 0 then
local libs, err = loadTable(self.user_libraries)
local libs, err = self.cache_libs:load()
if not libs then
logger.warn("no calibre libraries", err)
self:prompt(_("No calibre libraries"))
@ -504,7 +499,7 @@ function CalibreSearch:prompt(message)
paths = paths .. "\n" .. _("SD card") .. ": " .. sd_paths
end
util.dumpTable(self.libraries, self.user_libraries)
self.cache_libs:save(self.libraries)
self:invalidateCache()
self.books = self:getMetadata()
local info_text
@ -559,7 +554,7 @@ end
-- invalidate current cache
function CalibreSearch:invalidateCache()
util.removeFile(self.user_book_cache)
self.cache_books:delete()
self.books = {}
end
@ -571,12 +566,14 @@ function CalibreSearch:getMetadata()
-- try to load metadata from cache
if self.cache_metadata then
local function cacheIsNewer(timestamp)
if not timestamp then return false end
local file_timestamp = self.cache_books:timestamp()
if not timestamp or not file_timestamp then return false end
local Y, M, D, h, m, s = timestamp:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)")
local date = os.time({year = Y, month = M, day = D, hour = h, min = m, sec = s})
return lfs.attributes(self.user_book_cache, "modification") > date
return file_timestamp > date
end
local cache, err = loadTable(self.user_book_cache)
local cache, err = self.cache_books:load()
if not cache then
logger.warn("invalid cache:", err)
else
@ -612,7 +609,7 @@ function CalibreSearch:getMetadata()
for index, book in ipairs(books) do
table.insert(dump, index, removeNull(book))
end
util.dumpTable(dump, self.user_book_cache)
self.cache_books:save(dump)
end
local elapsed = socket.gettime() - start
logger.info(string.format(template, #books, "calibre", elapsed * 1000))

Loading…
Cancel
Save