Skip to content

Commit

Permalink
Fixed windows incompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
alberti42 committed Jul 30, 2024
1 parent 662e357 commit c485c2d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "import-attachments-plus",
"name": "Import Attachments+",
"version": "1.2.6",
"version": "1.3.0",
"minAppVersion": "1.5.0",
"description": "Move and link the attachments into the vault.",
"author": "Andrea Alberti",
Expand Down
10 changes: 5 additions & 5 deletions src/ImportAttachmentsModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class OverwriteChoiceModal extends Modal {
origFileLink.addEventListener('click', (e) => {
e.preventDefault(); // Prevent the default anchor behavior
// Open the folder in the system's default file explorer
window.require('electron').remote.shell.showItemInFolder(this.originalFilePath);
window.require('electron').remote.shell.showItemInFolder(Utils.makePosixPathOScompatible(this.originalFilePath));
});

paragraph.append(' into the vault. However, a ');
Expand All @@ -226,7 +226,7 @@ export class OverwriteChoiceModal extends Modal {
vaultFileLink.addEventListener('click', (e) => {
e.preventDefault(); // Prevent the default anchor behavior
// Open the folder in the system's default file explorer
window.require('electron').remote.shell.showItemInFolder(Utils.joinPaths(this.plugin.vaultPath,this.destFilePath));
window.require('electron').remote.shell.showItemInFolder(Utils.makePosixPathOScompatible(Utils.joinPaths(this.plugin.vaultPath,this.destFilePath)));
});

paragraph.append(' with the same name already exists at the destination location.');
Expand Down Expand Up @@ -309,7 +309,7 @@ export class DeleteAttachmentFolderModal extends Modal {
e.preventDefault(); // Prevent the default anchor behavior
// Open the folder in the system's default file explorer
// window.require('electron').remote.shell.showItemInFolder(this.attachmentFolderPath);
window.require('electron').remote.shell.openPath(Utils.joinPaths(this.plugin.vaultPath,this.attachmentFolderPath));
window.require('electron').remote.shell.openPath(Utils.makePosixPathOScompatible(Utils.joinPaths(this.plugin.vaultPath,this.attachmentFolderPath)));
});
} else {
paragraph.createEl('strong', {text: attachmentFolderPath_parsed.filename});
Expand Down Expand Up @@ -384,7 +384,7 @@ export class ImportFromVaultChoiceModal extends Modal {
fileLink.addEventListener('click', (e) => {
e.preventDefault(); // Prevent the default anchor behavior
// Open the folder in the system's default file explorer
window.require('electron').remote.shell.showItemInFolder(Utils.joinPaths(this.plugin.vaultPath,this.relativeFilePath));
window.require('electron').remote.shell.showItemInFolder(Utils.makePosixPathOScompatible(Utils.joinPaths(this.plugin.vaultPath,this.relativeFilePath)));
});

paragraph.append(' is already stored in the vault.');
Expand Down Expand Up @@ -482,7 +482,7 @@ export class FolderImportErrorModal extends Modal {
fileLink.addEventListener('click', (e) => {
e.preventDefault(); // Prevent the default anchor behavior
// Open the folder in the system's default file explorer
window.require('electron').remote.shell.openPath(folder.path);
window.require('electron').remote.shell.openPath(Utils.makePosixPathOScompatible(folder.path));
});
});

Expand Down
9 changes: 5 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
} from './types';
import * as Utils from "utils";

import { sep, posix } from 'path';

import { promises as fs } from 'fs'; // This imports the promises API from fs

import { patchOpenFile, unpatchOpenFile, addKeyListeners, removeKeyListeners } from 'patchOpenFile';
Expand Down Expand Up @@ -97,7 +99,8 @@ export default class ImportAttachments extends Plugin {
if (!(adapter instanceof FileSystemAdapter)) {
throw new Error("The vault folder could not be determined.");
}
this.vaultPath = adapter.getBasePath();
// Normalize to POSIX-style path
this.vaultPath = adapter.getBasePath().split(sep).join(posix.sep);
} else {
this.vaultPath = "";
}
Expand Down Expand Up @@ -919,9 +922,7 @@ export default class ImportAttachments extends Plugin {

// Open the folder in the system's default file explorer
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { shell } = require('electron');
// window.require('electron').remote.shell.showItemInFolder(attachmentsFolder.attachmentsFolderPath);
shell.openPath(absAttachmentsFolderPath);
require('electron').remote.shell.openPath(Utils.makePosixPathOScompatible(absAttachmentsFolderPath));
}

// Function to insert links to the imported files in the editor
Expand Down
4 changes: 2 additions & 2 deletions src/patchOpenFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let metaKeyPressed = false;
let altKeyPressed = false;
let keyListenersInstalled = false

import { joinPaths } from 'utils';
import { joinPaths, makePosixPathOScompatible } from 'utils';

// Save a reference to the original method for the monkey patch
let originalOpenFile: ((this: WorkspaceLeaf, file: TFile, openState?: OpenViewState)=> Promise<void>) | null = null;
Expand Down Expand Up @@ -108,7 +108,7 @@ function patchOpenFile(plugin: ImportAttachments) {
const newEmptyLeave = this.getViewState()?.type == 'empty';

if(plugin.settings.revealAttachment && metaKeyPressed && altKeyPressed){
window.require('electron').remote.shell.showItemInFolder(joinPaths(plugin.vaultPath,file.path));
window.require('electron').remote.shell.showItemInFolder(makePosixPathOScompatible(joinPaths(plugin.vaultPath,file.path)));
}
else if(plugin.settings.openAttachmentExternal && metaKeyPressed && !altKeyPressed) {
plugin.app.openWithDefaultApp(file.path);
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export function arePathsSameFile(vault: Vault, filePath1: string, filePath2: str
return false;
}

export function makePosixPathOScompatible(posixPath:string): string {
return posixPath.split(path.posix.sep).join(path.sep);
}

export async function hashFile(filePath: string): Promise<string> {
const hash = crypto.createHash('md5');
let fileHandle = null;
Expand Down

0 comments on commit c485c2d

Please sign in to comment.