DocSettings: Minor cleanup following #10861

Allows me to fix an old pet-peeve of mine re: the terrible variable
names used in there ;o).
reviewable/pr10880/r1
NiLuJe 9 months ago
parent f46f341b9b
commit ba5c7df0db

@ -225,12 +225,12 @@ function DocSettings:open(doc_path)
return new return new
end end
function DocSettings.writeFile(f_out, s_out) local function writeToFile(data, file)
f_out:write("-- we can read Lua syntax here!\nreturn ") file:write("-- we can read Lua syntax here!\nreturn ")
f_out:write(s_out) file:write(data)
f_out:write("\n") file:write("\n")
ffiutil.fsyncOpenedFile(f_out) -- force flush to the storage device ffiutil.fsyncOpenedFile(file) -- force flush to the storage device
f_out:close() file:close()
end end
--- Serializes settings and writes them to `metadata.lua`. --- Serializes settings and writes them to `metadata.lua`.
@ -262,7 +262,7 @@ function DocSettings:flush(data, no_custom_metadata)
logger.dbg("DocSettings: Writing to", sidecar_file) logger.dbg("DocSettings: Writing to", sidecar_file)
local f_out = io.open(sidecar_file, "w") local f_out = io.open(sidecar_file, "w")
if f_out ~= nil then if f_out ~= nil then
DocSettings.writeFile(f_out, s_out) writeToFile(s_out, f_out)
if directory_updated then if directory_updated then
-- Ensure the file renaming is flushed to storage device -- Ensure the file renaming is flushed to storage device
@ -413,31 +413,31 @@ end
-- custom cover -- custom cover
local function findCoverFileInDir(dir)
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
--- Returns path to book custom cover file if it exists, or nil. --- Returns path to book custom cover file if it exists, or nil.
function DocSettings:findCoverFile(doc_path) function DocSettings:findCoverFile(doc_path)
doc_path = doc_path or self.data.doc_path doc_path = doc_path or self.data.doc_path
local location = G_reader_settings:readSetting("document_metadata_folder", "doc") local location = G_reader_settings:readSetting("document_metadata_folder", "doc")
local sidecar_dir = self:getSidecarDir(doc_path, location) local sidecar_dir = self:getSidecarDir(doc_path, location)
local cover_file = DocSettings._findCoverFileInDir(sidecar_dir) local cover_file = findCoverFileInDir(sidecar_dir)
if not cover_file then if not cover_file then
location = location == "doc" and "dir" or "doc" location = location == "doc" and "dir" or "doc"
sidecar_dir = self:getSidecarDir(doc_path, location) sidecar_dir = self:getSidecarDir(doc_path, location)
cover_file = DocSettings._findCoverFileInDir(sidecar_dir) cover_file = findCoverFileInDir(sidecar_dir)
end end
return cover_file return cover_file
end end
function DocSettings._findCoverFileInDir(dir)
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
function DocSettings:getCoverFile(reset_cache) function DocSettings:getCoverFile(reset_cache)
if reset_cache then if reset_cache then
self.cover_file = nil self.cover_file = nil
@ -512,7 +512,7 @@ function DocSettings:flushCustomMetadata(doc_path)
util.makePath(sidecar_dir) util.makePath(sidecar_dir)
local f_out = io.open(sidecar_dir .. "/" .. custom_metadata_filename, "w") local f_out = io.open(sidecar_dir .. "/" .. custom_metadata_filename, "w")
if f_out ~= nil then if f_out ~= nil then
DocSettings.writeFile(f_out, s_out) writeToFile(s_out, f_out)
new_sidecar_dir = sidecar_dir .. "/" new_sidecar_dir = sidecar_dir .. "/"
break break
end end

Loading…
Cancel
Save