mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-22 09:22:23 +00:00
Finished upload portion of device db
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user