From 1c70e53006084d22f9e6c8cd376804f78b546581 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Tue, 3 Dec 2024 16:23:00 -0500 Subject: [PATCH] Fix several automatic relinking regressions due to PathUtils (#4887) --- chrome/content/zotero/zoteroPane.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index c89b155f7b..9e4390cda1 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -5015,7 +5015,14 @@ var ZoteroPane = new function() ); return; } - let fileExists = await IOUtils.exists(path); + let fileExists; + try { + fileExists = await IOUtils.exists(path); + } + catch (e) { + Zotero.logError(e); + fileExists = false; + } // If the file is an evicted iCloud Drive file, launch that to trigger a download. // As of 10.13.6, launching an .icloud file triggers the download and opens the @@ -6080,10 +6087,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 + const PATH_SEP = Zotero.isWin ? '\\' : '/'; + + // Split on any separator, join with the platform separator for PathUtils let split = path => path.split(/[/\\]/); - let join = (...segments) => segments.join('/'); + let join = (base, ...segments) => [base.replace(/\//g, PATH_SEP), ...segments].join(PATH_SEP); Zotero.debug('Attempting to relink automatically'); @@ -6140,11 +6148,6 @@ var ZoteroPane = new function() throw e; } 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, @@ -6159,9 +6162,6 @@ var ZoteroPane = new function() if (!otherParts.length) continue; let otherCorrectedPath = join(basePath, ...otherParts); if (await IOUtils.exists(otherCorrectedPath)) { - if (Zotero.isWin) { - otherCorrectedPath = otherCorrectedPath.replace(/\//g, '\\'); - } othersToRelink.set(otherItem, otherCorrectedPath); } } @@ -6173,8 +6173,9 @@ var ZoteroPane = new function() return true; case 'all': await item.relinkAttachmentFile(correctedPath); - await Promise.all([...othersToRelink] - .map(([i, p]) => i.relinkAttachmentFile(p))); + for (let [otherItem, otherCorrectedPath] of othersToRelink) { + await otherItem.relinkAttachmentFile(otherCorrectedPath); + } return true; case 'manual': await this.relinkAttachment(item.id);