-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
130 lines (109 loc) · 3.77 KB
/
Copy pathmain.js
File metadata and controls
130 lines (109 loc) · 3.77 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
const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron');
const path = require('path');
const fs = require('fs');
const createWindow = () => {
const mainWindow = new BrowserWindow({
width: 900,
height: 700,
minWidth: 800,
minHeight: 600,
frame: false,
backgroundColor: '#000000',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
});
mainWindow.loadFile('index.html');
mainWindow.on('maximize', () => mainWindow.webContents.send('window-state-changed', true));
mainWindow.on('unmaximize', () => mainWindow.webContents.send('window-state-changed', false));
};
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Window Controls
ipcMain.on('window-minimize', () => {
BrowserWindow.getFocusedWindow()?.minimize();
});
ipcMain.on('window-maximize', () => {
const window = BrowserWindow.getFocusedWindow();
if (window?.isMaximized()) {
window.unmaximize();
} else {
window?.maximize();
}
});
ipcMain.on('window-close', () => {
BrowserWindow.getFocusedWindow()?.close();
});
const MASTER_EXTENSION = '.sCrypt';
// Handle file open dialog
ipcMain.handle('dialog:openFile', async (event, operation) => {
const filters = (operation === 'decrypt')
? [{ name: 'Encrypted Files', extensions: [MASTER_EXTENSION.substring(1)] }]
: [];
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ['openFile', 'multiSelections'],
filters: filters
});
if (canceled) {
return [];
} else {
return filePaths;
}
});
// Handle file processing
ipcMain.handle('process-files', async (event, { filePaths, destinationDir, operation }) => {
const results = [];
for (const filePath of filePaths) {
try {
const parsedPath = path.parse(filePath);
let newPath;
if (operation === 'decrypt') {
if (!parsedPath.base.endsWith(MASTER_EXTENSION)) {
throw new Error('Not a valid encrypted file.');
}
const parts = parsedPath.name.split('.');
if (parts.length < 2) throw new Error('Invalid encrypted filename format.');
const encodedExt = parts.pop();
const originalNameBase = parts.join('.');
const originalExt = Buffer.from(encodedExt, 'base64').toString('utf-8');
newPath = path.join(destinationDir, `${originalNameBase}.${originalExt}`);
} else { // Encrypt
const originalExt = parsedPath.ext.substring(1); // remove dot
const encodedExt = Buffer.from(originalExt).toString('base64');
newPath = path.join(destinationDir, `${parsedPath.name}.${encodedExt}${MASTER_EXTENSION}`);
}
fs.copyFileSync(filePath, newPath);
results.push({ success: true, path: newPath });
} catch (error) {
results.push({ success: false, path: filePath, error: error.message });
}
}
return results;
});
// Handle 'Select Directory' dialog for export
ipcMain.handle('dialog:selectDirectory', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
title: '选择导出文件夹',
properties: ['openDirectory']
});
if (canceled || filePaths.length === 0) {
return null;
}
return filePaths[0]; // Return the selected directory path
});
ipcMain.on('shell:openExternal', (event, url) => {
shell.openExternal(url);
});