From c1dcd9244c859796937a46aea78bedaacb0835f9 Mon Sep 17 00:00:00 2001 From: mgmeyers Date: Thu, 27 Jul 2023 11:11:01 -0700 Subject: [PATCH] Check if image exists before attempting to copy --- manifest.json | 2 +- src/bbt/export.ts | 14 ++++++++++---- src/bbt/exportNotes.ts | 26 ++++++++++++++++++++------ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/manifest.json b/manifest.json index 4737d9d..6787cb4 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-zotero-desktop-connector", "name": "Zotero Integration", - "version": "3.0.6", + "version": "3.0.7", "minAppVersion": "1.1.1", "description": "Insert and import citations, bibliographies, notes, and PDF annotations from Zotero.", "author": "mgmeyers", diff --git a/src/bbt/export.ts b/src/bbt/export.ts index 1a46679..346bf26 100644 --- a/src/bbt/export.ts +++ b/src/bbt/export.ts @@ -148,11 +148,17 @@ function convertNativeAnnotation( mkdirSync(imageOutputPath, { recursive: true }); } + let input = path.join(parsed.dir, `${annotation.key}${parsed.ext}`); try { - copyFileSync( - path.join(parsed.dir, `${annotation.key}${parsed.ext}`), - imagePath - ); + if (!existsSync(input)) { + const origInput = input; + input = annotation.annotationImagePath; + if (!existsSync(input)) { + throw new Error('Cannot find annotation image: ' + origInput); + } + } + + copyFileSync(input, imagePath); } catch (e) { new Notice( 'Error: unable to copy annotation image from Zotero into your vault', diff --git a/src/bbt/exportNotes.ts b/src/bbt/exportNotes.ts index 328d000..b3c6498 100644 --- a/src/bbt/exportNotes.ts +++ b/src/bbt/exportNotes.ts @@ -1,4 +1,4 @@ -import { copyFileSync } from 'fs'; +import { copyFileSync, existsSync, mkdirSync } from 'fs'; import { Editor, Notice, @@ -44,16 +44,30 @@ export async function processZoteroAnnotationNotes( if (imagePath) { const parsed = path.parse(imagePath); const destPath = await getAvailablePathForAttachments( - parsed.name, + annotationKey, parsed.ext.slice(1), destination ); + const output = path.parse(path.join(getVaultRoot(), destPath)); + const imageOutputPath = output.dir; + + if (!existsSync(imageOutputPath)) { + mkdirSync(imageOutputPath, { recursive: true }); + } + + let input = path.join(parsed.dir, `${annotationKey}${parsed.ext}`); + try { - copyFileSync( - path.join(parsed.dir, `${annotationKey}${parsed.ext}`), - path.join(getVaultRoot(), destPath) - ); + if (!existsSync(input)) { + const origInput = input; + input = imagePath; + if (!existsSync(input)) { + throw new Error('Cannot find annotation image: ' + origInput); + } + } + + copyFileSync(input, path.join(getVaultRoot(), destPath)); } catch (e) { new Notice( 'Error: unable to copy annotation image from Zotero into your vault',