2018-04-12 21:57:16 -04:00
|
|
|
/**
|
|
|
|
* Websocket event handlers
|
|
|
|
*/
|
2018-04-20 11:17:44 -04:00
|
|
|
import {dialog, BrowserWindow} from 'electron';
|
2018-04-17 15:55:19 -04:00
|
|
|
// eslint-disable-next-line
|
2018-04-20 11:17:44 -04:00
|
|
|
import WebSocket from 'ws';
|
2018-04-12 21:57:16 -04:00
|
|
|
|
2018-04-20 11:17:44 -04:00
|
|
|
import {getExifTags} from './exif-helpers';
|
|
|
|
import {JSONMessage} from '../helpers/web-socket';
|
2018-04-19 15:23:36 -04:00
|
|
|
|
2018-04-17 15:55:19 -04:00
|
|
|
const wss = new WebSocket.Server({
|
|
|
|
perMessageDeflate: false,
|
|
|
|
port: 65432,
|
|
|
|
});
|
2018-04-16 16:37:00 -04:00
|
|
|
|
2018-04-17 15:55:19 -04:00
|
|
|
wss.broadcast = (data) => {
|
|
|
|
wss.clients.forEach(client => {
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(data);
|
|
|
|
}
|
2018-04-12 21:57:16 -04:00
|
|
|
});
|
|
|
|
};
|
2018-04-16 16:37:00 -04:00
|
|
|
|
2018-04-17 15:55:19 -04:00
|
|
|
wss.on('connection', ws => {
|
|
|
|
ws.send(JSONMessage('server-log', 'Connected to client!'));
|
|
|
|
ws.on('message', async (...args) => {
|
2018-04-19 15:23:36 -04:00
|
|
|
try {
|
|
|
|
const [type, message] = JSON.parse(args);
|
|
|
|
switch (type) {
|
|
|
|
case 'dropped-files':
|
|
|
|
const filemap = await Promise.all(
|
|
|
|
message.map(file => getExifTags(file))
|
|
|
|
);
|
|
|
|
wss.broadcast(JSONMessage('parsed-exif-tags', await filemap));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'show-open-dialog':
|
|
|
|
const files = dialog.showOpenDialog(BrowserWindow.getFocusedWindow(), {
|
|
|
|
filters: [{
|
|
|
|
extension: ['jpg', 'jpeg', 'tif', 'tiff', 'dng'],
|
|
|
|
name: 'EXIF image types',
|
|
|
|
}, {
|
|
|
|
extensions: ['*'],
|
|
|
|
name: 'All Files',
|
|
|
|
}],
|
|
|
|
properties: [
|
|
|
|
'openFile',
|
|
|
|
'multiSelections',
|
|
|
|
],
|
|
|
|
});
|
|
|
|
wss.broadcast(JSONMessage('selected-files', files));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'show-save-dialog':
|
|
|
|
const saveFile = dialog.showSaveDialog(BrowserWindow.getFocusedWindow());
|
|
|
|
wss.broadcast(JSONMessage('save-file-location', saveFile));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'show-error-box':
|
|
|
|
dialog.showErrorBox('An Error has Occured', message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ws.send(JSONMessage('server-log', [type, message]));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.info(args);
|
2018-04-17 15:55:19 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|