Skip to content

Commit

Permalink
Check if image exists before attempting to copy
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Jul 27, 2023
1 parent 05f9bfa commit c1dcd92
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 10 additions & 4 deletions src/bbt/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
26 changes: 20 additions & 6 deletions src/bbt/exportNotes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyFileSync } from 'fs';
import { copyFileSync, existsSync, mkdirSync } from 'fs';
import {
Editor,
Notice,
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit c1dcd92

Please sign in to comment.