-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
150 lines (131 loc) · 3.98 KB
/
main.js
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
const os = require('os');
const fs = require('fs');
const url = require('url');
const path = require('path');
const mm = require('musicmetadata');
const scanner = require('readdirp');
const electron = require('electron');
const drivelist = require('drivelist');
const Config = require('electron-config');
let mainWindow = null;
let app = electron.app;
let ipc = electron.ipcMain;
let dialog = electron.dialog;
let BrowserWindow = electron.BrowserWindow;
let config = new Config({
name: 'electron-player-user-preferences',
pathList: []
});
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
height: 900,
width: 1600,
frame: true,
resizable: true,
transparent: false,
fullscreen: false,
webPreferences: {
nodeIntegration: true
},
icon: __dirname + '/dist/public/images/player-icon-4.png'
});
// and load the index.html of the app.
//mainWindow.loadURL('http://localhost:8080');
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, '/dist/index.html'),
protocol: 'file:',
slashes: true
}));
//mainWindow.webContents.openDevTools();
mainWindow.on('closed', function () {
mainWindow = null
});
}
app.on('ready', createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
/*********************************** IPC Section *****************************************/
let handleDialogOpenRequest = (event) => {
dialog.showOpenDialog(dialogConfigs, handleFile);
function handleFile(files) {
if(files) {
let path = files.toString();
handleMetadata(path, function (metadata) {
let track = buildTrackInfo({fullPath: path}, metadata);
event.sender.send('selected-track', track);
});
}
}
};
let handleMetadata = (path, callback) => {
let readableStream = fs.createReadStream(path);
let parser = mm(readableStream, function (err, metadata) {
if (err) {
//console.error(err);
return
}
callback.call(this, metadata);
readableStream.close();
});
};
let buildTrackInfo = (entry, metadata) => {
return {
info: entry,
metadata: metadata
}
};
let dialogConfigs = {
properties: ['openFile'],
filters: [
{name: 'Audio', extensions: ['mp3']}
]
};
ipc.on('populate-track-list', function (event, arg) {
console.log('Got request from UI...');
getFileReady(event);
});
ipc.on('open-file-dialog', handleDialogOpenRequest);
/*********************************** File Scanning Section *****************************************/
let files = [];
let counter = 0;
let directoryFilter;
let getFileReady = (ipcEvent) => {
let platform = os.platform();
let pathList = config.get('pathList');
if(pathList && pathList.length) {
pathList.forEach((path) => {
console.log('Started Scanning for files at ', path);
scanner({
root: path,
fileFilter: '*.mp3',
depth: 4,
directoryFilter: directoryFilter
})
.on('data', (entry) => {
let filePath = entry.fullPath;
console.log('Found a file at:', filePath);
files.push(entry);
handleMetadata(filePath, (metadata) => {
console.log('Metadata counter:', ++counter);
let track = buildTrackInfo(entry, metadata);
ipcEvent.sender.send('new-track', track);
});
})
.on('end', () =>{
ipcEvent.sender.send('track-snanning-finished', files.length);
console.log('Length: ', files.length);
});
});
} else {
ipcEvent.sender.send('track-scanning-finished', files.length);
}
};