99import java .io .InputStream ;
1010import java .io .OutputStream ;
1111import java .io .PrintWriter ;
12- import java .nio .file .Files ;
13- import java .nio .file .attribute .BasicFileAttributes ;
14- import java .nio .file .attribute .PosixFileAttributeView ;
15- import java .nio .file .attribute .PosixFileAttributes ;
16- import java .nio .file .attribute .PosixFilePermission ;
17- import java .nio .file .attribute .PosixFilePermissions ;
1812import java .text .SimpleDateFormat ;
1913import java .util .ArrayList ;
2014import java .util .Date ;
2418import java .util .Properties ;
2519import java .util .Set ;
2620
21+ import java .nio .file .Path ;
22+ import java .nio .file .Paths ;
23+ import java .nio .file .DirectoryStream ;
24+ import java .nio .file .Files ;
25+ import java .nio .file .attribute .BasicFileAttributes ;
26+ import java .nio .file .attribute .PosixFileAttributeView ;
27+ import java .nio .file .attribute .PosixFileAttributes ;
28+ import java .nio .file .attribute .PosixFilePermission ;
29+ import java .nio .file .attribute .PosixFilePermissions ;
30+
2731import javax .servlet .ServletException ;
2832import javax .servlet .http .HttpServlet ;
2933import javax .servlet .http .HttpServletRequest ;
@@ -99,37 +103,51 @@ enum Mode {
99103 }
100104
101105 private String REPOSITORY_BASE_URL = "" ;
106+ private String REPOSITORY_BASE_PATH = "/tmp" ;
102107 // private String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss"; // (2001-07-04 12:08:56)
103108 private String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss z" ; // (Wed, 4 Jul 2001 12:08:56)
104109
105110 @ Override
106111 public void init () throws ServletException {
107112 super .init ();
108113
109- // load from properties file REPOSITORY_BASE_URL and DATE_FORMAT, use default if missing
114+ // load from properties file REPOSITORY_BASE_PATH, REPOSITORY_BASE_URL and DATE_FORMAT, use default if missing
110115 // throw exception in case of bad data
111116 InputStream propertiesFile = null ;
112117 try {
113- propertiesFile = getClass ().getClassLoader ().getResourceAsStream ("angular-filemanager.properties" );
118+ String splittedThisServletPara [] = getInitParameter ("properties" ).split ("," );
119+ propertiesFile = getServletContext ().getResourceAsStream (splittedThisServletPara [1 ].trim ()); // get the default one
114120 if (propertiesFile != null ) {
115121 Properties prop = new Properties ();
116122 // load a properties file from class path, inside static method
117123 prop .load (propertiesFile );
118124 REPOSITORY_BASE_URL = prop .getProperty ("repository.base.url" , REPOSITORY_BASE_URL );
119125 if (!"" .equals (REPOSITORY_BASE_URL ) && !new File (getServletContext ().getRealPath (REPOSITORY_BASE_URL )).isDirectory ()) {
126+ // REPOSITORY_BASE_URL is not empty AND NOT a directory
120127 throw new ServletException ("invalid repository.base.url" );
121128 }
129+ REPOSITORY_BASE_PATH = prop .getProperty ("repository.base.path" , REPOSITORY_BASE_PATH );
130+ if (!"" .equals (REPOSITORY_BASE_PATH ) && !new File (REPOSITORY_BASE_PATH ).isDirectory ()) {
131+ // REPOSITORY_BASE_URL is not empty AND not a directory
132+ throw new ServletException ("invalid repository.base.path" );
133+ }
122134
123135 DATE_FORMAT = prop .getProperty ("date.format" , DATE_FORMAT );
124136 try {
125137 if (new SimpleDateFormat (DATE_FORMAT ).format (new Date ()) == null ) {
126138 // Invalid date format
139+ LOG .debug ("throw invalid date.format " );
127140 throw new ServletException ("invalid date.format" );
128141 }
129142 } catch (NullPointerException | IllegalArgumentException e ) {
130143 throw new ServletException ("invalid date.format" , e );
131144 }
132145 }
146+
147+ else {
148+ LOG .debug ("NO properties File" );
149+ }
150+
133151 } catch (Throwable t ) {
134152 } finally {
135153 if (propertiesFile != null ) {
@@ -152,10 +170,12 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
152170 String mode = request .getParameter ("mode" );
153171 boolean preview = BooleanUtils .toBoolean (request .getParameter ("preview" ));
154172 String path = request .getParameter ("path" );
173+ LOG .debug ("doGet: {} mode: {} preview: {}" , path , mode , preview );
155174
156- File file = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , path );
175+ File file = new File (REPOSITORY_BASE_PATH , path );
157176
158177 if (!file .isFile ()) {
178+ // if not a file, it is a folder, show this error.
159179 response .sendError (HttpServletResponse .SC_NOT_FOUND , "Resource Not Found" );
160180 return ;
161181 }
@@ -196,6 +216,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
196216
197217 @ Override
198218 protected void doPost (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
219+ LOG .debug ("doPost" );
199220 try {
200221 // if request contains multipart-form-data
201222 if (ServletFileUpload .isMultipartContent (request )) {
@@ -229,6 +250,7 @@ private void uploadFile(HttpServletRequest request, HttpServletResponse response
229250 // URL: $config.uploadUrl, Method: POST, Content-Type: multipart/form-data
230251 // Unlimited file upload, each item will be enumerated as file-1, file-2, etc.
231252 // [$config.uploadUrl]?destination=/public_html/image.jpg&file-1={..}&file-2={...}
253+ LOG .debug ("upload now" );
232254 try {
233255 String destination = null ;
234256 Map <String , InputStream > files = new HashMap <String , InputStream >();
@@ -246,20 +268,32 @@ private void uploadFile(HttpServletRequest request, HttpServletResponse response
246268 }
247269 }
248270 if (files .size () == 0 ) {
271+ LOG .debug ("file size = 0" );
249272 throw new Exception ("file size = 0" );
250273 } else {
251274 for (Map .Entry <String , InputStream > fileEntry : files .entrySet ()) {
252- File f = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL + destination ) , fileEntry .getKey ());
275+ File f = new File (REPOSITORY_BASE_PATH + destination , fileEntry .getKey ());
253276 if (!write (fileEntry .getValue (), f )) {
277+ LOG .debug ("write error" );
254278 throw new Exception ("write error" );
255279 }
256280 }
281+
282+ JSONObject responseJsonObject = null ;
283+ responseJsonObject = this .success (responseJsonObject );
284+ response .setContentType ("application/json" );
285+ PrintWriter out = response .getWriter ();
286+ out .print (responseJsonObject );
287+ out .flush ();
257288 }
258289 } catch (FileUploadException e ) {
290+ LOG .debug ("Cannot parse multipart request: DiskFileItemFactory.parseRequest" );
259291 throw new ServletException ("Cannot parse multipart request: DiskFileItemFactory.parseRequest" , e );
260292 } catch (IOException e ) {
293+ LOG .debug ("Cannot parse multipart request: item.getInputStream" );
261294 throw new ServletException ("Cannot parse multipart request: item.getInputStream" , e );
262295 } catch (Exception e ) {
296+ LOG .debug ("Cannot write file" );
263297 throw new ServletException ("Cannot write file" , e );
264298 }
265299
@@ -367,34 +401,32 @@ private void fileOperation(HttpServletRequest request, HttpServletResponse respo
367401 out .print (responseJsonObject );
368402 out .flush ();
369403 }
370-
404+
371405 private JSONObject list (JSONObject params ) throws ServletException {
372406 try {
373407 boolean onlyFolders = params .getBoolean ("onlyFolders" );
374408 String path = params .getString ("path" );
375- LOG .debug ("list path: {} onlyFolders: {}" , path , onlyFolders );
376-
377- File dir = new File (getServletContext ().getRealPath (REPOSITORY_BASE_URL ), path );
378- File [] fileList = dir .listFiles ();
409+ LOG .debug ("list path: {} onlyFolders, try 中文: {}" , path , onlyFolders );
379410
380411 List <JSONObject > resultList = new ArrayList <JSONObject >();
381- SimpleDateFormat dt = new SimpleDateFormat ( DATE_FORMAT );
382- if ( fileList != null ) {
412+ try ( DirectoryStream < Path > directoryStream = Files . newDirectoryStream ( Paths . get ( REPOSITORY_BASE_PATH , path ))) {
413+ SimpleDateFormat dt = new SimpleDateFormat ( DATE_FORMAT );
383414 // Calendar cal = Calendar.getInstance();
384- for (File f : fileList ) {
385- if (!f .exists () || (onlyFolders && !f .isDirectory ())) {
415+ for (Path pathObj : directoryStream ) {
416+ BasicFileAttributes attrs = Files .readAttributes (pathObj , BasicFileAttributes .class );
417+
418+ if (onlyFolders && !attrs .isDirectory ()) {
386419 continue ;
387420 }
388- BasicFileAttributes attrs = Files .readAttributes (f .toPath (), BasicFileAttributes .class );
389421 JSONObject el = new JSONObject ();
390- el .put ("name" , f . getName ());
391- el .put ("rights" , getPermissions (f ));
422+ el .put ("name" , pathObj . getFileName (). toString ());
423+ el .put ("rights" , getPermissions (pathObj ));
392424 el .put ("date" , dt .format (new Date (attrs .lastModifiedTime ().toMillis ())));
393- el .put ("size" , f . length ());
394- el .put ("type" , f . isFile () ? "file " : "dir " );
425+ el .put ("size" , attrs . size ());
426+ el .put ("type" , attrs . isDirectory () ? "dir " : "file " );
395427 resultList .add (el );
396- }
397- }
428+ }
429+ } catch ( IOException ex ) { }
398430
399431 return new JSONObject ().put ("result" , resultList );
400432 } catch (Exception e ) {
@@ -409,8 +441,8 @@ private JSONObject rename(JSONObject params) throws ServletException {
409441 String newpath = params .getString ("newPath" );
410442 LOG .debug ("rename from: {} to: {}" , path , newpath );
411443
412- File srcFile = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , path );
413- File destFile = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , newpath );
444+ File srcFile = new File (REPOSITORY_BASE_PATH , path );
445+ File destFile = new File (REPOSITORY_BASE_PATH , newpath );
414446 if (srcFile .isFile ()) {
415447 FileUtils .moveFile (srcFile , destFile );
416448 } else {
@@ -428,8 +460,8 @@ private JSONObject copy(JSONObject params) throws ServletException {
428460 String path = params .getString ("path" );
429461 String newpath = params .getString ("newPath" );
430462 LOG .debug ("copy from: {} to: {}" , path , newpath );
431- File srcFile = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , path );
432- File destFile = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , newpath );
463+ File srcFile = new File (REPOSITORY_BASE_PATH , path );
464+ File destFile = new File (REPOSITORY_BASE_PATH , newpath );
433465 if (srcFile .isFile ()) {
434466 FileUtils .copyFile (srcFile , destFile );
435467 } else {
@@ -446,7 +478,7 @@ private JSONObject delete(JSONObject params) throws ServletException {
446478 try {
447479 String path = params .getString ("path" );
448480 LOG .debug ("delete {}" , path );
449- File srcFile = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , path );
481+ File srcFile = new File (REPOSITORY_BASE_PATH , path );
450482 if (!FileUtils .deleteQuietly (srcFile )) {
451483 throw new Exception ("Can't delete: " + srcFile .getAbsolutePath ());
452484 }
@@ -463,7 +495,7 @@ private JSONObject editFile(JSONObject params) throws ServletException {
463495 String path = params .getString ("path" );
464496 LOG .debug ("editFile path: {}" , path );
465497
466- File srcFile = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , path );
498+ File srcFile = new File (REPOSITORY_BASE_PATH , path );
467499 String content = FileUtils .readFileToString (srcFile );
468500
469501 return new JSONObject ().put ("result" , content );
@@ -480,7 +512,7 @@ private JSONObject saveFile(JSONObject params) throws ServletException {
480512 String content = params .getString ("content" );
481513 LOG .debug ("saveFile path: {} content: isNotBlank {}, size {}" , path , StringUtils .isNotBlank (content ), content != null ? content .length () : 0 );
482514
483- File srcFile = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , path );
515+ File srcFile = new File (REPOSITORY_BASE_PATH , path );
484516 FileUtils .writeStringToFile (srcFile , content );
485517
486518 return success (params );
@@ -495,7 +527,8 @@ private JSONObject addFolder(JSONObject params) throws ServletException {
495527 String path = params .getString ("path" );
496528 String name = params .getString ("name" );
497529 LOG .debug ("addFolder path: {} name: {}" , path , name );
498- File newDir = new File (getServletContext ().getRealPath (REPOSITORY_BASE_URL + path ), name );
530+
531+ File newDir = new File (REPOSITORY_BASE_PATH , name );
499532 if (!newDir .mkdir ()) {
500533 throw new Exception ("Can't create directory: " + newDir .getAbsolutePath ());
501534 }
@@ -513,7 +546,7 @@ private JSONObject changePermissions(JSONObject params) throws ServletException
513546 String permsCode = params .getString ("permsCode" ); // "rw-r-x-wx"
514547 boolean recursive = params .getBoolean ("recursive" );
515548 LOG .debug ("changepermissions path: {} perms: {} permsCode: {} recursive: {}" , path , perms , permsCode , recursive );
516- File f = new File (getServletContext (). getRealPath ( REPOSITORY_BASE_URL ) , path );
549+ File f = new File (REPOSITORY_BASE_PATH , path );
517550 setPermissions (f , permsCode , recursive );
518551 return success (params );
519552 } catch (Exception e ) {
@@ -552,9 +585,9 @@ private JSONObject extract(JSONObject params) throws ServletException {
552585 }
553586 }
554587
555- private String getPermissions (File f ) throws IOException {
588+ private String getPermissions (Path path ) throws IOException {
556589 // http://www.programcreek.com/java-api-examples/index.php?api=java.nio.file.attribute.PosixFileAttributes
557- PosixFileAttributeView fileAttributeView = Files .getFileAttributeView (f . toPath () , PosixFileAttributeView .class );
590+ PosixFileAttributeView fileAttributeView = Files .getFileAttributeView (path , PosixFileAttributeView .class );
558591 PosixFileAttributes readAttributes = fileAttributeView .readAttributes ();
559592 Set <PosixFilePermission > permissions = readAttributes .permissions ();
560593 return PosixFilePermissions .toString (permissions );
0 commit comments