super-productivity/electron/ipc-handler-wrapper.ts
Pue-Tsuâ 9a9e31f36a
Adding Ctrl-V pasting function from clipboard. (#5998)
* docs(clipboard): Added the plan to guide AI implementation.

* feat(clipboard): Changed requirement to reflect the fact that some functions can't be done in browsers.

* feat(clipboard): Added Ctrl-v pasting functionality from clipboard.

- Storing image in IndexedDB in browser, and storing image in userdata path in Electron.
- Added image resizing in markdown.

* docs(clipboard): Removed requirement file since it's implemented.

* feat(clipboard): Added clipboard images management in settings.

* feat(tests): Enhance focus mode and inline markdown tests with mock store and actions

* feat(clipboard): Add electron-only annotation to findImageFile function

* feat(clipboard): Prevent memory leaks by revoking cached blob URLs on image deletion

* feat(clipboard): Improve paste handling by tracking current placeholder and cleaning up old progress

* Merge branch 'master' into feat/clipboard-files

* feat(marked-options): Refactor renderer methods and add hooks for image sizing syntax

* feat(clipboard): Added feature that pasted image will be added to task attachment.

* revert: Revert the change of lock file from last merge.

* revert: Removed unwanted changes.

* refactor(clipboard): Cleaned up code that seems not necessary.

* feat(clipboard): Handle storage quota exceeded error and update messages

* feat(clipboard): Implement validated IPC handlers for clipboard image operations

* feat(clipboard): Add error handling for clipboard image URL resolution

* feat(clipboard): Refactor file operations to use async fs promises

* feat(clipboard): Integrate ClipboardPasteHandlerService for improved paste handling

* feat(clipboard): Rename resolveUrl to resolveIndexedDbUrl for clarity and update references

* feat(clipboard): Add defaultPath option to showOpenDialog for improved user experience

* feat(clipboard): Implement getDefaultClipboardImagesPath utility for consistent image path retrieval

* feat(clipboard): Refactor MIME type handling to use centralized mapping for improved maintainability

* feat(clipboard): Add computed property to toggle markdown parsing based on formatting settings

* revert: Removed unwanted changes.

* revert: Removed unwanted chagnes.

* revert: Removed unwanted chagnes.

* fix(clipboard-images-cfg): remove debug log from selectImagePath method

* refactor(paste-handler): update currentPlaceholder to use getter/setter methods

* fix(docs): correct formatting and improve clarity in multiple wiki pages

* fix: update dialog handling in initLocalFileSyncAdapter for type safety

* revert: Revert space change.

* fix(inline-markdown): ensure model is set to an empty string instead of undefined

* fix(tests): update clipboard images section locator to use collapsible title
2026-02-04 15:46:51 +01:00

43 lines
1.3 KiB
TypeScript

import { app } from 'electron';
import * as path from 'path';
/**
* Validates if the given path is within the userData directory.
*/
export const validatePathInUserData = (basePath: string): boolean => {
const userDataPath = app.getPath('userData');
const resolvedBasePath = path.resolve(basePath);
const resolvedUserDataPath = path.resolve(userDataPath);
return resolvedBasePath.startsWith(resolvedUserDataPath);
};
/**
* Creates a validated IPC handler with consistent error handling and optional path validation.
*/
export const createValidatedHandler = <TArgs extends object, TResult>(
handler: (args: TArgs) => Promise<TResult>,
options?: {
validatePath?: boolean;
errorValue?: TResult;
},
): ((event: Electron.IpcMainInvokeEvent, args: TArgs) => Promise<TResult>) => {
return async (_, args: TArgs) => {
try {
// Add path validation if requested
if (options?.validatePath && 'basePath' in args) {
const basePath = (args as any).basePath;
if (!validatePathInUserData(basePath)) {
throw new Error('Invalid base path');
}
}
return await handler(args);
} catch (error) {
console.error(`IPC handler error:`, error);
if (options?.errorValue !== undefined) {
return options.errorValue;
}
throw error;
}
};
};