diff --git a/apps/readest-app/src/utils/book.ts b/apps/readest-app/src/utils/book.ts index 9a8c70bf..2ff372e5 100644 --- a/apps/readest-app/src/utils/book.ts +++ b/apps/readest-app/src/utils/book.ts @@ -251,10 +251,30 @@ const normalizeIdentifier = (identifier: string) => { return identifier; }; +const getPreferredIdentifier = (identifiers: string[] | Identifier[]) => { + for (const scheme of ['uuid', 'calibre', 'isbn']) { + const found = identifiers.find((identifier) => + typeof identifier === 'string' + ? identifier.toLowerCase().includes(scheme) + : identifier.scheme.toLowerCase() === scheme, + ); + if (found) { + return typeof found === 'string' ? normalizeIdentifier(found) : found.value; + } + } + return; +}; + const getIdentifiersList = ( identifiers: undefined | string | string[] | Identifier | Identifier[], ) => { if (!identifiers) return []; + if (Array.isArray(identifiers)) { + const preferred = getPreferredIdentifier(identifiers); + if (preferred) { + return [preferred]; + } + } return Array.isArray(identifiers) ? identifiers .map((identifier) => @@ -267,10 +287,15 @@ const getIdentifiersList = ( }; export const getMetadataHash = (metadata: BookMetadata) => { - const title = getTitleForHash(metadata.title); - const authors = getAuthorsList(metadata.author).join(','); - const identifiers = getIdentifiersList(metadata.altIdentifier || metadata.identifier).join(','); - const hashSource = `${title}|${authors}|${identifiers}`; - const metaHash = md5(hashSource.normalize('NFC')); - return metaHash; + try { + const title = getTitleForHash(metadata.title); + const authors = getAuthorsList(metadata.author).join(','); + const identifiers = getIdentifiersList(metadata.altIdentifier || metadata.identifier).join(','); + const hashSource = `${title}|${authors}|${identifiers}`; + const metaHash = md5(hashSource.normalize('NFC')); + return metaHash; + } catch (error) { + console.error('Error generating metadata hash:', error); + } + return; }; diff --git a/apps/readest.koplugin/main.lua b/apps/readest.koplugin/main.lua index f90b1918..924c093a 100644 --- a/apps/readest.koplugin/main.lua +++ b/apps/readest.koplugin/main.lua @@ -312,11 +312,25 @@ function ReadestSync:generateMetadataHash() local identifiers = doc_props.identifiers or '' if identifiers:find("\n") then - identifiers = util.splitToArray(identifiers, "\n") - for i, id in ipairs(identifiers) do - identifiers[i] = normalizeIdentifier(id) + local list = util.splitToArray(identifiers, "\n") + local normalized = {} + local priorities = { "uuid", "calibre", "isbn" } + local preferred = nil + for i, id in ipairs(list) do + normalized[i] = normalizeIdentifier(id) + local candidate = id:lower() + for _, p in ipairs(priorities) do + if candidate:find(p, 1, true) then + preferred = normalized[i] + break + end + end + end + if preferred then + identifiers = preferred + else + identifiers = table.concat(normalized, ",") end - identifiers = table.concat(identifiers, ",") else identifiers = normalizeIdentifier(identifiers) end @@ -327,10 +341,10 @@ end function ReadestSync:getMetaHash() local doc_readest_sync = self.ui.doc_settings:readSetting("readest_sync") or {} - local meta_hash = doc_readest_sync.meta_hash + local meta_hash = doc_readest_sync.meta_hash_v1 if not meta_hash then meta_hash = self:generateMetadataHash() - doc_readest_sync.meta_hash = meta_hash + doc_readest_sync.meta_hash_v1 = meta_hash self.ui.doc_settings:saveSetting("readest_sync", doc_readest_sync) end return meta_hash