2017-04-14 04:11:10 +00:00
|
|
|
--[[--
|
|
|
|
This module is responsible for reading and writing `metadata.lua` files
|
|
|
|
in the so-called sidecar directory
|
|
|
|
([Wikipedia definition](https://en.wikipedia.org/wiki/Sidecar_file)).
|
|
|
|
]]
|
|
|
|
|
2015-06-15 08:46:43 +00:00
|
|
|
local DataStorage = require("datastorage")
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local LuaSettings = require("luasettings")
|
2015-10-03 06:18:47 +00:00
|
|
|
local dump = require("dump")
|
2019-12-10 22:00:08 +00:00
|
|
|
local ffiutil = require("ffi/util")
|
2017-04-14 04:11:10 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2017-08-17 01:38:58 +00:00
|
|
|
local logger = require("logger")
|
2021-10-21 20:43:05 +00:00
|
|
|
local util = require("util")
|
2015-10-03 06:18:47 +00:00
|
|
|
|
2023-06-12 06:08:56 +00:00
|
|
|
local DocSettings = LuaSettings:extend{}
|
2015-10-03 06:18:47 +00:00
|
|
|
|
2016-06-04 05:05:14 +00:00
|
|
|
local HISTORY_DIR = DataStorage:getHistoryDir()
|
2023-02-17 06:35:49 +00:00
|
|
|
local DOCSETTINGS_DIR = DataStorage:getDocSettingsDir()
|
2023-10-11 08:39:33 +00:00
|
|
|
local DOCSETTINGS_HASH_DIR = DataStorage:getDocSettingsHashDir()
|
2023-09-01 05:07:29 +00:00
|
|
|
local custom_metadata_filename = "custom_metadata.lua"
|
2011-12-05 21:31:40 +00:00
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings.getSidecarStorage(location)
|
|
|
|
if location == "dir" then
|
|
|
|
return DOCSETTINGS_DIR
|
|
|
|
elseif location == "hash" then
|
|
|
|
return DOCSETTINGS_HASH_DIR
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function isDir(dir)
|
|
|
|
return lfs.attributes(dir, "mode") == "directory"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function isFile(file)
|
|
|
|
return lfs.attributes(file, "mode") == "file"
|
|
|
|
end
|
|
|
|
|
2023-10-15 04:47:09 +00:00
|
|
|
local doc_hash_cache = {}
|
2023-10-31 05:30:39 +00:00
|
|
|
local is_hash_location_enabled
|
2023-10-11 08:39:33 +00:00
|
|
|
|
|
|
|
function DocSettings.isHashLocationEnabled()
|
|
|
|
if is_hash_location_enabled == nil then
|
2023-10-31 05:30:39 +00:00
|
|
|
is_hash_location_enabled = isDir(DOCSETTINGS_HASH_DIR)
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
|
|
|
return is_hash_location_enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings.setIsHashLocationEnabled(value)
|
|
|
|
is_hash_location_enabled = value
|
|
|
|
end
|
|
|
|
|
2022-10-09 17:32:35 +00:00
|
|
|
local function buildCandidates(list)
|
|
|
|
local candidates = {}
|
2022-12-12 00:22:46 +00:00
|
|
|
local previous_entry_exists = false
|
2022-10-09 17:32:35 +00:00
|
|
|
|
|
|
|
for i, file_path in ipairs(list) do
|
|
|
|
-- Ignore missing files.
|
2023-10-31 05:30:39 +00:00
|
|
|
if file_path ~= "" and isFile(file_path) then
|
2022-12-12 00:22:46 +00:00
|
|
|
local mtime = lfs.attributes(file_path, "modification")
|
2022-12-12 01:07:01 +00:00
|
|
|
-- NOTE: Extra trickery: if we're inserting a "backup" file, and its primary buddy exists,
|
|
|
|
-- make sure it will *never* sort ahead of it by using the same mtime.
|
2022-12-12 00:22:46 +00:00
|
|
|
-- This aims to avoid weird UTC/localtime issues when USBMS is involved,
|
|
|
|
-- c.f., https://github.com/koreader/koreader/issues/9227#issuecomment-1345263324
|
|
|
|
if file_path:sub(-4) == ".old" and previous_entry_exists then
|
2022-12-12 01:07:01 +00:00
|
|
|
local primary_mtime = candidates[#candidates].mtime
|
|
|
|
-- Only proceed with the switcheroo when necessary, and warn about it.
|
|
|
|
if primary_mtime < mtime then
|
|
|
|
logger.warn("DocSettings: Backup", file_path, "is newer (", mtime, ") than its primary (", primary_mtime, "), fudging timestamps!")
|
|
|
|
-- Use the most recent timestamp for both (i.e., the backup's).
|
|
|
|
candidates[#candidates].mtime = mtime
|
|
|
|
end
|
2022-12-12 00:22:46 +00:00
|
|
|
end
|
2022-10-09 17:32:35 +00:00
|
|
|
table.insert(candidates, {
|
|
|
|
path = file_path,
|
2022-12-12 00:22:46 +00:00
|
|
|
mtime = mtime,
|
2022-10-09 17:32:35 +00:00
|
|
|
prio = i,
|
|
|
|
}
|
|
|
|
)
|
2022-12-12 00:22:46 +00:00
|
|
|
previous_entry_exists = true
|
|
|
|
else
|
|
|
|
previous_entry_exists = false
|
2022-10-09 17:32:35 +00:00
|
|
|
end
|
2016-04-26 02:45:55 +00:00
|
|
|
end
|
2022-10-09 17:32:35 +00:00
|
|
|
|
|
|
|
-- MRU sort, tie breaker is insertion order (higher priority locations were inserted first).
|
2022-12-12 01:07:01 +00:00
|
|
|
-- Iff a primary/backup pair of file both exist, of the two of them, the primary one *always* has priority,
|
2022-12-12 00:22:46 +00:00
|
|
|
-- regardless of mtime (c.f., NOTE above).
|
2022-10-09 17:32:35 +00:00
|
|
|
table.sort(candidates, function(l, r)
|
|
|
|
if l.mtime == r.mtime then
|
|
|
|
return l.prio < r.prio
|
|
|
|
else
|
|
|
|
return l.mtime > r.mtime
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
return candidates
|
2016-04-26 02:45:55 +00:00
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
local function getOrderedLocationCandidates()
|
|
|
|
local preferred_location = G_reader_settings:readSetting("document_metadata_folder", "doc")
|
|
|
|
if preferred_location == "hash" then
|
|
|
|
return { "hash", "doc", "dir" }
|
|
|
|
end
|
|
|
|
local candidates = preferred_location == "doc" and { "doc", "dir" } or { "dir", "doc" }
|
|
|
|
if DocSettings.isHashLocationEnabled() then
|
|
|
|
table.insert(candidates, "hash")
|
|
|
|
end
|
|
|
|
return candidates
|
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Returns path to sidecar directory (`filename.sdr`).
|
2016-04-26 02:45:55 +00:00
|
|
|
-- Sidecar directory is the file without _last_ suffix.
|
2017-04-14 04:11:10 +00:00
|
|
|
-- @string doc_path path to the document (e.g., `/foo/bar.pdf`)
|
2023-10-11 08:39:33 +00:00
|
|
|
-- @string force_location prefer e.g., "hash" or "dir" location over standard "doc", if available
|
2017-04-14 04:11:10 +00:00
|
|
|
-- @treturn string path to the sidecar directory (e.g., `/foo/bar.sdr`)
|
2023-02-17 06:35:49 +00:00
|
|
|
function DocSettings:getSidecarDir(doc_path, force_location)
|
|
|
|
if doc_path == nil or doc_path == "" then return "" end
|
|
|
|
local path = doc_path:match("(.*)%.") or doc_path -- file path without the last suffix
|
|
|
|
local location = force_location or G_reader_settings:readSetting("document_metadata_folder", "doc")
|
|
|
|
if location == "dir" then
|
2023-10-11 08:39:33 +00:00
|
|
|
path = DOCSETTINGS_DIR .. path
|
|
|
|
elseif location == "hash" then
|
2023-10-15 04:47:09 +00:00
|
|
|
local hsh = doc_hash_cache[doc_path]
|
2023-10-11 08:39:33 +00:00
|
|
|
if not hsh then
|
2023-10-15 04:47:09 +00:00
|
|
|
hsh = util.partialMD5(doc_path)
|
|
|
|
if not hsh then -- fallback to "doc"
|
|
|
|
return path .. ".sdr"
|
|
|
|
end
|
|
|
|
doc_hash_cache[doc_path] = hsh
|
2023-10-11 08:39:33 +00:00
|
|
|
logger.dbg("DocSettings: Caching new partial MD5 hash for", doc_path, "as", hsh)
|
|
|
|
else
|
|
|
|
logger.dbg("DocSettings: Using cached partial MD5 hash for", doc_path, "as", hsh)
|
|
|
|
end
|
|
|
|
-- converts b3fb8f4f8448160365087d6ca05c7fa2 to b3/ to avoid too many files in one dir
|
|
|
|
local subpath = string.format("/%s/", hsh:sub(1, 2))
|
|
|
|
path = DOCSETTINGS_HASH_DIR .. subpath .. hsh
|
2016-12-03 13:19:35 +00:00
|
|
|
end
|
2023-10-11 08:39:33 +00:00
|
|
|
return path .. ".sdr"
|
2016-12-03 13:19:35 +00:00
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings.getSidecarFilename(doc_path)
|
|
|
|
local suffix = doc_path:match(".*%.(.+)") or "_"
|
|
|
|
return "metadata." .. suffix .. ".lua"
|
2017-01-21 09:32:42 +00:00
|
|
|
end
|
|
|
|
|
2023-09-05 04:42:46 +00:00
|
|
|
--- Returns `true` if there is a `metadata.lua` file.
|
|
|
|
-- @string doc_path path to the document (e.g., `/foo/bar.pdf`)
|
|
|
|
-- @treturn bool
|
|
|
|
function DocSettings:hasSidecarFile(doc_path)
|
2023-10-31 05:30:39 +00:00
|
|
|
return self:findSidecarFile(doc_path) and true or false
|
2023-09-05 04:42:46 +00:00
|
|
|
end
|
|
|
|
|
2023-05-03 12:43:05 +00:00
|
|
|
--- Returns path of `metadata.lua` file if it exists, or nil.
|
2017-04-14 04:11:10 +00:00
|
|
|
-- @string doc_path path to the document (e.g., `/foo/bar.pdf`)
|
2023-05-03 12:43:05 +00:00
|
|
|
-- @bool no_legacy set to true to skip check of the legacy history file
|
2024-01-12 17:48:12 +00:00
|
|
|
-- @treturn string (or nil on failure)
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings:findSidecarFile(doc_path, no_legacy)
|
2024-01-12 17:48:12 +00:00
|
|
|
if doc_path == nil or doc_path == "" then return nil end
|
2023-10-31 05:30:39 +00:00
|
|
|
local sidecar_filename = DocSettings.getSidecarFilename(doc_path)
|
|
|
|
local sidecar_file
|
|
|
|
for _, location in ipairs(getOrderedLocationCandidates()) do
|
|
|
|
sidecar_file = self:getSidecarDir(doc_path, location) .. "/" .. sidecar_filename
|
|
|
|
if isFile(sidecar_file) then
|
|
|
|
return sidecar_file, location
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
|
|
|
end
|
2023-05-03 12:43:05 +00:00
|
|
|
if not no_legacy then
|
|
|
|
sidecar_file = self:getHistoryPath(doc_path)
|
2023-10-31 05:30:39 +00:00
|
|
|
if isFile(sidecar_file) then
|
|
|
|
return sidecar_file, "hist" -- for isSidecarFileNotInPreferredLocation() used in moveBookMetadata
|
2023-05-03 12:43:05 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-17 06:35:49 +00:00
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings.isSidecarFileNotInPreferredLocation(doc_path)
|
|
|
|
local _, location = DocSettings:findSidecarFile(doc_path)
|
|
|
|
return location and location ~= G_reader_settings:readSetting("document_metadata_folder", "doc")
|
|
|
|
end
|
|
|
|
|
2023-02-17 06:35:49 +00:00
|
|
|
function DocSettings:getHistoryPath(doc_path)
|
|
|
|
if doc_path == nil or doc_path == "" then return "" end
|
|
|
|
return HISTORY_DIR .. "/[" .. doc_path:gsub("(.*/)([^/]+)", "%1] %2"):gsub("/", "#") .. ".lua"
|
2012-12-12 01:35:49 +00:00
|
|
|
end
|
|
|
|
|
2013-03-12 04:51:00 +00:00
|
|
|
function DocSettings:getPathFromHistory(hist_name)
|
2023-02-17 06:35:49 +00:00
|
|
|
if hist_name == nil or hist_name == "" then return "" end
|
|
|
|
if hist_name:sub(-4) ~= ".lua" then return "" end -- ignore .lua.old backups
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 1. select everything included in brackets
|
|
|
|
local s = string.match(hist_name,"%b[]")
|
2023-02-17 06:35:49 +00:00
|
|
|
if s == nil or s == "" then return "" end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 2. crop the bracket-sign from both sides
|
|
|
|
-- 3. and finally replace decorative signs '#' to dir-char '/'
|
2022-10-09 17:32:35 +00:00
|
|
|
return string.gsub(string.sub(s, 2, -3), "#", "/")
|
2013-03-12 04:51:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:getNameFromHistory(hist_name)
|
2023-02-17 06:35:49 +00:00
|
|
|
if hist_name == nil or hist_name == "" then return "" end
|
|
|
|
if hist_name:sub(-4) ~= ".lua" then return "" end -- ignore .lua.old backups
|
2016-09-13 03:02:48 +00:00
|
|
|
local s = string.match(hist_name, "%b[]")
|
2023-02-17 06:35:49 +00:00
|
|
|
if s == nil or s == "" then return "" end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- at first, search for path length
|
|
|
|
-- and return the rest of string without 4 last characters (".lua")
|
2016-09-13 03:02:48 +00:00
|
|
|
return string.sub(hist_name, string.len(s)+2, -5)
|
2013-03-12 04:51:00 +00:00
|
|
|
end
|
|
|
|
|
2023-02-17 06:35:49 +00:00
|
|
|
function DocSettings:getFileFromHistory(hist_name)
|
|
|
|
local path = self:getPathFromHistory(hist_name)
|
|
|
|
if path ~= "" then
|
|
|
|
local name = self:getNameFromHistory(hist_name)
|
|
|
|
if name ~= "" then
|
|
|
|
return ffiutil.joinPath(path, name)
|
|
|
|
end
|
2017-01-21 09:32:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Opens a document's individual settings (font, margin, dictionary, etc.)
|
2023-02-17 06:35:49 +00:00
|
|
|
-- @string doc_path path to the document (e.g., `/foo/bar.pdf`)
|
2017-04-14 04:11:10 +00:00
|
|
|
-- @treturn DocSettings object
|
2023-02-17 06:35:49 +00:00
|
|
|
function DocSettings:open(doc_path)
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- NOTE: Beware, our new instance is new, but self is still DocSettings!
|
|
|
|
local new = DocSettings:extend{}
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
new.sidecar_filename = DocSettings.getSidecarFilename(doc_path)
|
|
|
|
|
2023-02-17 06:35:49 +00:00
|
|
|
new.doc_sidecar_dir = new:getSidecarDir(doc_path, "doc")
|
|
|
|
local doc_sidecar_file, legacy_sidecar_file
|
2023-10-31 05:30:39 +00:00
|
|
|
if isDir(new.doc_sidecar_dir) then
|
|
|
|
doc_sidecar_file = new.doc_sidecar_dir .. "/" .. new.sidecar_filename
|
2023-10-11 08:39:33 +00:00
|
|
|
legacy_sidecar_file = new.doc_sidecar_dir .. "/" .. ffiutil.basename(doc_path) .. ".lua"
|
2023-02-17 06:35:49 +00:00
|
|
|
end
|
|
|
|
new.dir_sidecar_dir = new:getSidecarDir(doc_path, "dir")
|
|
|
|
local dir_sidecar_file
|
2023-10-31 05:30:39 +00:00
|
|
|
if isDir(new.dir_sidecar_dir) then
|
|
|
|
dir_sidecar_file = new.dir_sidecar_dir .. "/" .. new.sidecar_filename
|
2016-06-26 00:53:08 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
local hash_sidecar_file
|
2023-10-11 08:39:33 +00:00
|
|
|
if DocSettings.isHashLocationEnabled() then
|
2023-10-31 05:30:39 +00:00
|
|
|
new.hash_sidecar_dir = new:getSidecarDir(doc_path, "hash")
|
|
|
|
hash_sidecar_file = new.hash_sidecar_dir .. "/" .. new.sidecar_filename
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
local history_file = new:getHistoryPath(doc_path)
|
2023-10-11 08:39:33 +00:00
|
|
|
|
2022-10-09 17:32:35 +00:00
|
|
|
-- Candidates list, in order of priority:
|
|
|
|
local candidates_list = {
|
2023-02-17 06:35:49 +00:00
|
|
|
-- New sidecar file in doc folder
|
|
|
|
doc_sidecar_file or "",
|
|
|
|
-- Backup file of new sidecar file in doc folder
|
2023-10-11 08:39:33 +00:00
|
|
|
doc_sidecar_file and (doc_sidecar_file .. ".old") or "",
|
2022-10-09 17:32:35 +00:00
|
|
|
-- Legacy sidecar file
|
2023-02-17 06:35:49 +00:00
|
|
|
legacy_sidecar_file or "",
|
|
|
|
-- New sidecar file in docsettings folder
|
|
|
|
dir_sidecar_file or "",
|
|
|
|
-- Backup file of new sidecar file in docsettings folder
|
2023-10-11 08:39:33 +00:00
|
|
|
dir_sidecar_file and (dir_sidecar_file .. ".old") or "",
|
2023-10-31 05:30:39 +00:00
|
|
|
-- New sidecar file in hashdocsettings folder
|
2023-10-11 08:39:33 +00:00
|
|
|
hash_sidecar_file or "",
|
2023-10-31 05:30:39 +00:00
|
|
|
-- Backup file of new sidecar file in hashdocsettings folder
|
|
|
|
hash_sidecar_file and (hash_sidecar_file .. ".old") or "",
|
2022-10-09 17:32:35 +00:00
|
|
|
-- Legacy history folder
|
2023-02-17 06:35:49 +00:00
|
|
|
history_file,
|
2022-10-09 17:32:35 +00:00
|
|
|
-- Backup file in legacy history folder
|
2023-10-11 08:39:33 +00:00
|
|
|
history_file .. ".old",
|
2022-10-09 17:32:35 +00:00
|
|
|
-- Legacy kpdfview setting
|
2023-10-11 08:39:33 +00:00
|
|
|
doc_path .. ".kpdfview.lua",
|
2022-10-09 17:32:35 +00:00
|
|
|
}
|
|
|
|
-- We get back an array of tables for *existing* candidates, sorted MRU first (insertion order breaks ties).
|
|
|
|
local candidates = buildCandidates(candidates_list)
|
|
|
|
|
2023-05-03 12:43:05 +00:00
|
|
|
local candidate_path, ok, stored
|
2022-10-09 17:32:35 +00:00
|
|
|
for _, t in ipairs(candidates) do
|
2023-05-03 12:43:05 +00:00
|
|
|
candidate_path = t.path
|
2017-10-12 17:52:01 +00:00
|
|
|
-- Ignore empty files
|
2022-10-09 17:32:35 +00:00
|
|
|
if lfs.attributes(candidate_path, "size") > 0 then
|
|
|
|
ok, stored = pcall(dofile, candidate_path)
|
|
|
|
-- Ignore empty tables
|
2017-10-12 17:52:01 +00:00
|
|
|
if ok and next(stored) ~= nil then
|
2022-10-09 17:32:35 +00:00
|
|
|
logger.dbg("DocSettings: data is read from", candidate_path)
|
2017-10-12 17:52:01 +00:00
|
|
|
break
|
|
|
|
end
|
2014-07-03 12:42:48 +00:00
|
|
|
end
|
2022-10-09 17:32:35 +00:00
|
|
|
logger.dbg("DocSettings:", candidate_path, "is invalid, removed.")
|
|
|
|
os.remove(candidate_path)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-06-04 18:55:39 +00:00
|
|
|
if ok and stored then
|
2014-03-13 13:52:43 +00:00
|
|
|
new.data = stored
|
2016-06-26 00:53:08 +00:00
|
|
|
new.candidates = candidates
|
2023-05-03 12:43:05 +00:00
|
|
|
new.source_candidate = candidate_path
|
2016-06-26 00:53:08 +00:00
|
|
|
else
|
|
|
|
new.data = {}
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2023-03-01 12:52:08 +00:00
|
|
|
new.data.doc_path = doc_path
|
2016-04-18 22:38:14 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
return new
|
2012-04-18 09:29:27 +00:00
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
--- Light version of open(). Opens a sidecar file or a custom metadata file.
|
|
|
|
-- Returned object cannot be used to save changes to the sidecar file (flush()).
|
|
|
|
-- Must be used to save changes to the custom metadata file (flushCustomMetadata()).
|
|
|
|
function DocSettings.openSettingsFile(sidecar_file)
|
|
|
|
local new = DocSettings:extend{}
|
|
|
|
local ok, stored
|
|
|
|
if sidecar_file then
|
|
|
|
ok, stored = pcall(dofile, sidecar_file)
|
|
|
|
end
|
|
|
|
if ok and next(stored) ~= nil then
|
|
|
|
new.data = stored
|
|
|
|
else
|
|
|
|
new.data = {}
|
|
|
|
end
|
|
|
|
new.sidecar_file = sidecar_file
|
|
|
|
return new
|
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Serializes settings and writes them to `metadata.lua`.
|
2023-09-01 05:07:29 +00:00
|
|
|
function DocSettings:flush(data, no_custom_metadata)
|
2023-10-31 05:30:39 +00:00
|
|
|
data = data or self.data
|
|
|
|
local sidecar_dirs
|
|
|
|
local preferred_location = G_reader_settings:readSetting("document_metadata_folder", "doc")
|
|
|
|
if preferred_location == "doc" then
|
|
|
|
sidecar_dirs = { self.doc_sidecar_dir, self.dir_sidecar_dir } -- fallback for read-only book storage
|
|
|
|
elseif preferred_location == "dir" then
|
|
|
|
sidecar_dirs = { self.dir_sidecar_dir }
|
|
|
|
elseif preferred_location == "hash" then
|
|
|
|
if self.hash_sidecar_dir == nil then
|
|
|
|
self.hash_sidecar_dir = self:getSidecarDir(data.doc_path, "hash")
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
sidecar_dirs = { self.hash_sidecar_dir }
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2014-07-03 12:42:48 +00:00
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
local ser_data = dump(data, nil, true)
|
|
|
|
for _, sidecar_dir in ipairs(sidecar_dirs) do
|
|
|
|
local sidecar_dir_slash = sidecar_dir .. "/"
|
|
|
|
local sidecar_file = sidecar_dir_slash .. self.sidecar_filename
|
2023-05-03 12:43:05 +00:00
|
|
|
util.makePath(sidecar_dir)
|
2023-02-17 06:35:49 +00:00
|
|
|
logger.dbg("DocSettings: Writing to", sidecar_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
local directory_updated = LuaSettings:backup(sidecar_file) -- "*.old"
|
|
|
|
if util.writeToFile(ser_data, sidecar_file, true, true, directory_updated) then
|
2023-09-01 05:07:29 +00:00
|
|
|
-- move custom cover file and custom metadata file to the metadata file location
|
|
|
|
if not no_custom_metadata then
|
|
|
|
local metadata_file, filepath, filename
|
|
|
|
-- custom cover
|
2023-10-31 05:30:39 +00:00
|
|
|
metadata_file = self:getCustomCoverFile()
|
2023-09-01 05:07:29 +00:00
|
|
|
if metadata_file then
|
|
|
|
filepath, filename = util.splitFilePathName(metadata_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
if filepath ~= sidecar_dir_slash then
|
|
|
|
ffiutil.copyFile(metadata_file, sidecar_dir_slash .. filename)
|
2023-09-01 05:07:29 +00:00
|
|
|
os.remove(metadata_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
self:getCustomCoverFile(true) -- reset cache
|
2023-05-31 12:28:07 +00:00
|
|
|
end
|
2023-05-03 12:43:05 +00:00
|
|
|
end
|
2023-09-01 05:07:29 +00:00
|
|
|
-- custom metadata
|
|
|
|
metadata_file = self:getCustomMetadataFile()
|
|
|
|
if metadata_file then
|
|
|
|
filepath, filename = util.splitFilePathName(metadata_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
if filepath ~= sidecar_dir_slash then
|
|
|
|
ffiutil.copyFile(metadata_file, sidecar_dir_slash .. filename)
|
2023-09-01 05:07:29 +00:00
|
|
|
os.remove(metadata_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
self:getCustomMetadataFile(true) -- reset cache
|
2023-09-01 05:07:29 +00:00
|
|
|
end
|
|
|
|
end
|
2023-05-03 12:43:05 +00:00
|
|
|
end
|
|
|
|
|
2023-03-01 12:52:08 +00:00
|
|
|
self:purge(sidecar_file) -- remove old candidates and empty sidecar folders
|
2023-02-17 06:35:49 +00:00
|
|
|
|
2023-05-03 12:43:05 +00:00
|
|
|
return sidecar_dir
|
2014-07-03 12:42:48 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Purges (removes) sidecar directory.
|
2023-09-05 04:42:46 +00:00
|
|
|
function DocSettings:purge(sidecar_to_keep, data_to_purge)
|
|
|
|
local custom_cover_file, custom_metadata_file
|
|
|
|
if sidecar_to_keep == nil then
|
2023-10-31 05:30:39 +00:00
|
|
|
custom_cover_file = self:getCustomCoverFile()
|
2023-09-05 04:42:46 +00:00
|
|
|
custom_metadata_file = self:getCustomMetadataFile()
|
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
if data_to_purge == nil then -- purge all
|
2023-09-05 04:42:46 +00:00
|
|
|
data_to_purge = {
|
|
|
|
doc_settings = true,
|
|
|
|
custom_cover_file = custom_cover_file,
|
|
|
|
custom_metadata_file = custom_metadata_file,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- Remove any of the old ones we may consider as candidates in DocSettings:open()
|
2023-09-05 04:42:46 +00:00
|
|
|
if data_to_purge.doc_settings and self.candidates then
|
2023-02-17 06:35:49 +00:00
|
|
|
for _, t in ipairs(self.candidates) do
|
|
|
|
local candidate_path = t.path
|
2023-10-31 05:30:39 +00:00
|
|
|
if isFile(candidate_path) then
|
2023-02-17 06:35:49 +00:00
|
|
|
if (not sidecar_to_keep)
|
2023-10-11 08:39:33 +00:00
|
|
|
or (candidate_path ~= sidecar_to_keep and candidate_path ~= sidecar_to_keep .. ".old") then
|
2023-02-17 06:35:49 +00:00
|
|
|
os.remove(candidate_path)
|
|
|
|
logger.dbg("DocSettings: purged:", candidate_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-10-21 20:43:05 +00:00
|
|
|
end
|
2023-02-17 06:35:49 +00:00
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
-- Remove custom
|
2023-09-05 04:42:46 +00:00
|
|
|
if data_to_purge.custom_cover_file then
|
|
|
|
os.remove(data_to_purge.custom_cover_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
self:getCustomCoverFile(true) -- reset cache
|
2023-03-01 12:52:08 +00:00
|
|
|
end
|
2023-09-05 04:42:46 +00:00
|
|
|
if data_to_purge.custom_metadata_file then
|
|
|
|
os.remove(data_to_purge.custom_metadata_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
self:getCustomMetadataFile(true) -- reset cache
|
2014-10-07 05:06:52 +00:00
|
|
|
end
|
2023-05-03 12:43:05 +00:00
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
-- Remove empty sidecar dirs
|
2023-09-05 04:42:46 +00:00
|
|
|
if data_to_purge.doc_settings or data_to_purge.custom_cover_file or data_to_purge.custom_metadata_file then
|
2023-10-31 05:30:39 +00:00
|
|
|
for _, dir in ipairs({ self.doc_sidecar_dir, self.dir_sidecar_dir, self.hash_sidecar_dir }) do
|
|
|
|
DocSettings.removeSidecarDir(dir)
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2023-09-05 04:42:46 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
|
2023-10-11 08:39:33 +00:00
|
|
|
DocSettings.setIsHashLocationEnabled(nil) -- reset this in case last hash book is purged
|
2023-05-03 12:43:05 +00:00
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
--- Removes sidecar dir iff empty.
|
|
|
|
function DocSettings.removeSidecarDir(dir)
|
|
|
|
if dir and isDir(dir) then
|
|
|
|
if dir:match("^"..DOCSETTINGS_DIR) or dir:match("^"..DOCSETTINGS_HASH_DIR) then
|
|
|
|
util.removePath(dir) -- remove empty parent folders
|
|
|
|
else
|
|
|
|
os.remove(dir) -- keep parent folders
|
|
|
|
end
|
2023-05-03 12:43:05 +00:00
|
|
|
end
|
2023-02-17 06:35:49 +00:00
|
|
|
end
|
|
|
|
|
2023-05-03 12:43:05 +00:00
|
|
|
--- Updates sdr location for file rename/copy/move/delete operations.
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings.updateLocation(doc_path, new_doc_path, copy)
|
|
|
|
local has_sidecar_file = DocSettings:hasSidecarFile(doc_path)
|
|
|
|
local custom_cover_file = DocSettings:findCustomCoverFile(doc_path)
|
|
|
|
local custom_metadata_file = DocSettings:findCustomMetadataFile(doc_path)
|
|
|
|
if not (has_sidecar_file or custom_cover_file or custom_metadata_file) then return end
|
|
|
|
|
|
|
|
local doc_settings = DocSettings:open(doc_path)
|
|
|
|
local do_purge
|
|
|
|
|
|
|
|
if new_doc_path then -- copy/rename/move
|
|
|
|
if G_reader_settings:readSetting("document_metadata_folder") ~= "hash" then -- keep hash location unchanged
|
|
|
|
local new_sidecar_dir
|
|
|
|
if has_sidecar_file then
|
2023-10-11 08:39:33 +00:00
|
|
|
local new_doc_settings = DocSettings:open(new_doc_path)
|
2023-10-31 05:30:39 +00:00
|
|
|
doc_settings.data.doc_path = new_doc_path
|
|
|
|
new_sidecar_dir = new_doc_settings:flush(doc_settings.data, true) -- without custom
|
2021-10-21 20:43:05 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
if not new_sidecar_dir then
|
|
|
|
new_sidecar_dir = DocSettings:getSidecarDir(new_doc_path)
|
|
|
|
util.makePath(new_sidecar_dir)
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
if custom_cover_file then
|
|
|
|
local _, filename = util.splitFilePathName(custom_cover_file)
|
|
|
|
ffiutil.copyFile(custom_cover_file, new_sidecar_dir .. "/" .. filename)
|
2023-09-01 05:07:29 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
if custom_metadata_file then
|
|
|
|
ffiutil.copyFile(custom_metadata_file, new_sidecar_dir .. "/" .. custom_metadata_filename)
|
|
|
|
end
|
|
|
|
do_purge = not copy
|
2021-10-21 20:43:05 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
else -- delete
|
|
|
|
if has_sidecar_file then
|
|
|
|
local cache_file_path = doc_settings:readSetting("cache_file_path")
|
|
|
|
if cache_file_path then
|
|
|
|
os.remove(cache_file_path)
|
|
|
|
end
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
do_purge = true
|
2023-05-03 12:43:05 +00:00
|
|
|
end
|
2023-09-01 05:07:29 +00:00
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
if do_purge then
|
|
|
|
doc_settings.custom_cover_file = custom_cover_file -- cache
|
|
|
|
doc_settings.custom_metadata_file = custom_metadata_file -- cache
|
|
|
|
doc_settings:purge()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- custom section
|
|
|
|
|
|
|
|
function DocSettings:getCustomLocationCandidates(doc_path)
|
|
|
|
local sidecar_dir
|
|
|
|
local sidecar_file = self:findSidecarFile(doc_path, true) -- new locations only
|
|
|
|
if sidecar_file then -- book was opened, write custom metadata to its sidecar dir
|
|
|
|
sidecar_dir = util.splitFilePathName(sidecar_file):sub(1, -2)
|
|
|
|
return { sidecar_dir }
|
|
|
|
end
|
|
|
|
-- new book, create sidecar dir in accordance with sdr location setting
|
|
|
|
local preferred_location = G_reader_settings:readSetting("document_metadata_folder", "doc")
|
|
|
|
if preferred_location ~= "hash" then
|
|
|
|
sidecar_dir = self:getSidecarDir(doc_path, "dir")
|
|
|
|
if preferred_location == "doc" then
|
|
|
|
local doc_sidecar_dir = self:getSidecarDir(doc_path, "doc")
|
|
|
|
return { doc_sidecar_dir, sidecar_dir } -- fallback for read-only book storage
|
|
|
|
end
|
|
|
|
else -- "hash"
|
|
|
|
sidecar_dir = self:getSidecarDir(doc_path, "hash")
|
2021-10-21 20:43:05 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
return { sidecar_dir }
|
2014-10-07 05:06:52 +00:00
|
|
|
end
|
|
|
|
|
2023-09-01 05:07:29 +00:00
|
|
|
-- custom cover
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
local function findCustomCoverFileInDir(dir)
|
2023-09-01 15:38:43 +00:00
|
|
|
local ok, iter, dir_obj = pcall(lfs.dir, dir)
|
|
|
|
if ok then
|
|
|
|
for f in iter, dir_obj do
|
|
|
|
if util.splitFileNameSuffix(f) == "cover" then
|
|
|
|
return dir .. "/" .. f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-01 05:07:29 +00:00
|
|
|
--- Returns path to book custom cover file if it exists, or nil.
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings:findCustomCoverFile(doc_path)
|
2023-09-01 05:07:29 +00:00
|
|
|
doc_path = doc_path or self.data.doc_path
|
2023-10-31 05:30:39 +00:00
|
|
|
for _, location in ipairs(getOrderedLocationCandidates()) do
|
|
|
|
local sidecar_dir = self:getSidecarDir(doc_path, location)
|
|
|
|
local custom_cover_file = findCustomCoverFileInDir(sidecar_dir)
|
|
|
|
if custom_cover_file then
|
|
|
|
return custom_cover_file
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2023-09-01 05:07:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings:getCustomCoverFile(reset_cache)
|
2023-09-01 05:07:29 +00:00
|
|
|
if reset_cache then
|
2023-10-31 05:30:39 +00:00
|
|
|
self.custom_cover_file = nil
|
2023-09-01 05:07:29 +00:00
|
|
|
else
|
2023-10-31 05:30:39 +00:00
|
|
|
if self.custom_cover_file == nil then -- fill empty cache
|
|
|
|
self.custom_cover_file = self:findCustomCoverFile() or false
|
2023-09-01 05:07:29 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
return self.custom_cover_file
|
2023-09-01 05:07:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:flushCustomCover(doc_path, image_file)
|
2023-10-31 05:30:39 +00:00
|
|
|
local sidecar_dirs = self:getCustomLocationCandidates(doc_path)
|
2023-09-01 05:07:29 +00:00
|
|
|
local new_cover_filename = "/cover." .. util.getFileNameSuffix(image_file):lower()
|
|
|
|
for _, sidecar_dir in ipairs(sidecar_dirs) do
|
|
|
|
util.makePath(sidecar_dir)
|
|
|
|
local new_cover_file = sidecar_dir .. new_cover_filename
|
|
|
|
if ffiutil.copyFile(image_file, new_cover_file) == nil then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- custom metadata
|
|
|
|
|
|
|
|
--- Returns path to book custom metadata file if it exists, or nil.
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings:findCustomMetadataFile(doc_path)
|
2023-09-01 05:07:29 +00:00
|
|
|
doc_path = doc_path or self.data.doc_path
|
2023-10-31 05:30:39 +00:00
|
|
|
for _, location in ipairs(getOrderedLocationCandidates()) do
|
|
|
|
local sidecar_dir = self:getSidecarDir(doc_path, location)
|
|
|
|
local custom_metadata_file = sidecar_dir .. "/" .. custom_metadata_filename
|
|
|
|
if isFile(custom_metadata_file) then
|
|
|
|
return custom_metadata_file
|
2023-09-01 05:07:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
function DocSettings:getCustomMetadataFile(reset_cache)
|
|
|
|
if reset_cache then
|
|
|
|
self.custom_metadata_file = nil
|
2023-09-01 05:07:29 +00:00
|
|
|
else
|
2023-10-31 05:30:39 +00:00
|
|
|
if self.custom_metadata_file == nil then -- fill empty cache
|
|
|
|
self.custom_metadata_file = self:findCustomMetadataFile() or false
|
|
|
|
end
|
|
|
|
return self.custom_metadata_file
|
2023-09-01 05:07:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:flushCustomMetadata(doc_path)
|
2023-10-31 05:30:39 +00:00
|
|
|
local sidecar_dirs = self:getCustomLocationCandidates(doc_path)
|
2023-09-01 05:07:29 +00:00
|
|
|
local s_out = dump(self.data, nil, true)
|
|
|
|
for _, sidecar_dir in ipairs(sidecar_dirs) do
|
|
|
|
util.makePath(sidecar_dir)
|
2023-10-31 05:30:39 +00:00
|
|
|
local new_metadata_file = sidecar_dir .. "/" .. custom_metadata_filename
|
|
|
|
if util.writeToFile(s_out, new_metadata_file, true, true) then
|
|
|
|
return true
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-31 05:30:39 +00:00
|
|
|
-- "hash" section
|
|
|
|
|
|
|
|
-- Returns the list of pairs {sidecar_file, custom_metadata_file}.
|
|
|
|
function DocSettings.findSidecarFilesInHashLocation()
|
|
|
|
local res = {}
|
|
|
|
local callback = function(fullpath, name)
|
|
|
|
if name:match("metadata%..+%.lua$") then
|
|
|
|
local sdr = { fullpath }
|
|
|
|
local custom_metadata_file = fullpath:gsub(name, custom_metadata_filename)
|
|
|
|
if isFile(custom_metadata_file) then
|
|
|
|
table.insert(sdr, custom_metadata_file)
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
table.insert(res, sdr)
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
|
|
|
end
|
2023-10-31 05:30:39 +00:00
|
|
|
util.findFiles(DOCSETTINGS_HASH_DIR, callback)
|
|
|
|
return res
|
2023-10-11 08:39:33 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return DocSettings
|