-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreaddirmedia.js
More file actions
55 lines (48 loc) · 1.2 KB
/
readdirmedia.js
File metadata and controls
55 lines (48 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";
var fs = require('fs'),
path = require('path');
function readdir(dir, ext, callback) {
var list = []
fs.readdir(dir, function (err, files) {
if (err) {
return callback(err)
}
var pending = files.length
if (!pending) {
// we are done, woop woop
return callback(null, list)
}
files.forEach(function (file) {
fs.stat(dir + '/' + file, function (err, stats) {
if (err) {
return callback(err)
}
if (stats.isDirectory()) {
files = readdir(dir + '/' + file, ext, function (err, res) {
list = list.concat(res)
pending -= 1
if (!pending) {
callback(null, list)
}
})
}
else {
var detail = {};
detail.name = file;
detail.path = dir;
detail.fullname = dir + '/' + file;
detail.ext = path.extname(file);
detail.size = stats.size;
if(ext.indexOf(detail.ext)!=-1){
list.push(detail)
}
pending -= 1
if (!pending) {
callback(null, list)
}
}
})
})
})
}
module.exports = readdir