From 7e7e8b88a2a93822ec97c1d63c321541fbf11028 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Thu, 5 Jan 2023 15:07:10 -0500 Subject: [PATCH] LABD file relinking: Process using Unix paths (#2961) --- chrome/content/zotero/zoteroPane.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index fd83ca49ce..c4f6f2882a 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -5497,6 +5497,11 @@ var ZoteroPane = new function() * @return {Promise} True if relinked successfully or canceled */ this.checkForLinkedFilesToRelink = async function (item) { + // Naive split and join implementations that split on any separator and join using forward slashes + // OS.Path methods have different behavior depending on the platform, and a naive approach is good enough here + let split = path => path.split(/[/\\]/); + let join = (...segments) => segments.join('/'); + Zotero.debug('Attempting to relink automatically'); let basePath = Zotero.Prefs.get('baseAttachmentPath'); @@ -5504,6 +5509,7 @@ var ZoteroPane = new function() Zotero.debug('No LABD'); return false; } + basePath = Zotero.File.normalizeToUnix(basePath); Zotero.debug('LABD path: ' + basePath); let syncedPath = item.getFilePath(); @@ -5531,15 +5537,20 @@ var ZoteroPane = new function() unNormalizedDirname = unNormalizedDirname.substring(0, lastSlash + 1); } - let parts = OS.Path.split(syncedPath).components; + let parts = split(syncedPath); for (let segmentsToDrop = 0; segmentsToDrop < parts.length; segmentsToDrop++) { - let correctedPath = OS.Path.join(basePath, ...parts.slice(segmentsToDrop)); + let correctedPath = join(basePath, ...parts.slice(segmentsToDrop)); if (!(await OS.File.exists(correctedPath))) { Zotero.debug('Does not exist: ' + correctedPath); continue; } Zotero.debug('Exists! ' + correctedPath); + + if (Zotero.isWin) { + correctedPath = correctedPath.replace(/\//g, '\\'); + Zotero.debug('Converted back to Windows path: ' + correctedPath); + } let otherUnlinked = await Zotero.Items.findMissingLinkedFiles( item.libraryID, @@ -5548,13 +5559,15 @@ var ZoteroPane = new function() let othersToRelink = new Map(); for (let otherItem of otherUnlinked) { if (otherItem.id === item.id) continue; - let otherParts = otherItem.getFilePath() - .split(/[/\\]/) + let otherParts = split(otherItem.getFilePath()) // Slice as much off the beginning as when creating correctedPath .slice(segmentsToDrop); if (!otherParts.length) continue; - let otherCorrectedPath = OS.Path.join(basePath, ...otherParts); + let otherCorrectedPath = join(basePath, ...otherParts); if (await OS.File.exists(otherCorrectedPath)) { + if (Zotero.isWin) { + otherCorrectedPath = otherCorrectedPath.replace(/\//g, '\\'); + } othersToRelink.set(otherItem, otherCorrectedPath); } }