Newer
Older
const fs = require('fs');
const {spawn} = require('child_process');
const ElectronStore = require('electron-store');
const request = require('request');
const appPackage = require('../package.json');
const appConfig = require('./configs/application.json');
const ocsManagerConfig = require('./configs/ocs-manager.json');
const isDebugMode = process.argv.includes('--debug');
const previewpicDirectory = `${app.getPath('userData')}/previewpic`;
const windowIcon = `${__dirname}/images/app-icons/pling-store.png`;
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
const appConfigStoreStorage = 'application';
let mainWindow = null;
let ocsManager = null;
let ocsManagerUrl = '';
async function startOcsManager() {
return new Promise((resolve) => {
const resolveOcsManagerUrl = (data) => {
const matches = data.toString().match(/Websocket server started at: "(wss?:\/\/.+)"/);
if (matches) {
ocsManagerUrl = matches[1];
resolve(true);
}
};
ocsManager = spawn(ocsManagerConfig.bin, ['-p', ocsManagerConfig.port]);
ocsManager.stdout.on('data', (data) => {
console.log(`[${ocsManagerConfig.bin}] ${data}`);
if (!ocsManagerUrl) {
resolveOcsManagerUrl(data);
}
});
ocsManager.stderr.on('data', (data) => {
console.error(`[${ocsManagerConfig.bin}] ${data}`);
if (!ocsManagerUrl) {
resolveOcsManagerUrl(data);
}
});
ocsManager.on('close', (code) => {
console.log(`${ocsManagerConfig.bin} exited with code ${code}`);
});
ocsManager.on('error', () => {
console.error(`Failed to start ${ocsManagerConfig.bin}`);
resolve(false);
});
});
}
function stopOcsManager() {
if (ocsManager) {
ocsManager.kill();
ocsManagerUrl = '';
}
}
function createWindow() {
const appConfigStore = new ElectronStore({
name: appConfigStoreStorage,
defaults: appConfig.defaults
});
const windowBounds = appConfigStore.get('windowBounds');
mainWindow = new BrowserWindow({
title: appPackage.productName,
icon: windowIcon,
x: windowBounds.x,
y: windowBounds.y,
width: windowBounds.width,
height: windowBounds.height,
webPreferences: {
nodeIntegration: true
}
});
if (!isDebugMode) {
mainWindow.setMenu(null);
}
mainWindow.loadURL(indexFileUrl);
mainWindow.on('close', () => {
const appConfigStore = new ElectronStore({name: appConfigStoreStorage});
appConfigStore.set('windowBounds', mainWindow.getBounds());
});
mainWindow.on('closed', () => {
mainWindow = null;
});
if (isDebugMode) {
}
}
function isFile(path) {
try {
const stats = fs.statSync(path);
return stats.isFile();
console.error(error);
return false;
}
}
function isDirectory(path) {
try {
const stats = fs.statSync(path);
return stats.isDirectory();
console.error(error);
return false;
}
}
function btoa(string) {
const buffer = (string instanceof Buffer) ? string : Buffer.from(string.toString(), 'binary');
return buffer.toString('base64');
}
//function atob(string) {
// return Buffer.from(string, 'base64').toString('binary');
//}
function previewpicFilename(itemKey) {
// "itemKey" will be URL to product file
return btoa(itemKey).slice(-255);
}
function downloadPreviewpic(itemKey, url) {
if (!isDirectory(previewpicDirectory)) {
fs.mkdirSync(previewpicDirectory);
}
const path = `${previewpicDirectory}/${previewpicFilename(itemKey)}`;
request.get(url).on('error', (error) => {
console.error(error);
}).pipe(fs.createWriteStream(path));
}
function removePreviewpic(itemKey) {
const path = `${previewpicDirectory}/${previewpicFilename(itemKey)}`;
if (isFile(path)) {
fs.unlinkSync(path);
}
}
app.on('ready', async () => {
if (await startOcsManager()) {
createWindow();
app.quit();
}
});
app.on('quit', () => {
stopOcsManager();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
if (webContents.getType() === 'webview') {
webContents.on('will-navigate', (event, url) => {
if (url.startsWith('ocs://') || url.startsWith('ocss://')) {
// Cancel ocs protocol navigation
event.preventDefault();
}
});
}
// Set configs dir
app.setPath("userData", app.getPath("appData") + "/OCS-Store")
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
ipcMain.on('app', (event, key) => {
const data = {
package: appPackage,
config: appConfig,
isDebugMode: isDebugMode
};
event.returnValue = key ? data[key] : data;
});
ipcMain.on('ocs-manager', (event, key) => {
const data = {
config: ocsManagerConfig,
url: ocsManagerUrl
};
event.returnValue = key ? data[key] : data;
});
ipcMain.on('store', (event, key, value) => {
const appConfigStore = new ElectronStore({name: appConfigStoreStorage});
if (key && value) {
appConfigStore.set(key, value);
}
event.returnValue = key ? appConfigStore.get(key) : appConfigStore.store;
});
ipcMain.on('previewpic', (event, kind, itemKey, url) => {
if (kind === 'directory') {
event.returnValue = previewpicDirectory;
} else if (kind === 'path' && itemKey) {
event.returnValue = `${previewpicDirectory}/${previewpicFilename(itemKey)}`;
} else if (kind === 'download' && itemKey && url) {
downloadPreviewpic(itemKey, url);
event.returnValue = undefined;
} else if (kind === 'remove' && itemKey) {
removePreviewpic(itemKey);
event.returnValue = undefined;
event.returnValue = false;
}
});