Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const path = require("path");
  3. const getSize = require('get-folder-size');
  4.  
  5. class File {
  6.  
  7.     static list(content, cb) {
  8.  
  9.         let files = {files: []};
  10.         let items = 0;
  11.  
  12.         fs.exists(content.path, exists => {
  13.             if(exists === false) {
  14.                 fs.mkdir(content.path,err => {
  15.                     if(err) throw err;
  16.                     if(!err)  cb({error:"dossier cree",files: false}) ;
  17.                 })
  18.             }else {
  19.                 if(fs.readdirSync(content.path).length !== 0) {
  20.                     fs.readdirSync(content.path).forEach((file,item,array) => {
  21.                         fs.stat(`${content.path}/${file}`, (err,stats) => {
  22.                             if(err) throw err;
  23.                             if(stats.isFile()) {
  24.                                 files.files.push({
  25.                                     name: path.parse(file).name,
  26.                                     type: path.extname(file) === ".pdf" ? 'PDF' : "File",
  27.                                     size: stats.size,
  28.                                     lastedit: `${stats.atime.getHours()}:${stats.atime.getMinutes()}`
  29.                                 });
  30.                                 items++
  31.                             }else if(stats.isDirectory()) {
  32.                                 getSize(`${content.path}/${file}`, (err,size) => {
  33.                                     if(err) throw err;
  34.                                     if(size) {
  35.                                         console.log(size + ' bytes');
  36.                                         console.log((size / 1024 / 1024).toFixed(2) + ' Mb');
  37.                                         files.files.push({
  38.                                             name: file,
  39.                                             type: "Directory",
  40.                                             size: size,
  41.                                             lastedit: `${stats.atime.getHours()}:${stats.atime.getMinutes()}`
  42.                                         });
  43.  
  44.                                     }
  45.                                     items++
  46.                                 });
  47.  
  48.                             }else {
  49.                                 files.files.push({
  50.                                     name: file,
  51.                                     type: "undifined",
  52.                                     size: stats.size,
  53.                                     lastedit: `${stats.atime.getHours()}:${stats.atime.getMinutes()}`
  54.                                 });
  55.                                 items++
  56.                             }
  57.                             if(items === array.length) {
  58.                                 cb(files)
  59.                             }
  60.                         })
  61.                     })
  62.                 }else {
  63.                     cb({error:"dossier vide",files: false})
  64.                 }
  65.             }
  66.         });
  67.  
  68.     }
  69. }
  70. module.exports = File;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement