From ec1392d30b176810cfc59b44b99536b81100bfb8 Mon Sep 17 00:00:00 2001 From: Copper Phosphate Date: Fri, 7 Aug 2015 12:24:29 +0200 Subject: [PATCH] Add confirmation dialog to battle video backup --- app/elements/bv-dumper/bv-dumper.html | 20 ++++++--------- app/elements/bv-dumper/bv-dumper.js | 34 ++++++++++++++++++++++++-- app/elements/bv-dumper/bv-dumper.ts | 35 +++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/app/elements/bv-dumper/bv-dumper.html b/app/elements/bv-dumper/bv-dumper.html index b8765158..dbaaecf6 100644 --- a/app/elements/bv-dumper/bv-dumper.html +++ b/app/elements/bv-dumper/bv-dumper.html @@ -17,6 +17,12 @@ diff --git a/app/elements/bv-dumper/bv-dumper.js b/app/elements/bv-dumper/bv-dumper.js index 0417f880..2167c42d 100644 --- a/app/elements/bv-dumper/bv-dumper.js +++ b/app/elements/bv-dumper/bv-dumper.js @@ -18,7 +18,15 @@ var __metadata = (this && this.__metadata) || function (k, v) { /// var IpcClient = require("electron-ipc-tunnel/client"); var fs = require("fs"); +var path = require("path-extra"); (function () { + function mkdirOptional(path) { + if (!fs.existsSync(path)) + fs.mkdirSync(path); + } + var backupDirectory = path.join(path.homedir(), "Documents", "KeySAVe", "backup"); + mkdirOptional(path.join(path.homedir(), "Documents", "KeySAVe")); + mkdirOptional(backupDirectory); var BvDumper = (function (_super) { __extends(BvDumper, _super); function BvDumper() { @@ -42,7 +50,8 @@ var fs = require("fs"); }); this.ipcClient.on("dump-bv-nokey", function () { _this.path = ""; - _this.$.dialogNokey.toggle(); + _this.dialogMessage = "You have to break for this video first!"; + _this.$.dialog.toggle(); }); } BvDumper.prototype.pathChanged = function (newValue, oldValue) { @@ -51,7 +60,8 @@ var fs = require("fs"); fs.stat(newValue, function (err, stats) { if (err !== null || stats.size !== 28256) { _this.path = oldValue; - _this.$.dialogInvalid.toggle(); + _this.dialogMessage = "Sorry, but this is not a valid battle video!"; + _this.$.dialog.toggle(); } else { _this.ipcClient.send("dump-bv", _this.path); @@ -64,6 +74,22 @@ var fs = require("fs"); BvDumper.prototype.not = function (val) { return !val; }; + BvDumper.prototype.backup = function () { + var _this = this; + if (this.path) + fs.createReadStream(this.path).pipe(fs.createWriteStream(path.join(backupDirectory, path.basename(this.path))).on("error", function () { + _this.dialogMessage = "Couldn't backup battle video."; + _this.$.dialog.toggle(); + })) + .on("error", function () { + _this.dialogMessage = "Couldn't backup battle video."; + _this.$.dialog.toggle(); + }) + .on("finish", function () { + _this.dialogMessage = "Battle video backupped!"; + _this.$.dialog.toggle(); + }); + }; __decorate([ property({ type: String }), __metadata('design:type', String) @@ -84,6 +110,10 @@ var fs = require("fs"); property({ type: Object }), __metadata('design:type', Object) ], BvDumper.prototype, "fileOptions"); + __decorate([ + property({ type: String }), + __metadata('design:type', String) + ], BvDumper.prototype, "dialogMessage"); Object.defineProperty(BvDumper.prototype, "pathChanged", __decorate([ observe("path"), diff --git a/app/elements/bv-dumper/bv-dumper.ts b/app/elements/bv-dumper/bv-dumper.ts index 4a1e012d..a85479ba 100644 --- a/app/elements/bv-dumper/bv-dumper.ts +++ b/app/elements/bv-dumper/bv-dumper.ts @@ -2,8 +2,18 @@ /// import IpcClient = require("electron-ipc-tunnel/client"); import fs = require("fs"); +import path = require("path-extra"); (() => { +function mkdirOptional(path) { + if (!fs.existsSync(path)) + fs.mkdirSync(path); +} + +var backupDirectory = path.join(path.homedir(), "Documents", "KeySAVe", "backup"); +mkdirOptional(path.join(path.homedir(), "Documents", "KeySAVe")); +mkdirOptional(backupDirectory); + @component("bv-dumper") class BvDumper extends polymer.Base { @property({type: String}) @@ -21,6 +31,9 @@ class BvDumper extends polymer.Base { @property({type: Object}) fileOptions: GitHubElectron.Dialog.OpenDialogOptions; + @property({type: String}) + dialogMessage: string; + ipcClient: IpcClient; myTeam: any[]; enemyTeam: any[]; @@ -46,7 +59,8 @@ class BvDumper extends polymer.Base { this.ipcClient.on("dump-bv-nokey", () => { this.path = ""; - this.$.dialogNokey.toggle(); + this.dialogMessage = "You have to break for this video first!"; + this.$.dialog.toggle(); }); } @@ -56,7 +70,8 @@ class BvDumper extends polymer.Base { fs.stat(newValue, (err, stats) => { if (err !== null || stats.size !== 28256) { this.path = oldValue; - this.$.dialogInvalid.toggle(); + this.dialogMessage = "Sorry, but this is not a valid battle video!"; + this.$.dialog.toggle(); } else { this.ipcClient.send("dump-bv", this.path); } @@ -71,6 +86,22 @@ class BvDumper extends polymer.Base { not(val) { return !val; } + + backup() { + if (this.path) + fs.createReadStream(this.path).pipe(fs.createWriteStream(path.join(backupDirectory, path.basename(this.path))).on("error", () => { + this.dialogMessage = "Couldn't backup battle video." + this.$.dialog.toggle(); + })) + .on("error", () => { + this.dialogMessage = "Couldn't backup battle video." + this.$.dialog.toggle(); + }) + .on("finish", () => { + this.dialogMessage = "Battle video backupped!" + this.$.dialog.toggle(); + }); + } } polymer.createElement(BvDumper); })()