-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (117 loc) · 3.08 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
var fs = require("fs");
var path = require("path");
var async = require("async");
var readExtName = [];
var retFileArr = [];
var callback_Function;
var q;
var option;
function checkFileIsDirectory(fileObj,callback) {
if(fileObj.file == "noneFile" || !isExistFile(fileObj.file)){
callback();
return;
}
if (fs.statSync(fileObj.file).isDirectory()) {
fs.readdir(fileObj.file, function(err, files) {
for (var i = 0; i < files.length; i++) {
q.push({"file":path.join(fileObj.file,files[i]) }, function(){} );
}
if(files.length==0){
q.push({"file":"noneFile"}, function(){} );
}
callback();
});
}else{
readFile(fileObj.file, callback);
}
}
function isExistFile(file) {
try {
fs.statSync(file);
return true
} catch(err) {
if(err.code === 'ENOENT') return false
}
}
function readFile(inFilePath, inCallback){
if( isReadPermission(inFilePath) ){
if(option && option.isStats){
fs.stat(inFilePath, function(err,stats){
if(!err){
retFileArr.push({
"path" : inFilePath ,
"stats" : stats
});
inCallback();
}else{
console.log(err);
inCallback();
}
});
}else{
retFileArr.push({
"path" : inFilePath
});
inCallback();
}
}else{
inCallback();
}
}
function initQueue(){
q = null;
q = async.queue(function(fileObj, callback) {
checkFileIsDirectory(fileObj,callback);
}, 1);
q.drain = function() {
qDrain();
}
}
function qDrain(){
readComplete();
initVar();
}
function saveReadExtName(inOption){
if(!inOption || !inOption.ext || inOption.ext == "" ){
readExtName = [];
}else{
readExtName = inOption.ext.replace(/^\|/,"").replace(/\|$/,"").split("|");
}
}
function isReadPermission(inFile){
if(readExtName.length == 0)return true;
var flg = false;
for(var i=0; i<readExtName.length; i++){
if("."+readExtName[i].replace(".","") == getExtName(inFile)){
flg = true;
break;
}
}
return flg;
}
function getExtName(inFile){
return path.extname( path.normalize(inFile) );
}
function readComplete(){
callback_Function(retFileArr);
};
function initVar(){
readExtName = [];
retFileArr = [];
callback_Function = null;
q = null;
initQueue();
}
module.exports.read = function(inFileList, inOption ,inCallback_Func){
initVar();
callback_Function = inCallback_Func;
option = inOption;
saveReadExtName(inOption);
for(var i=0; i<inFileList.length; i++){
q.push({"file": path.resolve(inFileList[i]) }, function() {});
}
};
module.exports.getFileExtName = function(inFile){
return getExtName(inFile);
};