Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs");
- const path = require("path");
- const getSize = require('get-folder-size');
- class File {
- static list(content, cb) {
- let files = {files: []};
- let items = 0;
- fs.exists(content.path, exists => {
- if(exists === false) {
- fs.mkdir(content.path,err => {
- if(err) throw err;
- if(!err) cb({error:"dossier cree",files: false}) ;
- })
- }else {
- if(fs.readdirSync(content.path).length !== 0) {
- fs.readdirSync(content.path).forEach((file,item,array) => {
- fs.stat(`${content.path}/${file}`, (err,stats) => {
- if(err) throw err;
- if(stats.isFile()) {
- files.files.push({
- name: path.parse(file).name,
- type: path.extname(file) === ".pdf" ? 'PDF' : "File",
- size: stats.size,
- lastedit: `${stats.atime.getHours()}:${stats.atime.getMinutes()}`
- });
- items++
- }else if(stats.isDirectory()) {
- getSize(`${content.path}/${file}`, (err,size) => {
- if(err) throw err;
- if(size) {
- console.log(size + ' bytes');
- console.log((size / 1024 / 1024).toFixed(2) + ' Mb');
- files.files.push({
- name: file,
- type: "Directory",
- size: size,
- lastedit: `${stats.atime.getHours()}:${stats.atime.getMinutes()}`
- });
- }
- items++
- });
- }else {
- files.files.push({
- name: file,
- type: "undifined",
- size: stats.size,
- lastedit: `${stats.atime.getHours()}:${stats.atime.getMinutes()}`
- });
- items++
- }
- if(items === array.length) {
- cb(files)
- }
- })
- })
- }else {
- cb({error:"dossier vide",files: false})
- }
- }
- });
- }
- }
- module.exports = File;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement