Finished upload portion of device db

This commit is contained in:
BWS Systems
2019-06-11 15:28:54 -05:00
parent e86b700e93
commit 69b510ae18
10 changed files with 160 additions and 7 deletions

View File

@@ -5,7 +5,7 @@
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>5.2.next_d</version>
<version>5.2.next_e</version>
<packaging>jar</packaging>
<name>HA Bridge</name>

View File

@@ -2,6 +2,7 @@ package com.bwssystems.HABridge.dao;
public class BackupFilename {
private String filename;
private String file;
public String getFilename() {
return filename;
@@ -10,4 +11,13 @@ public class BackupFilename {
public void setFilename(String filename) {
this.filename = filename;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
}

View File

@@ -401,6 +401,25 @@ public class DeviceResource {
return backupContent;
}, new JsonTransformer());
// http://ip_address:port/api/devices/backup/upload CORS request
options(API_CONTEXT + "/backup/upload/:filename", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
put (API_CONTEXT + "/backup/upload/:filename", "application/json", (request, response) -> {
log.debug("Create upload: {} - {}", request.params(":filename"), request.body());
String theSuccess = deviceRepository.uploadBackup(request.params(":filename"), request.body());
if(theSuccess.contains("Error:"))
response.status(HttpStatus.SC_METHOD_FAILURE);
else
response.status(HttpStatus.SC_OK);
return theSuccess;
}, new JsonTransformer());
// http://ip_address:port/api/devices/backup/create CORS request
options(API_CONTEXT + "/backup/create", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);

View File

@@ -6,6 +6,7 @@ import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -109,4 +110,50 @@ public abstract class BackupHandler {
return content;
}
public String uploadBackup(String aFilename, String theContent) {
String successMessage = null;
if(aFilename == null || aFilename.isEmpty()) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
aFilename = defaultName + "upload-" + dateFormat.format(Calendar.getInstance().getTime()) + fileExtension;
} else {
if(!aFilename.endsWith(fileExtension)) {
aFilename = aFilename +fileExtension;
}
}
Path filePath = FileSystems.getDefault().getPath(repositoryPath.getParent().toString(), aFilename);
successMessage = uploadWriter(theContent, filePath);
return successMessage;
}
private String uploadWriter(String content, Path filePath) {
String aMessage = null;
if (Files.exists(filePath)) {
aMessage = "Error: File exists, cannot write: " + filePath;
log.error(aMessage);
return aMessage;
}
if (Files.notExists(filePath.getParent())) {
try {
Files.createDirectories(filePath.getParent());
} catch (IOException e) {
aMessage = "Error: creating the directory: " + filePath + " message: " + e.getMessage();
log.error(aMessage, e);
return aMessage;
}
}
try {
Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
aMessage = "Success: creating file: " + filePath;
} catch (IOException e) {
aMessage = "Error: writing the file: " + filePath + " message: " + e.getMessage();
log.error(aMessage, e);
}
return aMessage;
}
}

View File

@@ -60,4 +60,16 @@ legend.form-label {
.msg-error {
color:#F00;
font-size:14px;
}
}
.progress {
display: inline-block;
width: 100px;
border: 3px groove #CCC;
}
.progress div {
font-size: smaller;
background: greenyellow;
width: 0;
}

View File

@@ -89,7 +89,9 @@
<script src="js/angular-base64.min.js"></script>
<script src="js/angular-resource.min.js"></script>
<script src="js/ngStorage.min.js"></script>
<script src="js/bootstrap-colorpicker-module.min.js"></script>
<script src="js/bootstrap-colorpicker-module.min.js"></script>
<script src="js/ng-file-upload-shim.min.js"></script>
<script src="js/ng-file-upload.min.js"></script>
<script src="scripts/app.js"></script>
</body>
</html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,4 @@
var app = angular.module('habridge', ['ngRoute', 'ngToast', 'rzModule', 'ngDialog', 'base64', 'scrollable-table', 'ngResource', 'ngStorage', 'colorpicker.module']);
var app = angular.module('habridge', ['ngRoute', 'ngToast', 'rzModule', 'ngDialog', 'base64', 'scrollable-table', 'ngResource', 'ngStorage', 'colorpicker.module', 'ngFileUpload']);
app.config(function ($locationProvider, $routeProvider) {
$locationProvider.hashPrefix('!');
@@ -161,7 +161,7 @@ String.prototype.replaceAll = function (search, replace) {
};
app.service('bridgeService', function ($rootScope, $http, $base64, $location, ngToast) {
app.service('bridgeService', function ($rootScope, $http, $base64, $location, ngToast, Upload) {
var self = this;
this.state = {
base: "./api/devices",
@@ -1285,6 +1285,13 @@ app.service('bridgeService', function ($rootScope, $http, $base64, $location, ng
}).then(
function (response) {
self.state.backupContent = response.data;
var blob = new Blob([self.state.backupContent], {
type: 'text/plain'
});
var downloadLink = angular.element('<a></a>');
downloadLink.attr('href', window.URL.createObjectURL(blob));
downloadLink.attr('download', afilename);
downloadLink[0].click();
},
function (error) {
if (error.status === 401)
@@ -1295,6 +1302,31 @@ app.service('bridgeService', function ($rootScope, $http, $base64, $location, ng
);
};
this.uploadDeviceFile = function (filename, file) {
file.upload = Upload.http({
url: this.state.base + "/backup/upload/" + filename,
method: 'PUT',
headers: {
'Content-Type': file.type
},
data: file
});
file.upload.then(function (response) {
file.result = response.data;
self.viewBackups();
}, function (response) {
if (response.status === 401)
$rootScope.$broadcast('securityReinit', 'done');
else if (response.status > 0)
self.displayWarn('Upload Backup Db File Error:' + response.status + ': ' + response.data);
});
file.upload.progress(function (evt) {
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
this.checkForBridge = function () {
return $http.get(this.state.bridgelocation + "/description.xml").then(
function (response) {
@@ -2266,13 +2298,15 @@ function postrenderAction($timeout) {
}
}
app.controller('ViewingController', function ($scope, $location, bridgeService, ngDialog) {
app.controller('ViewingController', function ($scope, $location, bridgeService, ngDialog, Upload) {
bridgeService.viewDevices();
bridgeService.viewBackups();
$scope.bridge = bridgeService.state;
$scope.optionalbackupname = "";
$scope.bridge.backupContent = undefined;
$scope.bridge.isResumeSupported = Upload.isResumeSupported();
$scope.visible = false;
$scope.imgUrl = "glyphicon glyphicon-plus";
$scope.visibleBk = false;
@@ -2340,6 +2374,14 @@ app.controller('ViewingController', function ($scope, $location, bridgeService,
$scope.downloadBackup = function (backupname) {
bridgeService.downloadBackup(backupname);
};
$scope.uploadDeviceFile = function (aFilename, aDeviceFile) {
$scope.formUpload = true;
if (aDeviceFile != null) {
bridgeService.uploadDeviceFile(aFilename, aDeviceFile);
}
};
$scope.toggle = function () {
$scope.visible = !$scope.visible;
if ($scope.visible)

View File

@@ -132,6 +132,22 @@
Device DB</button>
</div>
</form>
<form class="form-horizontal" name="myForm">
<div class="form-group">
<label class="col-xs-12 col-sm-2 control-label" for="backup-name">Device DB to upload</label>
<div class="col-xs-8 col-sm-7">
<input type="file" ngf-select="" ng-model="deviceFile" name="file">
</div>
<button ng-disabled="!myForm.$valid" type="submit" class="btn btn-primary"
ng-click="uploadDeviceFile(deviceFile.name, deviceFile)">Upload</button>
<span class="progress" ng-show="deviceFile.progress >= 0">
<div style="width:{{deviceFile.progress}}%" ng-bind="deviceFile.progress + '%'"
class="ng-binding"></div>
</span>
<span ng-show="deviceFile.result">Upload Successful</span>
</div>
</form>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
@@ -140,7 +156,7 @@
</tr>
</thead>
<tr ng-repeat="backup in bridge.backups">
<td><button type="button" ng-click="downloadBackup(backup)">{{backup}}</button></td>
<td><a class="btn" ng-click="downloadBackup(backup)" ng-href="{{ url }}">{{backup}}</a></td>
<td>
<button class="btn btn-danger" type="submit" ng-click="restoreBackup(backup)">Restore</button>
<button class="btn btn-warning" type="submit" ng-click="deleteBackup(backup)">Delete</button>