Commit 1c5c893e by Owen Ryan Ang

Implemented file:// protocol for image fetching

parent 6aee2a9f
...@@ -60,3 +60,8 @@ var REASON_LIST = "Reason1,Reason2,Reason3,Reason4" ...@@ -60,3 +60,8 @@ var REASON_LIST = "Reason1,Reason2,Reason3,Reason4"
//KEYCLOAK CONFIG //KEYCLOAK CONFIG
const REDIRECT_URL = 'http://auth-server/auth/realms/GFS/protocol/openid-connect/logout?redirect_uri=encodedRedirectUri'; const REDIRECT_URL = 'http://auth-server/auth/realms/GFS/protocol/openid-connect/logout?redirect_uri=encodedRedirectUri';
//Temporary variables for dev
var LOCAL_DOMAIN = "http://localhost:8080"
var LOCAL_FETCH_URL = LOCAL_DOMAIN + "/WebGde/svc/gfs-rest/fetch-local"
var LOCAL_FETCH_TEST_URL = "http://localhost:8080/Sample_API/testservice/file"
...@@ -114,6 +114,21 @@ async function accessFile() { ...@@ -114,6 +114,21 @@ async function accessFile() {
if (img != null ? img.startsWith("http") : false) { if (img != null ? img.startsWith("http") : false) {
response = await fetch(img); response = await fetch(img);
gfsFileName = getGfsFileName(response.headers.get("content-disposition")); gfsFileName = getGfsFileName(response.headers.get("content-disposition"));
} else if (img != null ? img.startsWith("file") : false){
console.log('file');
const payload = {
dir: img
};
response = await fetch(`${GFS_URL}/fetch-local`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
gfsFileName = getGfsFileName(response.headers.get("content-disposition"));
} else { } else {
let formData = new FormData(); let formData = new FormData();
formData.append("data", JSON.stringify({ formData.append("data", JSON.stringify({
......
package com.svi.webgde.restservice.object;
public class Request {
private String dir;
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
}
...@@ -32,6 +32,7 @@ import org.json.simple.JSONArray; ...@@ -32,6 +32,7 @@ import org.json.simple.JSONArray;
import com.opencsv.CSVReader; import com.opencsv.CSVReader;
import com.opencsv.CSVWriter; import com.opencsv.CSVWriter;
import com.svi.template.restservice.globals.AppConfig; import com.svi.template.restservice.globals.AppConfig;
import com.svi.training.services.Request;
import com.svi.webgde.restservice.object.XMLContents; import com.svi.webgde.restservice.object.XMLContents;
import com.svi.webgde.restservice.utils.XMLUtil; import com.svi.webgde.restservice.utils.XMLUtil;
...@@ -98,7 +99,7 @@ public class GDEWebServices { ...@@ -98,7 +99,7 @@ public class GDEWebServices {
return Response.ok(json.toString()).build(); return Response.ok(json.toString()).build();
} }
@POST @POST
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
...@@ -224,7 +225,7 @@ public class GDEWebServices { ...@@ -224,7 +225,7 @@ public class GDEWebServices {
} }
return Response.ok("Success").build(); return Response.ok("Success").build();
} }
@POST @POST
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
...@@ -235,20 +236,20 @@ public class GDEWebServices { ...@@ -235,20 +236,20 @@ public class GDEWebServices {
} catch (Exception e) { } catch (Exception e) {
return Response.status(500).entity("Fail").build(); return Response.status(500).entity("Fail").build();
} }
} }
@POST @POST
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/update-exception") @Path("/update-exception")
public Response updateException(XMLContents xml) { public Response updateException(XMLContents xml) {
try { try {
File file = new File(xml.getOutputDir()); File file = new File(xml.getOutputDir());
if(file.exists()) { if (file.exists()) {
XMLUtil.updateExceptionRmrk(xml); XMLUtil.updateExceptionRmrk(xml);
}else { } else {
XMLUtil.generateXML(xml); XMLUtil.generateXML(xml);
} }
...@@ -260,5 +261,23 @@ public class GDEWebServices { ...@@ -260,5 +261,23 @@ public class GDEWebServices {
return Response.ok("Success").build(); return Response.ok("Success").build();
} }
@POST
@Path("/fetch-local")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile(Request request) {
// extract file path from file:// URL format
String filePath = request.getDir().substring("file://".length());
// read file from local file system
File file = new File(filePath);
if (!file.exists()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
// return file contents in blob format with filename in content-disposition header
ResponseBuilder response = Response.ok(file);
response.header("content-disposition", "attachment; filename = " + file.getName());
return response.build();
}
} }
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