Added API for listing folder contents

James Peret 7 years ago
parent
commit
4f3a4974de
1 changed files with 32 additions and 0 deletions
  1. 32 0
      index.js

+ 32 - 0
index.js

@@ -88,6 +88,24 @@ var buildFilename = function(path, filename){
88 88
   return path + "/" + filename
89 89
 }
90 90
 
91
+var list_folder = function(req, res){
92
+  var path = getPath(req);
93
+  var file_type = "";
94
+  var parts;
95
+  var extension;
96
+  var folder_content = [];
97
+  // Path is file or folder
98
+  if(!isFile(req.params['file'])){
99
+    path = path + req.params['file'];
100
+  }
101
+  fs.readdir(path, function(err, files) {
102
+    files.forEach(function() {
103
+      folder_content.push(file);
104
+    });
105
+    res.json({ files: folder_content });
106
+  })
107
+}
108
+
91 109
 var get_file = function(req, res){
92 110
 
93 111
   var path = getPath(req);
@@ -222,14 +240,28 @@ var url_paths = [
222 240
   '/:folder_10/:folder_9/:folder_8/:folder_7/:folder_6/:folder_5/:folder_4/:folder_3/:folder_2/:folder_1/:file'
223 241
 ]
224 242
 
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
+// Get file
225 254
 for (var i = 0; i < url_paths.length; i++) {
226 255
   app.get(url_paths[i], function (req, res) {
256
+    console.log("Sending File: " + url_paths[i]);
227 257
     get_file(req, res);
228 258
   })
229 259
 }
230 260
 
261
+// Write file content
231 262
 for (var i = 0; i < url_paths.length; i++) {
232 263
   app.post(url_paths[i], function (req, res) {
264
+    console.log("Writing file: " + url_paths[i]);
233 265
     write_file(req, res);
234 266
   })
235 267
 }