Commit 4d3e5c97 authored by Eduardo L. Buratti's avatar Eduardo L. Buratti
Browse files

Add controller to handle le-save-config

parent e0b3af31
package controllers;
import java.io.*;
import java.util.ArrayList;
import play.*;
import play.mvc.*;
import play.data.*;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import models.*;
public class SaveConfig extends Controller {
private static String TMP_DIR = "/tmp/";
public static Result get(String inep, String macaddr) {
try {
if (!School.checkMachineBelongsToSchool(inep, macaddr))
return forbidden();
}
catch (java.sql.SQLException e) {
e.printStackTrace();
return internalServerError();
}
String filename = "config_"+inep+".bz2";
File f = new File(TMP_DIR+filename);
if (f.exists()) {
response().setHeader("Content-Disposition", "attachment; filename="+filename);
return ok(f);
}
else
return notFound();
}
public static Result put(String inep, String macaddr) {
try {
if (!School.checkMachineBelongsToSchool(inep, macaddr))
return forbidden();
}
catch (java.sql.SQLException e) {
e.printStackTrace();
return internalServerError();
}
String filename = "config_"+inep+".bz2";
File f = new File(TMP_DIR+filename);
InputStream src = null;
OutputStream dst = null;
try {
if(!f.exists()) {
f.createNewFile();
}
MultipartFormData body = request().body().asMultipartFormData();
FilePart uploadPart = body.getFile("config");
if ((uploadPart == null) || (uploadPart.getFile() == null))
throw new Exception("failed to get file");
src = new FileInputStream(uploadPart.getFile());
dst = new FileOutputStream(f);
byte[] buf = new byte[4096];
int len;
while ((len = src.read(buf)) > 0) {
dst.write(buf, 0, len);
}
src.close();
dst.close();
}
catch (Exception e) {
e.printStackTrace();
return internalServerError();
}
return ok("config saved");
}
public static Result exists(String inep, String macaddr) {
try {
if (!School.checkMachineBelongsToSchool(inep, macaddr))
return forbidden();
}
catch (java.sql.SQLException e) {
e.printStackTrace();
return internalServerError();
}
String filename = "config_"+inep+".bz2";
File f = new File(TMP_DIR+filename);
if (f.exists())
return ok("true");
else
return ok("false");
}
}
......@@ -58,6 +58,13 @@ GET /attendance/network/:project/:region/:state/:city/ controllers.Attendan
GET /attendance/os/:project/ controllers.Attendance.osBrasil(project: String)
POST /le-save-config/:inep/:macaddr/ controllers.SaveConfig.put(inep: String, macaddr: String)
POST /le-save-config/:inep/:macaddr controllers.SaveConfig.put(inep: String, macaddr: String)
GET /le-save-config/:inep/:macaddr/ controllers.SaveConfig.get(inep: String, macaddr: String)
GET /le-save-config/:inep/:macaddr controllers.SaveConfig.get(inep: String, macaddr: String)
GET /le-save-config/:inep/:macaddr/exists/ controllers.SaveConfig.exists(inep: String, macaddr: String)
GET /le-save-config/:inep/:macaddr/exists controllers.SaveConfig.exists(inep: String, macaddr: String)
# Map routes for ajax requests
GET /routes.js controllers.Application.jsRoutes()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment