/
InjectLite.js
285 lines (240 loc) · 8.09 KB
/
InjectLite.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// For each helper function
var forEach = function(array, callback){
var currentValue, index;
var i = 0;
for (i; i < array.length; i += 1) {
if(typeof array[i] == "undefined"){
currentValue = null;
} else {
currentValue = array[i];
}
index = i;
callback(currentValue, i, array);
}
}
var getTargetType = function( url ){
// Check to see if it is a directory
//exists = false;
type = false;
dir = new Dir( url );
if(dir.exists === true){
//exists = true;
type = "dir";
} else {
// Check to see if is a file
fs = new FileStatistics( url );
is_file = fs.isFile();
if(is_file == true){
//exists = true;
type = "file";
}
}
return type;
}
// Determine directory seperator
var getDirectorySeperator = function( s : Switch ){
var directorySeperator;
if(s.isMac()){
directorySeperator = '/';
} else {
directorySeperator = '\\'
}
return directorySeperator;
};
var getElementProperties = function(s : Switch ){
var p = {
trigger: s.getPropertyValue("Trigger"),
target: s.getPropertyValue("Target"),
job_path: s.getPropertyValue("JobPath"),
job_repository: s.getPropertyValue("JobRepository"),
job_name: s.getPropertyValue("JobName"),
extension: s.getPropertyValue("Extension"),
inject_type: s.getPropertyValue("InjectType"),
target_url: null,
target_basename: null,
count_key: s.getPropertyValue("CountPrivateDataKey"),
size_key: s.getPropertyValue("SizePrivateDataKey"),
size_unit: s.getPropertyValue("SizeUnit"),
name_after: s.getPropertyValue("InjectNameAfter"),
search_depth: null
};
// Get search depth
if(p.inject_type == "Count files"){
p.search_depth = s.getPropertyValue("SearchDepthCount");
} else if(p.inject_type == "Get size"){
p.search_depth = s.getPropertyValue("SearchDepthSize");
}
// Determine the job path based on prop
if(p.target == "Specific job"){
p.target_url = p.job_path;
} else if(p.target == "Job repository"){
if(!p.extension){
p.target_url = p.job_repository + getDirectorySeperator(s) + p.job_name;
} else {
p.target_url = p.job_repository + getDirectorySeperator(s) + p.job_name + '.' + p.extension;
}
}
// Save target type as a property
if(p.target_url){
p.target_type = getTargetType(p.target_url);
}
// Add trailing space to directory if necessary
if(p.target_type == "dir"){
last_character = p.target_url.substring(p.target_url.length-1, p.target_url.length);
if(last_character !== "\\" && last_character !== "/"){
p.target_url += getDirectorySeperator(s);
}
}
// Resolve the target basename
var basename_regex = new RegExp("[\\/]");
p.target_basename = p.target_url.split(basename_regex).pop();
s.log(-1, p.target_url + " " + p.target_basename);
// Ensure url is set
if(!p.target_url){
s.log(3, "Job resource location could not be resolved.");
}
return p;
}
var handleInject = function( prop, s : Switch, job : Job, callback ){
if(prop.inject_type == "Assert exists"){
type = getTargetType(prop.target_url);
if(type === false){
callback(false, "Target file does not exist!", job.getPath());
} else {
callback(true, "Target file does exist.", job.getPath());
}
} else if(prop.inject_type == "Inject (read-only)"){
type = getTargetType(prop.target_url);
if(type === false){
callback(false, "Target file does not exist!", job.getPath());
} else {
// Determine output file name
if(prop.name_after == "Incoming job"){
output_basename = job.getName();
} else {
output_basename = prop.target_basename;
}
// Create temp path to copy to
if(type === "dir"){
temp_path = s.createPathWithName(output_basename, true);
} else {
temp_path = s.createPathWithName(output_basename, false);
}
// Copy and callback
copy_successful = s.copy(prop.target_url, temp_path);
if(copy_successful == true){
callback(true, "Read-only inject was successful.", temp_path);
} else {
callback(false, "Read-only inject was unsuccessful.", job.getPath());
}
}
} else if(prop.inject_type == "Count files"){
type = prop.target_type;
if(type === false){
callback(false, "Target file does not exist!", job.getPath());
} else if(type === "file"){
job.setPrivateData(prop.count_key, 1);
callback(true, "Target job is a single file.", job.getPath());
} else if(type === "dir"){
// Function for recursively counting files within a directory
get_recursive_file_count = function(target_dir, entry_list, search_depth, file_count, iteration){
if(typeof(file_count) == "undefined"){
file_count = 0;
}
if(typeof(iteration) == "undefined"){
iteration = 0;
}
if(iteration == search_depth){
return file_count;
}
iteration++;
forEach(entry_list, function(file_name, index){
type = getTargetType(target_dir + file_name);
if(type === "dir"){
sub_target_dir = target_dir + file_name + getDirectorySeperator(s);
sub_dir = new Dir(sub_target_dir);
sub_file_list = sub_dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
result_count = get_recursive_file_count(sub_target_dir, sub_file_list, search_depth, file_count, iteration);
file_count += result_count;
} else if(type === "file"){
file_count++;
}
});
return file_count;
}
dir = new Dir( prop.target_url );
file_list = dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
count = get_recursive_file_count(prop.target_url, file_list, prop.search_depth);
job.setPrivateData(prop.count_key, count);
callback(true, "Target job contains " + count + " files.", job.getPath());
}
} else if(prop.inject_type == "Get size"){
type = getTargetType(prop.target_url);
if(type === false){
callback(false, "Target file does not exist!", job.getPath());
} else if(type === "file"){
fs = new FileStatistics(prop.target_url);
byte_count = fs.getByteCount();
job.setPrivateData(prop.size_key, byte_count);
callback(true, "Target file is " + byte_count + " bytes.", job.getPath());
} else if(type === "dir"){
// Function for recursively counting bytes within a directory
get_recursive_byte_count = function(target_dir, entry_list, search_depth, byte_count, iteration){
if(typeof(byte_count) == "undefined"){
byte_count = 0;
}
if(typeof(iteration) == "undefined"){
iteration = 0;
}
if(iteration == search_depth){
return byte_count;
}
iteration++;
forEach(entry_list, function(file_name, index){
type = getTargetType(target_dir + file_name);
if(type === "dir"){
sub_target_dir = target_dir + file_name + getDirectorySeperator(s);
sub_dir = new Dir(sub_target_dir);
sub_file_list = sub_dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
result_size = get_recursive_byte_count(sub_target_dir, sub_file_list, search_depth, byte_count, iteration);
byte_count += result_size;
} else if(type === "file"){
fs = new FileStatistics(target_dir + file_name);
byte_count += fs.getByteCount();
}
});
return byte_count;
}
// Function to convert bytes to other sizes
bytesToSize = function(bytes, unit) {
if(bytes === 0 || unit == "Bytes") return bytes;
units = {};
units.KB = 1024;
units.MB = units.KB * 1024;
units.GB = units.MB * 1024;
units.TB = units.GB * 1024;
return Math.floor(bytes / units[unit]);
};
dir = new Dir( prop.target_url );
file_list = dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
byte_count = get_recursive_byte_count(prop.target_url, file_list, prop.search_depth);
converted_size = bytesToSize(byte_count, prop.size_unit);
job.setPrivateData(prop.size_key, converted_size);
callback(true, "Target directory is " + converted_size + " " + prop.size_unit + ".", job.getPath());
}
}
}
function jobArrived( s : Switch, job : Job )
{
var prop = getElementProperties(s);
handleInject(prop, s, job, function(success, message, job_path){
if(success === true){
s.log(-1, message);
job.sendToData(1, job_path );
} else {
s.log(3, message);
job.sendToData(3, job.getPath() );
}
});
return;
}