-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
111 lines (101 loc) · 3.48 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
// Electron Components
const { app, BrowserWindow, ipcMain, shell, Menu, dialog, Tray } = require('electron');
const _ = require('lodash');
const fs = require('fs');
const _path = require('path');
const ffmpeg = require('fluent-ffmpeg');
const CONFIG = require('./config.js');
const settings = {allowOverwrite:false,outputDir:_path.resolve(__dirname,'files')}; // Load&Save these from JSON memory store else CONFIG
const { convertFile } = require('./service/convert');
const mediaService = require('./service/file');
// Set the path to packaged binaries
ffmpeg.setFfmpegPath('/Applications/Agora.app/Contents/Resources/app.asar.unpacked/node_modules/ffmpeg-static/bin/darwin/x64/ffmpeg');
ffmpeg.setFfprobePath('/Applications/Agora.app/Contents/Resources/app.asar.unpacked/node_modules/ffprobe-static/bin/darwin/x64/ffprobe');
// Main Window Reference
let mainWindow = null, trayWindow = null;
app.on('ready', () => {
//trayWindow = new Tray(_path.join(__dirname, '/assets/icons/16x16.png'));
mainWindow = new BrowserWindow({
width:800,
height:600,
webPreferences: {
backgroundThrottling:false
},
icon:_path.join(__dirname, '/assets/icons/64x64.png')
});
if (process.env.MODE === 'development') mainWindow.webContents.openDevTools()
// Load primary UI
mainWindow.loadURL((process.env.MODE === 'development') ? `http://localhost:9000` : `file://${__dirname}/build/index.html`);
// Handle closed window
mainWindow.on('closed', () => {
// Dereference the window object
mainWindow = null
});
const menu = Menu.buildFromTemplate([
{
label:app.getName(),
submenu:[
{
label:'Select Files',
click() {
dialog.showOpenDialog({properties: ['openFile', 'multiSelections']}, addFiles);
}
}, {
label:'Convert All',
click() {
console.log("Convert list "+app.getName());
}
}, {
label:"Clear All"
}
]
}
]);
Menu.setApplicationMenu(menu);
});
/**
* Get File Metadata
* @desc Retrieves metadata for a list of files
* @param {object} event - IPC event data
* @param {array<string>} paths - Array of file paths
* @note Extract file logic into seperate ffmpeg file module
*/
ipcMain.on('files:added', (event, paths) => {
// Construct a metadata query queue
const queue = paths.map(path => mediaService.getMeta(path));
// Execute queue and notify mainWindow
Promise.all(queue).then(results => {
mainWindow.webContents.send('files:meta', results);
}).catch(error => {
mainWindow.webContents.send('files:error', error);
});
});
/**
* Convert Files
* @desc Iterates all files and passes to ffmpeg for conversion.
* @param {object} event - IPC event data
* @param {array<object>} queue - Array of task objects
* @note Progress percentage from ffmpeg might not be accurate / Extract file logic into seperate ffmpeg file module
*/
ipcMain.on('convert:start', (event, queue) => {
queue.forEach(task => convertFile(task.input, task.format, percent => {
task.progress = percent * 100;
mainWindow.webContents.send('convert:progress', task);
}).then(outputPath => {
task.output = outputPath;
mainWindow.webContents.send('convert:end', task);
}).catch(err => {
throw new Error(err);
}));
});
/**
* Show File
* @desc Opens the directory at file location.
* @param {object} event - IPC event data
* @param {string} fullPath - Full path to file
*/
ipcMain.on('file:show', (event, fullPath) => {
if ('string' !== typeof fullPath || fullPath.length === '') return;
if (!fs.existsSync(fullPath)) return;
shell.showItemInFolder(fullPath);
});