feat: add moveToTrash for external-attachment plugin

This commit is contained in:
lin onetwo 2025-05-05 12:41:23 +08:00
parent 1aecc6094f
commit e89d70677b
2 changed files with 22 additions and 1 deletions

View file

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
/* eslint-disable @typescript-eslint/require-await */
import { app, dialog, ipcMain, MessageBoxOptions, shell } from 'electron';
import fs from 'fs-extra';
@ -278,6 +277,21 @@ ${message.message}
}
}
public async moveToTrash(filePath: string): Promise<boolean> {
if (!filePath) {
logger.error('NativeService.moveToTrash() filePath is empty', { filePath });
return false;
}
logger.debug(`NativeService.moveToTrash() Moving to trash: ${filePath}`);
try {
await shell.trashItem(filePath);
return true;
} catch (error) {
logger.error('NativeService.moveToTrash() failed', { error, filePath });
return false;
}
}
public formatFileUrlToAbsolutePath(urlWithFileProtocol: string): string {
logger.info('getting url', { url: urlWithFileProtocol, function: 'formatFileUrlToAbsolutePath' });
let pathname = '';

View file

@ -70,6 +70,12 @@ export interface INativeService {
path(method: 'basename' | 'dirname' | 'join', pathString: string | undefined, ...paths: string[]): Promise<string | undefined>;
pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise<string[]>;
pickFile(filters?: Electron.OpenDialogOptions['filters']): Promise<string[]>;
/**
* Move a file or directory to the trash bin.
* @param filePath The absolute path of the file or directory to move to the trash.
* @returns A promise that resolves to true if the operation was successful, false otherwise.
*/
moveToTrash(filePath: string): Promise<boolean>;
quit(): void;
showElectronMessageBox(options: Electron.MessageBoxOptions, windowName?: WindowNames): Promise<Electron.MessageBoxReturnValue | undefined>;
/**
@ -92,6 +98,7 @@ export const NativeServiceIPCDescriptor = {
log: ProxyPropertyType.Function,
mkdir: ProxyPropertyType.Function,
movePath: ProxyPropertyType.Function,
moveToTrash: ProxyPropertyType.Function,
open: ProxyPropertyType.Function,
openInEditor: ProxyPropertyType.Function,
openInGitGuiApp: ProxyPropertyType.Function,