SHARE
TWEET

Untitled

a guest Oct 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const router = express.Router();
  3. const fs = require('fs');
  4. const path = require('path');
  5. const root = { }
  6. router.get("/:filePath", (req, res, next) => {
  7.   jsonData = root
  8.   jsonData.folders = []
  9.   jsonData.files = []
  10.  
  11.   var folderPath = req.params.filePath,
  12.     folderPath = folderPath.replace("+", "/"),
  13.     folderPath = `/home/cameron/TestRestApi/api/stoage/${folderPath}`
  14.   function walkDir(dir) {
  15.     fs.readdirSync(dir).forEach(f => {
  16.       let dirPath = path.join(dir, f);
  17.       if (fs.lstatSync(dirPath).isDirectory()) {
  18.         const folders = {
  19.           serverPath: dirPath,
  20.           name: f,
  21.         }
  22.  
  23.        jsonData.folders.push(folders)
  24.  
  25.       } else {
  26.         var splitPath = '1/2/3/4',
  27.         splitPath = splitPath.replace(/\//g, '+');
  28.         const fileExt = path.extname(dirPath)
  29.         const files = {
  30.           serverPath: dirPath,
  31.           name: f,
  32.           fileType: fileExt,
  33.           reqPath: splitPath
  34.          
  35.         }
  36.         jsonData.files.push(files)
  37.       }
  38.     })
  39.   }
  40.   walkDir(folderPath);
  41.   res.status(200).json(jsonData);
  42.  
  43.  
  44. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top