This repository has been archived by the owner on Jan 26, 2019. It is now read-only.
forked from rumpelstiel/node-file-search-indexer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileIndexer.js
162 lines (145 loc) · 3.69 KB
/
fileIndexer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var nowYear = new Date().getFullYear();
var filewalker = require('filewalker');
var fs = require('fs');
var nedb = require('nedb');
var isNumeric = require('isnumeric');
var pathCheck = require('path');
var walkPath = 'E:\\Dokumente';
var dbPath = 'C:\\nodejs\\filestore.nedb';
var fileFilter = '.pdf';
var directorysToWalk = [];
// create new DB File
fs.stat(dbPath, function(err, stat) {
if(err == null) {
fs.unlink(dbPath);
// fs.closeSync(fs.openSync(dbPath, 'w'));
} else if(err.code == 'ENOENT') {
console.log('file doesnt exist');
// fs.closeSync(fs.openSync(dbPath, 'w'));
} else {
console.log('Some other error: ', err.code);
}
});
//Open DB
var db = new nedb({ filename: dbPath, autoload: true });
// get all Files in WalkPath
var files = fs.readdirSync(walkPath);
// Prepare Directory List
files.forEach(function(item) {
var isDir = false;
isDir = fs.statSync(walkPath + '\\' + item).isDirectory();
if(isDir) {
// if its nowYear OR nowYear-1
// if(item.substring(0,4) == nowYear || item.substring(0,4) == (nowYear - 1) ) {
if(item.substring(0,4) == nowYear) {
//console.log(item);
directorysToWalk.push(walkPath + '\\' + item);
}
}
});
//walk directorys.. only pdfs
directorysToWalk.forEach(function(item) {
filewalker(item)
.on('file', function(p, s, f) {
if(findFileExtension(f, fileFilter)) {
var docType = getDocType(f);
if (docType == '') {} else {
var docId = getDocId(f);
var doc = { path: f, type: docType, id: docId };
db.insert(doc, function (err, newDoc) { })
}
}
})
.walk();
});
function findFileExtension(path, extension) {
var found = false;
var extensionLength = extension.length;
var pathLower = path.toLowerCase();
var pathLength = path.length;
if (pathLength > extensionLength) {
var readFrom = pathLength - extensionLength;
if (pathLower.substring(readFrom) == extension) {
found = true;
}
}
return found;
}
function getDocId(path) {
var docId = '';
var docName = pathCheck.basename(path);
var docName2Arr = docName.split('-');
docId = docName2Arr[2];
return docId;
}
function getDocType(path) {
var docType = '';
docType = pathCheck.basename(path);
docType = docType.substring(0,2);
switch(docType) {
case 'ab':
docType = 'Auftragsbestaetigung';
break;
case 'be':
docType = 'Bestellung';
break;
case 'fa':
docType = 'Faktura';
break;
case 'ko':
docType = 'Kommissionsschein';
break;
case 'lf':
docType = 'Lieferschein';
break;
case 'of':
docType = 'Offert';
break;
case 'di':
docType = 'Disposchein';
break;
default:
docType = '';
}
return docType;
};
// if DebugSwitch is on, wait for console input.
if (process.argv[2] == 'debug') {
console.log('Debug activated');
var stdin = process.openStdin();
stdin.addListener("data", function(d) {
var fullCommand='';
var commandArray=[ ];
fullCommand = d.toString().substring(0, d.length-2).toLowerCase();
commandArray = fullCommand.split(" ");
switch(commandArray[0]) {
case 'help':
console.log('no help available ... now');
break;
case 'show':
db.find({}, function (err, docs) {
console.log(docs);
});
break;
case 'find':
if (isNumeric(commandArray[1])) {
findWhat = commandArray[1];
var rePattern = new RegExp(findWhat);
console.log(rePattern);
db.find({path: rePattern}, function (err, docs) {
console.log(docs);
});
} else {
console.log('not numeric');
}
break;
case 'quit':
console.log('bye!');
process.exit(0);
break;
default:
console.log('command not found');
} //end switch
process.stdout.write('>');
}); // end stdin.addlistener
} //if debug mode on