@reldens/server-utils 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/file-handler.js +244 -0
  2. package/package.json +1 -1
@@ -22,6 +22,8 @@ class FileHandler
22
22
  'C:\\Windows\\', 'C:\\System32\\',
23
23
  '%2e%2e%2f', '%2e%2e%5c'
24
24
  ];
25
+ this.nativeHandler = fs;
26
+ this.nativePaths = path;
25
27
  }
26
28
 
27
29
  joinPaths(...args)
@@ -29,6 +31,24 @@ class FileHandler
29
31
  return path.join(...args);
30
32
  }
31
33
 
34
+ getFileName(filePath)
35
+ {
36
+ if(!this.isValidPath(filePath)){
37
+ this.error = {message: 'Invalid file path.', filePath};
38
+ return false;
39
+ }
40
+ return path.basename(filePath);
41
+ }
42
+
43
+ getFolderName(filePath)
44
+ {
45
+ if(!this.isValidPath(filePath)){
46
+ this.error = {message: 'Invalid file path.', filePath};
47
+ return false;
48
+ }
49
+ return path.dirname(filePath);
50
+ }
51
+
32
52
  exists(fullPath)
33
53
  {
34
54
  if(!this.isValidPath(fullPath)){
@@ -507,6 +527,230 @@ class FileHandler
507
527
  return this.joinPaths(tempDir, fileName);
508
528
  }
509
529
 
530
+ moveFile(from, to)
531
+ {
532
+ if(!this.isValidPath(from) || !this.isValidPath(to)){
533
+ this.error = {message: 'Invalid path for file move.', from, to};
534
+ return false;
535
+ }
536
+ if(!this.exists(from)){
537
+ this.error = {message: 'Source file does not exist.', from};
538
+ return false;
539
+ }
540
+ try {
541
+ fs.renameSync(from, to);
542
+ return true;
543
+ } catch (error) {
544
+ this.error = {message: 'Failed to move file.', error, from, to};
545
+ return false;
546
+ }
547
+ }
548
+
549
+ getFileSize(filePath)
550
+ {
551
+ if(!this.isValidPath(filePath)){
552
+ this.error = {message: 'Invalid file path.', filePath};
553
+ return false;
554
+ }
555
+ if(!this.exists(filePath)){
556
+ this.error = {message: 'File does not exist.', filePath};
557
+ return false;
558
+ }
559
+ try {
560
+ return fs.statSync(filePath).size;
561
+ } catch (error) {
562
+ this.error = {message: 'Failed to get file size.', error, filePath};
563
+ return false;
564
+ }
565
+ }
566
+
567
+ compareFiles(file1, file2)
568
+ {
569
+ if(!this.isValidPath(file1) || !this.isValidPath(file2)){
570
+ this.error = {message: 'Invalid file paths for comparison.', file1, file2};
571
+ return false;
572
+ }
573
+ if(!this.exists(file1) || !this.exists(file2)){
574
+ this.error = {message: 'One or both files do not exist.', file1, file2};
575
+ return false;
576
+ }
577
+ try {
578
+ let content1 = this.readFile(file1);
579
+ let content2 = this.readFile(file2);
580
+ if(!content1 || !content2){
581
+ return false;
582
+ }
583
+ return content1 === content2;
584
+ } catch (error) {
585
+ this.error = {message: 'Failed to compare files.', error, file1, file2};
586
+ return false;
587
+ }
588
+ }
589
+
590
+ getRelativePath(from, to)
591
+ {
592
+ if(!this.isValidPath(from) || !this.isValidPath(to)){
593
+ this.error = {message: 'Invalid paths for relative calculation.', from, to};
594
+ return false;
595
+ }
596
+ try {
597
+ return path.relative(from, to);
598
+ } catch (error) {
599
+ this.error = {message: 'Failed to calculate relative path.', error, from, to};
600
+ return false;
601
+ }
602
+ }
603
+
604
+ isAbsolutePath(filePath)
605
+ {
606
+ if(!this.isValidPath(filePath)){
607
+ this.error = {message: 'Invalid file path.', filePath};
608
+ return false;
609
+ }
610
+ return path.isAbsolute(filePath);
611
+ }
612
+
613
+ normalizePath(filePath)
614
+ {
615
+ if(!filePath){
616
+ this.error = {message: 'Path cannot be empty.', filePath};
617
+ return false;
618
+ }
619
+ try {
620
+ return path.normalize(filePath);
621
+ } catch (error) {
622
+ this.error = {message: 'Failed to normalize path.', error, filePath};
623
+ return false;
624
+ }
625
+ }
626
+
627
+ walkDirectory(dirPath, callback)
628
+ {
629
+ if(!this.isValidPath(dirPath)){
630
+ this.error = {message: 'Invalid directory path.', dirPath};
631
+ return false;
632
+ }
633
+ if(!this.isFolder(dirPath)){
634
+ this.error = {message: 'Path is not a directory.', dirPath};
635
+ return false;
636
+ }
637
+ if('function' !== typeof callback){
638
+ this.error = {message: 'Callback must be a function.', dirPath};
639
+ return false;
640
+ }
641
+ try {
642
+ let items = this.readFolder(dirPath);
643
+ for(let item of items){
644
+ let itemPath = this.joinPaths(dirPath, item);
645
+ callback(itemPath);
646
+ if(this.isFolder(itemPath)){
647
+ this.walkDirectory(itemPath, callback);
648
+ }
649
+ }
650
+ return true;
651
+ } catch (error) {
652
+ this.error = {message: 'Failed to walk directory.', error, dirPath};
653
+ return false;
654
+ }
655
+ }
656
+
657
+ getDirectorySize(dirPath)
658
+ {
659
+ if(!this.isValidPath(dirPath)){
660
+ this.error = {message: 'Invalid directory path.', dirPath};
661
+ return false;
662
+ }
663
+ if(!this.isFolder(dirPath)){
664
+ this.error = {message: 'Path is not a directory.', dirPath};
665
+ return false;
666
+ }
667
+ let totalSize = 0;
668
+ let calculateSize = (itemPath) => {
669
+ if(this.isFile(itemPath)){
670
+ let size = this.getFileSize(itemPath);
671
+ if(false !== size){
672
+ totalSize += size;
673
+ }
674
+ }
675
+ };
676
+ let walkResult = this.walkDirectory(dirPath, calculateSize);
677
+ if(!walkResult){
678
+ return false;
679
+ }
680
+ return totalSize;
681
+ }
682
+
683
+ emptyDirectory(dirPath)
684
+ {
685
+ if(!this.isValidPath(dirPath)){
686
+ this.error = {message: 'Invalid directory path.', dirPath};
687
+ return false;
688
+ }
689
+ if(!this.isFolder(dirPath)){
690
+ this.error = {message: 'Path is not a directory.', dirPath};
691
+ return false;
692
+ }
693
+ try {
694
+ let items = this.readFolder(dirPath);
695
+ for(let item of items){
696
+ let itemPath = this.joinPaths(dirPath, item);
697
+ this.remove(itemPath);
698
+ }
699
+ return true;
700
+ } catch (error) {
701
+ this.error = {message: 'Failed to empty directory.', error, dirPath};
702
+ return false;
703
+ }
704
+ }
705
+
706
+ appendToFile(filePath, content)
707
+ {
708
+ if(!this.isValidPath(filePath)){
709
+ this.error = {message: 'Invalid file path.', filePath};
710
+ return false;
711
+ }
712
+ try {
713
+ fs.appendFileSync(filePath, content, this.encoding);
714
+ return true;
715
+ } catch (error) {
716
+ this.error = {message: 'Failed to append to file.', error, filePath};
717
+ return false;
718
+ }
719
+ }
720
+
721
+ prependToFile(filePath, content)
722
+ {
723
+ if(!this.isValidPath(filePath)){
724
+ this.error = {message: 'Invalid file path.', filePath};
725
+ return false;
726
+ }
727
+ if(!this.exists(filePath)){
728
+ return this.writeFile(filePath, content);
729
+ }
730
+ let existingContent = this.readFile(filePath);
731
+ if(!existingContent){
732
+ return false;
733
+ }
734
+ return this.writeFile(filePath, content + existingContent);
735
+ }
736
+
737
+ replaceInFile(filePath, searchValue, replaceValue)
738
+ {
739
+ if(!this.isValidPath(filePath)){
740
+ this.error = {message: 'Invalid file path.', filePath};
741
+ return false;
742
+ }
743
+ if(!this.exists(filePath)){
744
+ this.error = {message: 'File does not exist.', filePath};
745
+ return false;
746
+ }
747
+ let content = this.readFile(filePath);
748
+ if(!content){
749
+ return false;
750
+ }
751
+ return this.writeFile(filePath, content.replace(searchValue, replaceValue));
752
+ }
753
+
510
754
  }
511
755
 
512
756
  module.exports.FileHandler = new FileHandler();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/server-utils",
3
3
  "scope": "@reldens",
4
- "version": "0.23.0",
4
+ "version": "0.24.0",
5
5
  "description": "Reldens - Server Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",