-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
120 lines (102 loc) · 3.13 KB
/
Copy pathmain.js
File metadata and controls
120 lines (102 loc) · 3.13 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
const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron');
const path = require('path');
const fs = require('fs').promises;
const fsSync = require('fs');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
backgroundColor: '#1e1e1e'
});
mainWindow.loadFile('index.html');
mainWindow.setMenuBarVisibility(false);
}
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
// IPC Handlers
ipcMain.handle('load-config', async () => {
try {
const configPath = path.join(__dirname, 'config.json');
const data = await fs.readFile(configPath, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error('Error loading config:', error);
return null;
}
});
ipcMain.handle('save-config', async (event, config) => {
try {
const configPath = path.join(__dirname, 'config.json');
await fs.writeFile(configPath, JSON.stringify(config, null, 2));
return { success: true };
} catch (error) {
console.error('Error saving config:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('load-images', async (event, folderPath) => {
try {
const files = await fs.readdir(folderPath);
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'];
const imageFiles = files.filter(file => {
const ext = path.extname(file).toLowerCase();
return imageExtensions.includes(ext);
}).map(file => path.join(folderPath, file));
return imageFiles;
} catch (error) {
console.error('Error loading images:', error);
return [];
}
});
ipcMain.handle('move-file', async (event, sourcePath, destFolder) => {
try {
// Create destination folder if it doesn't exist
if (!fsSync.existsSync(destFolder)) {
await fs.mkdir(destFolder, { recursive: true });
}
const fileName = path.basename(sourcePath);
const destPath = path.join(destFolder, fileName);
// Check if file already exists
if (fsSync.existsSync(destPath)) {
return {
success: false,
error: 'File already exists in destination folder'
};
}
await fs.rename(sourcePath, destPath);
return { success: true, newPath: destPath };
} catch (error) {
console.error('Error moving file:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('select-folder', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory']
});
if (!result.canceled && result.filePaths.length > 0) {
return result.filePaths[0];
}
return null;
});
ipcMain.handle('delete-file', async (event, filePath) => {
try {
await shell.trashItem(filePath);
return { success: true };
} catch (error) {
console.error('Error deleting file:', error);
return { success: false, error: error.message };
}
});