luci-app-nlbwmon: convert to client side JS
[project/luci.git] / applications / luci-app-nlbwmon / htdocs / luci-static / resources / view / nlbw / backup.js
1 'use strict';
2 'require view';
3 'require ui';
4 'require fs';
5
6 return view.extend({
7 load: function() {
8 return fs.trimmed('/proc/sys/kernel/hostname');
9 },
10
11 handleArchiveUpload: function(ev) {
12 return ui.uploadFile('/tmp/nlbw-restore.tar.gz').then(function() {
13 return fs.exec('/usr/libexec/nlbwmon-action', [ 'restore' ]).then(function(res) {
14 if (res.code != 0)
15 throw new Error(res.stderr || res.stdout);
16
17 var json = JSON.parse(res.stdout || '{}'),
18 list = (L.isObject(json) && Array.isArray(json.restored)) ? json.restored : [];
19
20 ui.showModal(_('Restore complete'), [
21 E('p', [ _('The following database files have been restored:') ]),
22 E('ul', list.map(function(file) { return E('li', [ file ]) })),
23 E('div', { 'class': 'right' }, [
24 E('button', { 'click': ui.hideModal }, [ _('Dismiss') ])
25 ])
26 ]);
27 }).catch(function(err) {
28 ui.addNotification(null, E('p', [ _('Failed to restore backup archive: %s').format(err.message) ]));
29 });
30 });
31 },
32
33 handleArchiveDownload: function(hostname, ev) {
34 return fs.exec_direct('/usr/libexec/nlbwmon-action', [ 'backup' ], 'blob').then(function(blob) {
35 var url = window.URL.createObjectURL(blob),
36 date = new Date(),
37 name = 'nlbwmon-backup-%s-%04d-%02d-%02d.tar.gz'.format(hostname, date.getFullYear(), date.getMonth() + 1, date.getDate()),
38 link = E('a', { 'style': 'display:none', 'href': url, 'download': name });
39
40 document.body.appendChild(link);
41 link.click();
42 document.body.removeChild(link);
43 window.URL.revokeObjectURL(url);
44 }).catch(function(err) {
45 ui.addNotification(null, E('p', [ _('Failed to download backup archive: %s').format(err.message) ]));
46 });
47 },
48
49 render: function(hostname) {
50 return E([], [
51 E('h2', [ _('Netlink Bandwidth Monitor - Backup / Restore') ]),
52 E('h5', [ _('Restore Database Backup') ]),
53 E('p', [
54 E('button', {
55 'click': ui.createHandlerFn(this, 'handleArchiveUpload')
56 }, [ _('Restore') ])
57 ]),
58 E('h5', [ _('Download Database Backup') ]),
59 E('p', [
60 E('button', {
61 'click': ui.createHandlerFn(this, 'handleArchiveDownload', hostname)
62 }, [ _('Generate Backup') ])
63 ])
64 ]);
65 },
66
67 handleSave: null,
68 handleSaveApply: null,
69 handleReset: null
70 });