forked from jameshulse/sc2replayuploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
124 lines (98 loc) · 2.97 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
const { app, Menu, MenuItem, Tray, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const chokidar = require('chokidar');
const username = require('username');
const request = require('request');
const randomstring = require('randomstring');
const fs = require('fs');
const mkdirp = require('mkdirp');
let appDataPath = path.join(app.getAppPath('appData'), 'sc2replaystats');
mkdirp.sync(appDataPath);
let hashKeyPath = path.join(appDataPath, 'hashkey.txt');
if (fs.existsSync(hashKeyPath)) {
global.hashKey = fs.readFileSync(hashKeyPath, 'utf-8');
} else {
global.hashKey = '';
}
app.dock.hide();
let tray;
let window;
let watcher;
let manifest = [];
if (fs.existsSync('manifest.json')) {
manifest = JSON.parse(fs.readFileSync('manifest.json'));
}
ipcMain.on('set-hash', (event, arg) => {
global.hashKey = arg;
fs.writeFileSync(hashKeyPath, arg, 'utf-8');
});
function createWindow() {
window = new BrowserWindow({
width: 250,
height: 220,
show: false,
acceptFirstMouse: true,
frame: false,
resizable: false
});
window.loadURL(`file://${path.join(__dirname, 'index.html')}`);
if (global.hashKey) {
ipcMain.emit('show-hash', global.hashKey);
} else {
window.show();
}
}
function watchForReplays(user) {
let replayPath = `/Users/${user}/Library/Application Support/Blizzard/Starcraft II/Accounts/**/Replays/Multiplayer/*.SC2Replay`;
watcher = chokidar.watch(replayPath, {
persistent: true
});
watcher.on('add', file => {
if (manifest.indexOf(file) !== -1) {
return;
}
request.post('https://sc2replaystats.com/upload_replay.php', {
formData: {
'token': randomstring.generate(32),
'hashkey': global.hashKey,
'timestamp': Math.round(+new Date() / 1000),
'Filedata': {
value: fs.readFileSync(file),
options: {
filename: path.basename(file)
}
}
}
}, (err, httpResponse, body) => {
if (err) {
console.log(err);
}
manifest.push(file);
tray.displayBalloon({
title: 'Replay uploaded',
content: `Uploaded ${file}`
});
});
});
}
function createTray() {
tray = new Tray(path.join(__dirname, 'sc2small.png'));
tray.setContextMenu(Menu.buildFromTemplate([
{ label: 'Settings', click: () => window.show() },
{
label: 'Quit', click: () => {
window.close();
}
}
]));
}
app.on('ready', async () => {
let user = await username();
createTray();
createWindow();
watchForReplays(user);
});
app.on('window-all-closed', app.quit);
app.on('before-quit', () => {
fs.writeFileSync('manifest.json', JSON.stringify(manifest, null, 4));
});