md5: centralize and deduplicate (#11003)

Document partial md5 hash is calculated by util.partialMD5() and stored in doc_settings as "partial_md5_checksum" on the first document opening.
This commit is contained in:
hius07
2023-10-15 07:47:09 +03:00
committed by GitHub
parent e9051353a2
commit 2ed2c2c23d
8 changed files with 47 additions and 104 deletions

View File

@@ -20,7 +20,7 @@ local DOCSETTINGS_HASH_DIR = DataStorage:getDocSettingsHashDir()
local custom_metadata_filename = "custom_metadata.lua"
local is_hash_location_enabled
local hash_path_cache = {}
local doc_hash_cache = {}
function DocSettings.isHashLocationEnabled()
if is_hash_location_enabled == nil then
@@ -93,13 +93,13 @@ function DocSettings:getSidecarDir(doc_path, force_location)
if location == "dir" then
path = DOCSETTINGS_DIR .. path
elseif location == "hash" then
local hsh = hash_path_cache[doc_path]
local hsh = doc_hash_cache[doc_path]
if not hsh then
local file = io.open(doc_path, 'rb')
if not file then return path .. ".sdr" end
hsh = util.partialMD5(file)
file:close()
hash_path_cache[doc_path] = hsh
hsh = util.partialMD5(doc_path)
if not hsh then -- fallback to "doc"
return path .. ".sdr"
end
doc_hash_cache[doc_path] = hsh
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)