Skip to content

Commit 5aeee5a

Browse files
committed
Updated FileService to extend the WebService class, added new upload() method, and fixed several bugs processing baseDir variable
1 parent f59744e commit 5aeee5a

File tree

1 file changed

+109
-26
lines changed

1 file changed

+109
-26
lines changed

src/javaxt/express/services/FileService.java

Lines changed: 109 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
import javaxt.express.*;
44
import javaxt.express.ServiceRequest.Sort;
5-
import javaxt.http.servlet.ServletException;
65
import javaxt.http.servlet.FormInput;
76
import javaxt.http.servlet.FormValue;
87

98
import javaxt.json.*;
109
import javaxt.io.File;
1110
import javaxt.io.Directory;
12-
import static javaxt.utils.Console.console;
1311

1412
import java.util.*;
1513

@@ -22,7 +20,7 @@
2220
*
2321
******************************************************************************/
2422

25-
public class FileService {
23+
public class FileService extends WebService {
2624

2725
private javaxt.io.Directory baseDir;
2826
private static int numDigits = (Long.MAX_VALUE+"").length();
@@ -57,7 +55,7 @@ public FileService(javaxt.io.Directory baseDir){
5755
* folders in a given path. Optional parameters include filter, sort,
5856
* hidden, offset, and limit.
5957
*/
60-
public ServiceResponse getList(ServiceRequest request) throws ServletException {
58+
public ServiceResponse getList(ServiceRequest request) throws Exception {
6159

6260
//Parse params
6361
String path = getPath(request);
@@ -292,19 +290,30 @@ else if (sortBy.equals("type")){
292290

293291
//Return json response
294292
JSONObject json = new JSONObject();
295-
json.set("dir", dir);
293+
if (baseDir==null){
294+
json.set("dir", dir);
295+
json.set("pathSeparator", Directory.PathSeparator);
296+
}
297+
else{
298+
String a = baseDir.toString().replace("\\", "/");
299+
String b = dir.toString().replace("\\", "/");
300+
String d = b.substring(a.length());
301+
if (!d.startsWith("/")) d = "/" + d;
302+
json.set("dir", d);
303+
json.set("pathSeparator", "/");
304+
}
296305
json.set("items", arr);
297306
json.set("count", files.size()+folders.size());
298307
json.set("size", totalSize);
299-
json.set("pathSeparator", Directory.PathSeparator);
308+
300309
return new ServiceResponse(json);
301310
}
302311

303312

304313
//**************************************************************************
305314
//** getDirectory
306315
//**************************************************************************
307-
private Directory getDirectory(String path) throws ServletException{
316+
private Directory getDirectory(String path) throws Exception {
308317

309318
Directory dir;
310319
if (path==null) path = "";
@@ -324,13 +333,24 @@ private Directory getDirectory(String path) throws ServletException{
324333
}
325334
else{
326335
path = path.replace("\\", "/");
336+
337+
338+
//Check if path starts with baseDir. Trim as needed.
339+
String a = baseDir.toString().replace("\\", "/");
340+
String b = path;
341+
if (!b.endsWith("/")) b += "/";
342+
if (b.startsWith(a)){
343+
path = path.substring(a.length());
344+
}
345+
346+
327347
String[] arr = path.split("/");
328348
path = baseDir.toString();
329349

330350
for (String str : arr){
331351
str = str.trim();
332352
if (str.equals(".") || str.equals("..")){
333-
throw new ServletException("Illegal path");
353+
throw new Exception("Illegal path");
334354
}
335355
path += str + "/";
336356
}
@@ -499,23 +519,86 @@ private String getPath(ServiceRequest request){
499519
}
500520

501521

502-
// //**************************************************************************
503-
// //** upload
504-
// //**************************************************************************
505-
// private ServiceResponse upload(ServiceRequest req) throws Exception {
506-
// if (uploadDir==null) return new ServiceResponse(501);
507-
//
508-
// java.util.Iterator<FormInput> it = req.getRequest().getFormInputs();
509-
// while (it.hasNext()){
510-
// FormInput input = it.next();
511-
// if (input.isFile()){
512-
// String fileName = input.getFileName();
513-
// FormValue value = input.getValue();
514-
// value.toFile(new java.io.File(uploadDir.toFile(), fileName));
515-
// }
516-
// }
517-
//
518-
// return new ServiceResponse(200);
519-
// }
522+
//**************************************************************************
523+
//** upload
524+
//**************************************************************************
525+
/** Used to upload files to the server
526+
* @param request ServiceRequest with "multipart/form-data" encoded data
527+
*/
528+
public ServiceResponse upload(ServiceRequest request) throws Exception {
529+
return upload(request, null);
530+
}
531+
532+
533+
//**************************************************************************
534+
//** upload
535+
//**************************************************************************
536+
/** Used to upload files to the server
537+
* @param request ServiceRequest with "multipart/form-data" encoded data
538+
* @param callback Optional callback. Called after a file has been uploaded.
539+
* The callback record will contain the "file", "path", and "op".
540+
*/
541+
public ServiceResponse upload(ServiceRequest request, Callback callback) throws Exception {
542+
543+
544+
javaxt.io.Directory dir = null;
545+
546+
java.util.Iterator<FormInput> it = request.getRequest().getFormInputs();
547+
while (it.hasNext()){
548+
FormInput input = it.next();
549+
if (input.isFile()){
550+
String fileName = input.getFileName();
551+
FormValue value = input.getValue();
552+
if (dir!=null){
553+
javaxt.io.File file = new javaxt.io.File(dir, fileName);
554+
String op = file.exists() ? "update" : "create";
555+
556+
//Save file
557+
value.toFile(file.toFile());
558+
559+
560+
//Call the callback
561+
if (callback!=null){
562+
String path;
563+
if (baseDir==null){
564+
path = dir.toString();
565+
}
566+
else{
567+
String a = baseDir.toString().replace("\\", "/");
568+
String b = dir.toString().replace("\\", "/");
569+
path = b.substring(a.length());
570+
if (!path.startsWith("/")) path = "/" + path;
571+
}
572+
573+
javaxt.utils.Record record = new javaxt.utils.Record();
574+
record.set("op", op);
575+
record.set("path", path);
576+
record.set("file", file);
577+
578+
callback.call(record);
579+
}
580+
}
581+
}
582+
else{
583+
String param = input.getName();
584+
585+
if (param.equalsIgnoreCase("path")){
586+
String path = input.getValue().toString();
587+
if (path==null) path = "";
588+
else path = path.trim();
589+
dir = getDirectory(path);
590+
}
591+
}
592+
}
593+
return new ServiceResponse(200);
594+
}
595+
596+
597+
//**************************************************************************
598+
//** Callback Interface
599+
//**************************************************************************
600+
public static interface Callback {
601+
public void call(javaxt.utils.Record record);
602+
}
520603

521604
}

0 commit comments

Comments
 (0)