-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron.js
More file actions
117 lines (99 loc) · 2.91 KB
/
Copy pathelectron.js
File metadata and controls
117 lines (99 loc) · 2.91 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const isDev = process.env.NODE_ENV === 'development';
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
},
backgroundColor: '#1a1a2e'
});
const startURL = isDev
? 'http://localhost:3000'
: `file://${path.join(__dirname, 'index.html')}`;
mainWindow.loadURL(startURL);
if (isDev) {
mainWindow.webContents.openDevTools();
}
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.whenReady().then(() => {
createWindow();
// Get audio files from the audio directory
ipcMain.handle('get-audio-files', async () => {
try {
const audioPath = isDev
? path.join(__dirname, 'public/audio')
: path.join(process.resourcesPath, 'audio');
if (!fs.existsSync(audioPath)) {
fs.mkdirSync(audioPath, { recursive: true });
}
const files = fs.readdirSync(audioPath);
const audioFiles = files
.filter(file => {
const ext = path.extname(file).toLowerCase();
return ['.mp3', '.wav', '.ogg', '.m4a', '.flac'].includes(ext);
})
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
.map(file => ({
name: file,
path: path.join(audioPath, file)
}));
return audioFiles;
} catch (error) {
console.error('Error reading audio files:', error);
return [];
}
});
// Select custom audio folder
ipcMain.handle('select-audio-folder', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory']
});
if (!result.canceled && result.filePaths.length > 0) {
return result.filePaths[0];
}
return null;
});
// Get files from custom folder
ipcMain.handle('get-audio-files-from-path', async (event, folderPath) => {
try {
if (!fs.existsSync(folderPath)) {
return [];
}
const files = fs.readdirSync(folderPath);
const audioFiles = files
.filter(file => {
const ext = path.extname(file).toLowerCase();
return ['.mp3', '.wav', '.ogg', '.m4a', '.flac'].includes(ext);
})
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
.map(file => ({
name: file,
path: path.join(folderPath, file)
}));
return audioFiles;
} catch (error) {
console.error('Error reading audio files from path:', error);
return [];
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});