-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcat.js
471 lines (418 loc) · 14.8 KB
/
cat.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
const fs = require("fs");
const path = require("path"); //path module
var sha256 = require('js-sha256');
const AES = require('./modules/AES');
let compression = require("./modules/compression");
//Command Syntax: node cat.js [option] [filepath] "[key]" //key is for encryption/decryption
// OPTIONS
//-s: removes extra spaces between lines ✔
//-n: enumerate ✔
//-b: enumerate except empty lines ✔
//-c: compress(huffman) ✔
//-d: decompress ✔
//-e: encrypt(AES-128) ✔
//-de: decrypt ✔
//-touch: new file or folder ✔
//-help: command ✔
//-organize ✔
//-tree
//
// TODO: also include https://www.tecmint.com/13-basic-cat-command-examples-in-linux/
let command = process.argv; // input from user
// console.log(command);
readCommand(command.splice(2));
function readCommand(command){
optionArray = [];
filepaths = [];
indx = 0;
while (indx < command.length && command[indx][0] == "-") {
optionArray.push(command[indx++]);
}
//If options are valid or not
if(!validateOptions(optionArray)){
console.log("Invalid option USE node cat.js -help");
console.log("for help");
return;
}
//Help option
if(optionArray.includes("-help")){
help();
return;
}
// Reading filepaths
for(let i=indx; i<command.length; i++){
filepaths.push(command[i]);
}
if(filepaths.length == 0 && !optionArray.includes("-organize") && !optionArray.includes("-tree")) {
console.log("Filepath not found");
return;
}
//Check for advance Options
conditionOrOptions = isAdvance(optionArray);
if (typeof conditionOrOptions == "object"){ //Either Boolean or object
// if optionArray contains advanced options then object is retured(array) else boolean(false) is returned.
advanceCatCommands(conditionOrOptions, filepaths);
return;
}
options = optionsConflictResolver(optionArray);
for(let i=0; i<filepaths.length; i++){
if (fs.existsSync(filepaths[i])) runCommands(options, filepaths[i]);
else console.log(`File: ${filepaths[i]} does not exist.`);
}
}
function validateOptions(options) {
const valids = ["-s", "-n", "-b", "-e", "-de", "-help", "-c", "-d", "-touch", "-organize", "-tree"];
for(let i=0; i<options.length; i++) {
if( !valids.includes(options[i]) ) {
return false;
}
}
return true;
}
function help() {
console.log("Syntax:");
console.log("node cat.js [option(s)] [File Path/Folder Path] [key]");
console.log("-s : Removes Extra spaces between lines and prints it.");
console.log("-n: Enumerates the content of files and print it.");
console.log("-b: Enumerates the content of files and print it.");
console.log("-n: Enumerates the non-empty of files and print it.");
console.log("-e: Encrypts the file with AES-128 bit and modifies the original file. Requires 16 character key");
console.log("-e: Decrypts the file and modifies the original file. Requires 16 character key (key authentication is also performed before decrypting)");
console.log("-c: Compresses the text based file using Huffman coding.");
console.log("-d: Decompresses the compressed file.");
console.log("-touch: Used to create a file/folder at the specified path");
console.log("-help: Help");
}
function organize(folderpath) {
//organizes the files in the folder into different types of folder
if(folderpath == undefined){
folderpath = process.cwd();
//Current working directory
}
let organisedFile = path.join(folderpath, "Organised_Files");
if ( fs.existsSync(organisedFile) == false ){
fs.mkdirSync(organisedFile);
let allFiles = fs.readdirSync(folderpath);
for (let i = 0; i < allFiles.length; i++){
let fullPathOfFile = path.join(folderpath, allFiles[i]);
if (allFiles[i].substring(allFiles[i].length-11) == "_compressed") moveToDestinationFolder(folderpath, allFiles[i], organisedFile, "Archives");
let isFile = fs.lstatSync(fullPathOfFile).isFile();
if (isFile) {
let ext = path.extname(allFiles[i]).split(".")[1];
let destFolderName = getFolderName(ext);
if (destFolderName != null)
moveToDestinationFolder(folderpath, allFiles[i], organisedFile, destFolderName);
}
}
}
else {
console.log("Folder Already exist");
return;
}
}
function moveToDestinationFolder(from, file, organisedFolder, destFolder) {
let destPath = path.join(organisedFolder, destFolder);
if(!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
let srcFile = path.join(from, file);
let destFile = path.join(destPath, file);
fs.rename(srcFile, destFile, function (err) { if (err) throw err }); // Moving File
}
function getFolderName(extension) {
// Return Folder name accrding to the file type else returns null
const types = {
Media: ["mp4", "mkv", "mp3"],
Archives: ['zip', '7z', 'rar', 'tar', 'gz', 'ar', 'iso', "xz"],
Documents: ['docx', 'doc', 'pdf', 'xlsx', 'xls', 'odt', 'ods', 'odp', 'odg', 'odf', 'txt', 'ps', 'tex'],
App: ['exe', 'dmg', 'pkg', "deb", "apk"],
Images: ['png','jpg','jpeg']
}
for (t in types) {
if (types[t].includes(extension)) return t;
}
return null;
}
function isAdvance(optionArray) {
if(optionArray.includes("-e") || optionArray.includes("-de")
|| optionArray.includes("-organize") || optionArray.includes("-c")
|| optionArray.includes("-d") || optionArray.includes("-touch")
|| optionArray.includes("-tree")) {
if (optionArray.length > 1) {
console.log("Invalid options: [Advanced Option should be used alone]");
return [];
}
// Change to switch
if(optionArray.includes("-e")) return ["-e"];
else if(optionArray.includes("-de")) return ["-de"];
else if(optionArray.includes("-organize")) return ["-organize"];
else if(optionArray.includes("-c")) return ["-c"];
else if(optionArray.includes("-d")) return ["-d"];
else if(optionArray.includes("-touch")) return ["-touch"];
else if(optionArray.includes("-tree")) return ["-tree"];
}
return false;
}
function advanceCatCommands(options, filepaths) {
if (options.length == 0) return;
//Organise
else if(options.includes("-organize")) {
if (filepaths.length > 1) {
console.log("[organize] Atmost one path is permitted");
return;
}
organize(filepaths[0]);
}
// Encryption/Decryption
else if(options.includes("-e") || options.includes("-de")) {
if (filepaths.length > 2) {
console.log("Advanced Options can be applied on exactly one file at a time");
return;
}
if (filepaths.length < 2) {
console.log("Requires Filepath and key");
return;
}
if (fs.existsSync(filepaths[0])) encryDecry(options, filepaths[0], filepaths[1]);
else console.log(`File: ${filepaths[0]} does not exist.`);
return;
}
// Compression/Decompression
else if (options.includes("-c") || options.includes("-d")){
if (filepaths.length != 1) {
console.log("Compression/Decompression requires path");
return;
}
compressDecompress(options, filepaths[0]);
}
// Touch
else if (options.includes("-touch")) {
touch(filepaths[0]);
}
// Tree
else if (options.includes("-tree")) {
if(filepaths[0] == undefined){
filepaths[0] = process.cwd();
}
tree(filepaths[0], "");
}
}
function optionsConflictResolver(optionArray) {
//Combinations
//if -n and -b then first one dominates
//if -s and -n or -s and -b togeather then first run -s then -n or -b.
// if options arry consist of ency/decry or compress/decompress or touch then ignore other commands and dont read the file after words.
// and try to give precedence to the option which comes first.
// case -s with -b then both functions will run and output will look similar to only -b.
if(optionArray.length == 0) {
return ["-r"];
}
options = [];
if (optionArray.includes("-s")) options.push("-s");
if (optionArray.includes("-n") && optionArray.includes("-b")) {
nindx = optionArray.indexOf("-n");
bindx = optionArray.indexOf("-b");
if(nindx < bindx) options.push("-n");
else options.push("-b");
}
else if(optionArray.includes("-n")) options.push("-n");
else if(optionArray.includes("-b")) options.push("-b");
options.push("-r");
return options;
}
function runCommands(options, file) {
//TODO: add implementations of optionArray
if(options.length == 0){
return;
}
let content = getContent(file);
for(let i=0; i<options.length; i++){
switch(options[i]){
case "-r" :
printContentFromArray(content);
break;
case "-n" :
content = enumerate(content);
break;
case "-b" :
content = enumerate(content, space=true);
break;
case "-s" :
content = spaceRemover(content);
break;
default :
console.log("Wrong Option");
}
}
}
function getContent(filepath) {
let c = fs.readFileSync(filepath, "utf-8");
return c.split("\n");
}
function printContentFromArray(content) {
//Print content from the error
for(let i=0;i<content.length;i++){
console.log(content[i]);
}
}
function spaceRemover(content) {
let result = [];
let indx = 0;
while(indx < content.length && (content[indx] == "\r" || content[indx].trim() == "")) indx++; // Removes the starting extra lines
while(indx < content.length) {
if(content[indx] == "\r"){
result.push(content[indx]);
while(indx < content.length && (content[indx] == "\r" || content[indx].trim() == "")) indx++;
}
else {
result.push(content[indx]);
indx++;
}
}
return result;
}
function enumerate(content, space=false) {
let result = [];
let number = 1;
for(i=0; i<content.length; i++){
if(space){
if ((content[i] == "\r" || content[i].trim() == "")) {
result.push(content[i]);
continue;
}
}
result.push(`${number} ${content[i]}`);
number += 1;
}
return result;
}
function encryDecry(options, file, key) {
if(key.length != 16) {
console.log("Require 16 character key");
return;
}
let obj = new AES(key);
let hash = sha256.sha256(key);
let filecontent = null;
let indx = 0;
if(options[0] == "-de") {
filecontent = fs.readFileSync(file, "utf-8");
let index = 0;
while(filecontent[index] != "\n" && index<filecontent.length) index++;
let keydigest = filecontent.substring(0, index+1);
//console.log( keydigest);
try{
const object = JSON.parse(keydigest);
if(object["keydigest"] != hash){
console("Key mismatch");
return;
}
indx = index+1;
}
catch(err){
console.log("Unable to authenticate the key");
return;
}
}
else {
filecontent = fs.readFileSync(file, "hex");
}
let finalmessage = "";
while(indx < filecontent.length){
let till = indx+16;
let temp = filecontent.substring(indx, till);
indx = till;
let text = null;
if(options[0] == "-e") {
text = obj.encrypt(temp);
}
else if(options[0] == "-de") {
text = obj.decrypt(temp);
}
//console.log(temp);
//console.log(text);
finalmessage += text;
}
//console.log(finalmessage);
if(options[0] == "-e") {
finalmessage = '{"keydigest":"'+hash+'"}\n'+finalmessage;
fs.writeFileSync(file, finalmessage.substring(0, finalmessage.length));
}
if(options[0] == "-de") {
last = finalmessage.length;
while(finalmessage.charCodeAt(last-1) == 0){
last--;
}
fs.writeFileSync(file, finalmessage.substring(0, last), "hex");
}
}
function decompressFolderBriefValidation(folder) {
let isFile = fs.lstatSync(folder).isFile();
if (!isFile){
check = folder.split("_");
if (check[check.length-1] == "compressed"){
fullPath = folder.split("/");
folderName = fullPath[fullPath.length-1];
fileNameAndExtensionArray = folderName.split("_");
filename = fileNameAndExtensionArray.splice(0, fileNameAndExtensionArray.length-2).join("_");
fileExtension = fileNameAndExtensionArray[0];
fullFileName = filename+"."+fileExtension;
filePath = path.join(folder, fullFileName);
metaFilePath = path.join(folder, filename+"(meta)."+fileExtension);
if (fs.existsSync(filePath) && fs.existsSync(metaFilePath)) return true;
}
}
return false;
}
function compressDecompress(option, path) {
if (fs.existsSync(path)){
if (option == "-c") {
let isFile = fs.lstatSync(path).isFile();
if (isFile) {
compression.compress(path)
}
else {
console.log("Unable to compress given path");
}
}
else if (option == "-d") {
if (decompressFolderBriefValidation(path)) {
compression.decompress(path);//check for valid file
}
else {
console.log("Unable to decompress given path");
}
}
}
else {
console.log("Unable to locate the file");
}
}
function touch(filePath) {
if(path.basename(filePath).split(".").length >= 2){
let directory = path.dirname(filePath);
if(!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
if(path.basename(filePath).length != 0){
fs.writeFileSync(filePath, "");
}
}
else {
fs.mkdirSync(filePath);
}
}
function tree(folder, prefix) {
let content = fs.readdirSync(folder);
for(let i=0; i<content.length; i++) {
let temp = path.join(folder, content[i]);
if (i == content.length-1) { //if last directory of the folder
console.log(prefix + "└───" + content[i]);
if(fs.lstatSync(temp).isDirectory()) tree(temp, prefix+" ");
}
else{
console.log(prefix + "├───" + content[i]);
if(fs.lstatSync(temp).isDirectory()) tree(temp, prefix+"│ ");
}
}
}