Folder listing fixes

James Peret 7 years ago
parent
commit
6516fa8161
2 changed files with 21 additions and 19 deletions
  1. 20 18
      index.js
  2. 1 1
      package.json

+ 20 - 18
index.js

@@ -95,14 +95,23 @@ var list_folder = function(req, res){
95 95
   var extension;
96 96
   var folder_content = [];
97 97
   // Path is file or folder
98
-  if(!isFile(req.params['file'])){
99
-    path = path + req.params['file'];
98
+  if(!isFile(req.params['file']) && req.params['file'] != undefined){
99
+    path = path + "./" + req.params['file'] + "/";
100
+  }
101
+  if(path == "" || path == "undefined"){
102
+    path = "./";
100 103
   }
104
+  console.log("Listing folder: " + path);
101 105
   fs.readdir(path, function(err, files) {
102
-    files.forEach(function() {
103
-      folder_content.push(file);
104
-    });
105
-    res.json({ files: folder_content });
106
+    if(files != null){
107
+      files.forEach(function(file) {
108
+        folder_content.push(file);
109
+      });
110
+      res.json({ files: folder_content });
111
+    } else {
112
+      res.json({ files: [] });
113
+    }
114
+
106 115
   })
107 116
 }
108 117
 
@@ -240,21 +249,14 @@ var url_paths = [
240 249
   '/:folder_10/:folder_9/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
241 250
 ]
242 251
 
243
-// API get files in folder (?list=true)
244
-for (var i = 0; i < url_paths.length; i++) {
245
-    app.get(url_paths[i], function (req, res) {
246
-      if(req.params.list == true){
247
-        console.log("Listing folder: " + url_paths[i]);
248
-        list_folder(req, res);
249
-      }
250
-  });
251
-}
252
-
253 252
 // Get file
254 253
 for (var i = 0; i < url_paths.length; i++) {
255 254
   app.get(url_paths[i], function (req, res) {
256
-    console.log("Sending File: " + url_paths[i]);
257
-    get_file(req, res);
255
+    if(req.query.list == "true"){
256
+      list_folder(req, res);
257
+    } else {
258
+      get_file(req, res);
259
+    }
258 260
   })
259 261
 }
260 262
 

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "@jamesperet/codex-server",
3
-  "version": "0.1.2",
3
+  "version": "0.1.3",
4 4
   "description": "A node file server with super powers.",
5 5
   "main": "index.js",
6 6
   "scripts": {