-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-to-dmpster.js
234 lines (198 loc) · 6.53 KB
/
upload-to-dmpster.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var argv = require('minimist')(process.argv.slice(2));
var fs = require('fs');
var async = require('async');
var uploadSingleDumpFile = require('./lib/upload-single-dumpfile.js');
var findDumpFiles = require('./lib/find-dumpfiles.js');
var uploadFileLogger = require('./lib/upload-file-logger.js');
var FileObject = require('./lib/file-object.js');
var dmpFileName = argv.f;
var dmpDir = argv.d;
var tags = argv.t || "";
var customConfigFile = argv.config || argv.c;
var deleteAfterUpload = argv["delete"];
var summaryFileName = argv.s;
var directoryDepth = argv["directoryDepth"];
if (typeof(directoryDepth) === "undefined") {
directoryDepth = -1;
}
function exitWithError(errorMessage) {
console.error("ERROR: " + errorMessage);
process.exit(-1);
}
if (!dmpFileName && !dmpDir) {
exitWithError("Dump file name parameter -f or dump file directory parameter -d missing!");
}
if (dmpFileName && dmpDir) {
exitWithError("Only one of the parameters -f and -d can be used at a time!");
}
var configFileName = customConfigFile || './config.js';
if (!fs.existsSync(configFileName)) {
exitWithError("Config file '" + customConfigFile + "' does not exist!");
}
console.log("Config File: " + configFileName);
var config = require(configFileName);
if (!config.uploadUrl) {
exitWithError("Configuration entry 'uploadUrl' missing! Check your config.js file.");
}
config.dmpsterUrl = config.baseUrl + "/dmpster";
console.log("Upload URL: '" + config.uploadUrl + "'");
var maxParallelUploads = config.maxParallelUploads || argv.p || 2;
var autoTagger = require('./lib/autotagger/autotagger.js');
autoTagger.load(config.autotagger || []);
var postUploadHandlers = require('./lib/postupload-handler/postupload-handlers.js');
var postUploadHandlersConfigList = config.postupload || [];
if (deleteAfterUpload && (postUploadHandlersConfigList.indexOf("postupload-delete") == -1)) {
postUploadHandlersConfigList.push("postupload-delete");
}
postUploadHandlers.load(postUploadHandlersConfigList);
var summaryWriter;
if (summaryFileName) {
console.log("Summary File: " + summaryFileName);
try {
summaryWriter = require("./lib/upload-summary-writer")(config);
}
catch (ex) {
exitWithError("Could not initialize the summary writer module! (" + ex + ")");
}
}
function uploadAndLogSingleDumpFile(dmpFileObject, callback) {
uploadFileLogger.uploadStarted(dmpFileObject);
uploadSingleDumpFile(config.uploadUrl, dmpFileObject, dmpFileObject.tags || tags, function(err, result) {
uploadFileLogger.uploadFinished(dmpFileObject, err, result);
if (result) {
result.fileObject = dmpFileObject;
result.url = config.baseUrl + result.url;
}
callback(null, result);
});
}
function createSingleFileObjectArray(filePath, callback) {
fs.stat(filePath, function(err, stat) {
if (err) {
callback("The file '" + filePath + "' does not exist!");
}
else {
callback(null, [ new FileObject(filePath, stat) ]);
}
});
}
function addAutoTagsToDumpFiles(dmpFileObjects, callback) {
async.map(
dmpFileObjects,
function(currDumpFileObject, callback) {
autoTagger.createTagsForDump(tags, currDumpFileObject, function(err, resultTags) {
if (err) {
callback(err);
}
else {
currDumpFileObject.tags = resultTags;
callback(null, currDumpFileObject);
}
});
},
callback);
}
function invokePostUploadHandlers(results, callback) {
if (postUploadHandlers.isEmpty()) {
callback(null, results);
}
else {
console.log("Post-upload-handlers ...");
postUploadHandlers.invokeHandlers(results, function(err, invokeResult) {
console.log("Post-upload-handlers finished.");
callback(err, results);
});
}
}
function filterFoundDumpFilesBySize(dmpFileObjects, callback) {
if (config.minDumpSize) {
var filteredDumpFiles = dmpFileObjects.filter(function(dumpFile) { return dumpFile.size >= config.minDumpSize; });
callback(null, filteredDumpFiles);
}
else {
callback(null, dmpFileObjects);
}
}
function uploadMultipleDumpFiles(dmpFileObjects, callback) {
async.mapLimit(dmpFileObjects, maxParallelUploads, uploadAndLogSingleDumpFile, function(err, results) {
callback(err, results);
});
}
function filterSuccessfulResults(uploadResults, callback) {
var filteredResults = uploadResults.filter(function(result) { return result; });
callback(null, filteredResults);
}
function sortFileObjects(fileObjects, callback) {
var sortedFileObjects = fileObjects.sort(function(lhs, rhs) {
return lhs.fullPath.localeCompare(rhs.fullPath);
});
callback(null, sortedFileObjects);
}
function formatFileSize(fileSize) {
var sizeMB = (fileSize / (1024 * 1024)).toFixed(2);
return sizeMB + " MB";
}
function printFileNames(files, callback) {
var totalSize = files.reduce(function(previous, current) { return previous + current.size; }, 0);
console.log();
console.log("Found " + files.length + " dump files (" + formatFileSize(totalSize) + "):");
files.forEach(function(currFileObject) {
console.log(" " + currFileObject.fullPath + " (" + formatFileSize(currFileObject.size) + ")");
});
console.log();
callback(null, files);
}
function writeSummaryFile(results, callback) {
if (summaryWriter) {
summaryWriter.writeSummary(results, summaryFileName, function(err) {
if (err) {
callback(err);
}
else {
callback(null, results);
}
});
}
else {
callback(null, results);
}
}
function finishUploadResultsHandler(err, results) {
if (err) {
exitWithError("Uploading dump files failed. " + err);
}
else {
console.log();
console.log("SUCCESS! Uploading dump files finished.");
console.log(results);
}
}
var fileFindAsyncFunction;
if (dmpFileName) {
console.log("Filename: '" + dmpFileName + "'");
console.log("Tags: '" + tags + "'");
fileFindAsyncFunction = function(callback) {
createSingleFileObjectArray(dmpFileName, callback);
};
}
else {
console.log("Directory: '" + dmpDir + "'");
console.log("Directory Depth: " + ((directoryDepth >= 0) ? directoryDepth : "UNLIMITED"));
console.log("Tags: '" + tags + "'");
fileFindAsyncFunction = function(callback) {
findDumpFiles(dmpDir, directoryDepth, callback);
};
}
async.waterfall(
[
fileFindAsyncFunction,
filterFoundDumpFilesBySize,
sortFileObjects,
printFileNames,
addAutoTagsToDumpFiles,
uploadMultipleDumpFiles,
filterSuccessfulResults,
invokePostUploadHandlers,
writeSummaryFile
],
finishUploadResultsHandler);