Fixed file upload
This commit is contained in:
parent
c0412d40c7
commit
6c8a591bd5
10 changed files with 45 additions and 41 deletions
|
|
@ -77,6 +77,8 @@
|
||||||
$("#button-bg-edit").click(function() {
|
$("#button-bg-edit").click(function() {
|
||||||
// Reset modal
|
// Reset modal
|
||||||
$('#bg-file-input').parent().show();
|
$('#bg-file-input').parent().show();
|
||||||
|
if ($("#file_input").prop("jFiler") != null)
|
||||||
|
$("#file_input").prop("jFiler").reset();
|
||||||
$('#bg-file-progress').parent().hide();
|
$('#bg-file-progress').parent().hide();
|
||||||
$('#bgUploadModal').modal('show');
|
$('#bgUploadModal').modal('show');
|
||||||
});
|
});
|
||||||
|
|
@ -97,10 +99,10 @@
|
||||||
drawMap();
|
drawMap();
|
||||||
},
|
},
|
||||||
error: function(el){
|
error: function(el){
|
||||||
$("#bg-upload-progress").addClass("progress-bar-danger");
|
$("#bg-file-progress").addClass("progress-bar-danger");
|
||||||
},
|
},
|
||||||
onProgress: function(t){
|
onProgress: function(t){
|
||||||
$("#bg-upload-progress").css("width", t+"%");
|
$("#bg-file-progress").css("width", t+"%");
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -133,7 +135,7 @@
|
||||||
|
|
||||||
//////////////// Draw
|
//////////////// Draw
|
||||||
// Background
|
// Background
|
||||||
svg.image("/img/floorplan.jpg", "100%", "100%").x(0).y(0).addClass("floorplan");
|
svg.image("?bgimage", "100%", "100%").x(0).y(0).addClass("bg-image");
|
||||||
|
|
||||||
// Sensors
|
// Sensors
|
||||||
$.each(json.sensors, function(i, sensor) {
|
$.each(json.sensors, function(i, sensor) {
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ public abstract class HalHttpPage implements HttpPage{
|
||||||
request.containsKey("json"))){
|
request.containsKey("json"))){
|
||||||
out.setHeader("Content-Type", "application/json");
|
out.setHeader("Content-Type", "application/json");
|
||||||
JSONWriter writer = new JSONWriter(out);
|
JSONWriter writer = new JSONWriter(out);
|
||||||
writer.write(((HalJsonPage)this).jsonResponse(header, session,cookie, request));
|
writer.write(((HalJsonPage)this).jsonResponse(session,cookie, request));
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
@ -66,7 +66,7 @@ public abstract class HalHttpPage implements HttpPage{
|
||||||
tmpl.set("rootNav", rootNav.createPagedNavInstance(header).getSubNavs());
|
tmpl.set("rootNav", rootNav.createPagedNavInstance(header).getSubNavs());
|
||||||
tmpl.set("userNav", userNav.createPagedNavInstance(header).getSubNavs());
|
tmpl.set("userNav", userNav.createPagedNavInstance(header).getSubNavs());
|
||||||
tmpl.set("alerts", HalAlertManager.getInstance().generateAlerts());
|
tmpl.set("alerts", HalAlertManager.getInstance().generateAlerts());
|
||||||
tmpl.set("content", httpRespond(header, session, cookie, request));
|
tmpl.set("content", httpRespond(session, cookie, request));
|
||||||
out.print(tmpl.compile());
|
out.print(tmpl.compile());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -90,7 +90,6 @@ public abstract class HalHttpPage implements HttpPage{
|
||||||
|
|
||||||
|
|
||||||
public abstract Templator httpRespond(
|
public abstract Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
@ -100,7 +99,6 @@ public abstract class HalHttpPage implements HttpPage{
|
||||||
|
|
||||||
public interface HalJsonPage{
|
public interface HalJsonPage{
|
||||||
DataNode jsonResponse(
|
DataNode jsonResponse(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ public class EventConfigHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(
|
public Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ public class EventOverviewHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(
|
public Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import se.hal.struct.Sensor;
|
||||||
import zutil.db.DBConnection;
|
import zutil.db.DBConnection;
|
||||||
import zutil.io.file.FileUtil;
|
import zutil.io.file.FileUtil;
|
||||||
import zutil.net.http.HttpHeader;
|
import zutil.net.http.HttpHeader;
|
||||||
|
import zutil.net.http.HttpPrintStream;
|
||||||
import zutil.net.http.multipart.MultipartField;
|
import zutil.net.http.multipart.MultipartField;
|
||||||
import zutil.net.http.multipart.MultipartFileField;
|
import zutil.net.http.multipart.MultipartFileField;
|
||||||
import zutil.net.http.multipart.MultipartParser;
|
import zutil.net.http.multipart.MultipartParser;
|
||||||
|
|
@ -16,6 +17,7 @@ import zutil.parser.Base64Encoder;
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
import zutil.parser.Templator;
|
import zutil.parser.Templator;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -23,7 +25,7 @@ import java.util.Map;
|
||||||
/**
|
/**
|
||||||
* Created by Ziver on 2016-06-23.
|
* Created by Ziver on 2016-06-23.
|
||||||
*/
|
*/
|
||||||
public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage{
|
public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage {
|
||||||
private static final String TEMPLATE = "resource/web/map.tmpl";
|
private static final String TEMPLATE = "resource/web/map.tmpl";
|
||||||
|
|
||||||
private String bgType;
|
private String bgType;
|
||||||
|
|
@ -36,36 +38,46 @@ public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage{
|
||||||
super.showSubNav(false);
|
super.showSubNav(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(HttpHeader header,
|
public void respond(HttpPrintStream out, HttpHeader header,
|
||||||
Map<String, Object> session,
|
Map<String, Object> session, Map<String, String> cookie,
|
||||||
Map<String, String> cookie,
|
Map<String, String> request) throws IOException {
|
||||||
Map<String, String> request) throws Exception {
|
|
||||||
if ("POST".equals(header.getRequestType())){
|
if ("POST".equals(header.getRequestType())) {
|
||||||
MultipartParser multipart = new MultipartParser(header);
|
MultipartParser multipart = new MultipartParser(header);
|
||||||
Iterator<MultipartField> it = multipart.iterator();
|
Iterator<MultipartField> it = multipart.iterator();
|
||||||
MultipartField field;
|
MultipartField field;
|
||||||
while ((field = it.next()) != null){
|
while ((field = it.next()) != null) {
|
||||||
if (field instanceof MultipartFileField){
|
if (field instanceof MultipartFileField) {
|
||||||
MultipartFileField file = (MultipartFileField)field;
|
MultipartFileField file = (MultipartFileField) field;
|
||||||
String ext = FileUtil.getFileExtension(file.getFilename());
|
String ext = FileUtil.getFileExtension(file.getFilename());
|
||||||
if (ext.equals("jpg") || ext.equals("png") || ext.equals("svg") || ext.equals("gif")) {
|
if (ext.equals("jpg") || ext.equals("png") || ext.equals("svg") || ext.equals("gif")) {
|
||||||
|
try {
|
||||||
saveBgImage(ext, file.getContent());
|
saveBgImage(ext, file.getContent());
|
||||||
loadBgImage();
|
out.println("Upload successful: " + file.getFilename());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
out.println("Upload error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
bgImage = null; // reload image from db
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
} else if (request.containsKey("bgimage")) {
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (request.containsKey("bgimage")){
|
|
||||||
if (bgImage == null)
|
if (bgImage == null)
|
||||||
loadBgImage();
|
loadBgImage();
|
||||||
// send bg image;
|
out.setHeader("Content-Type", "image/"+bgType);
|
||||||
|
out.setHeader("Content-Length", ""+bgImage.length);
|
||||||
|
out.write(bgImage);
|
||||||
|
} else { // Run default Hal behaviour
|
||||||
|
super.respond(out, header, session, cookie, request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Templator httpRespond(Map<String, Object> session,
|
||||||
|
Map<String, String> cookie,
|
||||||
|
Map<String, String> request) throws Exception {
|
||||||
|
|
||||||
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
|
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
|
||||||
return tmpl;
|
return tmpl;
|
||||||
|
|
@ -73,16 +85,14 @@ public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage{
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataNode jsonResponse(HttpHeader header,
|
public DataNode jsonResponse(Map<String, Object> session,
|
||||||
Map<String, Object> session,
|
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request) throws Exception {
|
Map<String, String> request) throws Exception {
|
||||||
DBConnection db = HalContext.getDB();
|
DBConnection db = HalContext.getDB();
|
||||||
DataNode root = new DataNode(DataNode.DataType.Map);
|
DataNode root = new DataNode(DataNode.DataType.Map);
|
||||||
if ("getdata".equals(request.get("action"))){
|
if ("getdata".equals(request.get("action"))) {
|
||||||
getDeviceNode(db, root);
|
getDeviceNode(db, root);
|
||||||
}
|
} else if ("save".equals(request.get("action"))) {
|
||||||
else if ("save".equals(request.get("action"))){
|
|
||||||
int id = Integer.parseInt(request.get("id"));
|
int id = Integer.parseInt(request.get("id"));
|
||||||
AbstractDevice device = null;
|
AbstractDevice device = null;
|
||||||
if ("sensor".equals(request.get("type")))
|
if ("sensor".equals(request.get("type")))
|
||||||
|
|
@ -115,7 +125,8 @@ public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage{
|
||||||
}
|
}
|
||||||
root.set("events", eventsNode);
|
root.set("events", eventsNode);
|
||||||
}
|
}
|
||||||
private DataNode getDeviceNode(AbstractDevice device){
|
|
||||||
|
private DataNode getDeviceNode(AbstractDevice device) {
|
||||||
DataNode deviceNode = new DataNode(DataNode.DataType.Map);
|
DataNode deviceNode = new DataNode(DataNode.DataType.Map);
|
||||||
deviceNode.set("id", device.getId());
|
deviceNode.set("id", device.getId());
|
||||||
deviceNode.set("name", device.getName());
|
deviceNode.set("name", device.getName());
|
||||||
|
|
@ -125,7 +136,7 @@ public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void loadBgImage(){
|
private void loadBgImage() {
|
||||||
String property = HalContext.getStringProperty("map_bgimage");
|
String property = HalContext.getStringProperty("map_bgimage");
|
||||||
if (property != null) {
|
if (property != null) {
|
||||||
String[] split = property.split(",", 2);
|
String[] split = property.split(",", 2);
|
||||||
|
|
@ -133,7 +144,8 @@ public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage{
|
||||||
bgImage = Base64Decoder.decodeToByte(split[1]);
|
bgImage = Base64Decoder.decodeToByte(split[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveBgImage(String type, byte[] data) throws SQLException {
|
private void saveBgImage(String type, byte[] data) throws SQLException {
|
||||||
HalContext.setProperty("map_bgimage", type+","+ Base64Encoder.encode(data));
|
HalContext.setProperty("map_bgimage", type + "," + Base64Encoder.encode(data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ public class PCHeatMapHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(
|
public Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ public class PCOverviewHttpPage extends HalHttpPage implements HalHttpPage.HalJs
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(
|
public Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
@ -50,7 +49,6 @@ public class PCOverviewHttpPage extends HalHttpPage implements HalHttpPage.HalJs
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataNode jsonResponse(
|
public DataNode jsonResponse(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request) throws Exception {
|
Map<String, String> request) throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ public class SensorConfigHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(
|
public Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ public class SensorOverviewHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(
|
public Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ public class UserConfigHttpPage extends HalHttpPage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Templator httpRespond(
|
public Templator httpRespond(
|
||||||
HttpHeader header,
|
|
||||||
Map<String, Object> session,
|
Map<String, Object> session,
|
||||||
Map<String, String> cookie,
|
Map<String, String> cookie,
|
||||||
Map<String, String> request)
|
Map<String, String> request)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue