-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain-fs-ipc.js
More file actions
166 lines (152 loc) · 5.74 KB
/
main-fs-ipc.js
File metadata and controls
166 lines (152 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* IPC handlers for electronFSAPI
* Preload location: contextBridge.exposeInMainWorld('electronFSAPI', { ... })
*
* NOTE: This file is copied from phoenix-fs library. Do not modify without
* updating the source library. Only add new Phoenix-specific handlers to main-window-ipc.js.
*/
const { ipcMain, dialog, BrowserWindow } = require('electron');
const path = require('path');
const fsp = require('fs/promises');
const os = require('os');
const { identifier: APP_IDENTIFIER } = require('./config');
const { assertTrusted } = require('./ipc-security');
// Electron IPC only preserves Error.message when errors cross the IPC boundary (see
// https://github.com/electron/electron/issues/24427). To preserve error.code for FS
// operations, we catch errors and return them as plain objects {error: {code, message}}.
// The preload layer unwraps these back into proper Error objects.
function fsResult(promise) {
return promise.catch(err => {
return { __fsError: true, code: err.code, message: err.message };
});
}
/**
* Returns the app's local data directory path with trailing separator.
* Matches Tauri's appLocalDataDir which uses the bundle identifier.
* - Linux: ~/.local/share/{APP_IDENTIFIER}/
* - macOS: ~/Library/Application Support/{APP_IDENTIFIER}/
* - Windows: %LOCALAPPDATA%/{APP_IDENTIFIER}/
*/
function getAppDataDir() {
const home = os.homedir();
let appDataDir;
switch (process.platform) {
case 'darwin':
appDataDir = path.join(home, 'Library', 'Application Support', APP_IDENTIFIER);
break;
case 'win32':
appDataDir = path.join(process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), APP_IDENTIFIER);
break;
default:
appDataDir = path.join(process.env.XDG_DATA_HOME || path.join(home, '.local', 'share'), APP_IDENTIFIER);
}
return appDataDir + path.sep;
}
function registerFsIpcHandlers() {
// Directory APIs
ipcMain.handle('get-documents-dir', (event) => {
assertTrusted(event);
// Match Tauri's documentDir which ends with a trailing slash
return path.join(os.homedir(), 'Documents') + path.sep;
});
ipcMain.handle('get-home-dir', (event) => {
assertTrusted(event);
// Match Tauri's homeDir which ends with a trailing slash
const home = os.homedir();
return home.endsWith(path.sep) ? home : home + path.sep;
});
ipcMain.handle('get-temp-dir', (event) => {
assertTrusted(event);
return os.tmpdir();
});
ipcMain.handle('get-app-data-dir', (event) => {
assertTrusted(event);
return getAppDataDir();
});
// Get Windows drive letters (returns null on non-Windows platforms)
ipcMain.handle('get-windows-drives', async (event) => {
assertTrusted(event);
if (process.platform !== 'win32') {
return null;
}
// On Windows, check which drive letters exist by testing A-Z
const drives = [];
for (let i = 65; i <= 90; i++) { // A-Z
const letter = String.fromCharCode(i);
const drivePath = `${letter}:\\`;
try {
await fsp.access(drivePath);
drives.push(letter);
} catch {
// Drive doesn't exist
}
}
return drives.length > 0 ? drives : null;
});
// Dialogs
ipcMain.handle('show-open-dialog', async (event, options) => {
assertTrusted(event);
const win = BrowserWindow.fromWebContents(event.sender);
const result = await dialog.showOpenDialog(win, options);
return result.filePaths;
});
ipcMain.handle('show-save-dialog', async (event, options) => {
assertTrusted(event);
const win = BrowserWindow.fromWebContents(event.sender);
const result = await dialog.showSaveDialog(win, options);
return result.filePath;
});
// FS operations
ipcMain.handle('fs-readdir', async (event, dirPath) => {
assertTrusted(event);
return fsResult(
fsp.readdir(dirPath, { withFileTypes: true })
.then(entries => entries.map(e => ({ name: e.name, isDirectory: e.isDirectory() })))
);
});
ipcMain.handle('fs-stat', async (event, filePath) => {
assertTrusted(event);
return fsResult(
fsp.stat(filePath).then(stats => ({
isFile: stats.isFile(),
isDirectory: stats.isDirectory(),
isSymbolicLink: stats.isSymbolicLink(),
size: stats.size,
mode: stats.mode,
ctimeMs: stats.ctimeMs,
atimeMs: stats.atimeMs,
mtimeMs: stats.mtimeMs,
nlink: stats.nlink,
dev: stats.dev
}))
);
});
ipcMain.handle('fs-mkdir', (event, dirPath, options) => {
assertTrusted(event);
return fsResult(fsp.mkdir(dirPath, options));
});
ipcMain.handle('fs-unlink', (event, filePath) => {
assertTrusted(event);
return fsResult(fsp.unlink(filePath));
});
ipcMain.handle('fs-rmdir', (event, dirPath, options) => {
assertTrusted(event);
return fsResult(fsp.rm(dirPath, options));
});
ipcMain.handle('fs-rename', (event, oldPath, newPath) => {
assertTrusted(event);
return fsResult(fsp.rename(oldPath, newPath));
});
ipcMain.handle('fs-read-file', (event, filePath) => {
assertTrusted(event);
return fsResult(fsp.readFile(filePath));
});
ipcMain.handle('fs-write-file', (event, filePath, data) => {
assertTrusted(event);
return fsResult(fsp.writeFile(filePath, Buffer.from(data)));
});
}
module.exports = {
registerFsIpcHandlers,
getAppDataDir
};