1+ const AbstractFile = require ( "./abstractClass" ) ;
2+ const FileClass = require ( "./fileClass" ) ;
3+ const nodePath = require ( 'path' ) ;
4+ const globalVariables = require ( './globalVar' ) ;
5+ const path = require ( 'path' ) ;
6+ const fs = require ( 'fs' ) ;
7+ const os = require ( 'os' ) ;
8+ module . exports = class FolderClass extends AbstractFile {
9+ #childrens = [ ] ;
10+ #setChildrens( ) {
11+ globalVariables . window . webContents . send ( 'fileUpdate' , this . currentPath ) ;
12+ let openedDir = [ ] ;
13+ try {
14+ openedDir = fs . readdirSync ( this . currentPath , { withFileTypes : true } ) ;
15+ }
16+ catch ( err ) {
17+ console . log ( "Can't open directory " + this . currentPath ) ;
18+ }
19+ if ( openedDir . length != 0 ) {
20+ for ( let i = 0 ; i < openedDir . length ; i ++ ) {
21+ let path = nodePath . join ( openedDir [ i ] . path , openedDir [ i ] . name ) ;
22+ if ( openedDir [ i ] . isDirectory ( ) ) {
23+ this . #childrens. push ( new FolderClass ( this , path ) ) ;
24+ }
25+ if ( openedDir [ i ] . isFile ( ) ) {
26+ this . #childrens. push ( new FileClass ( this , path ) ) ;
27+ }
28+ }
29+ }
30+ }
31+ findChild ( path ) {
32+ if ( path === this . currentPath ) {
33+ return this . #childrens;
34+ }
35+ let cuttedPath ;
36+ if ( this . currentPath == "/" ) {
37+ cuttedPath = path . replace ( this . currentPath , "" ) ;
38+ }
39+ else {
40+ if ( this . currentPath [ this . currentPath . length - 1 ] != globalVariables . separator ) {
41+ cuttedPath = path . replace ( this . currentPath + globalVariables . separator , "" ) ;
42+ }
43+ else {
44+ cuttedPath = path . replace ( this . currentPath , "" ) ;
45+ }
46+ }
47+ let arrFolders = cuttedPath . split ( globalVariables . separator ) ;
48+ for ( let i = 0 ; i < this . #childrens. length ; i ++ ) {
49+ if ( this . #childrens[ i ] . currentName === ( arrFolders [ 0 ] ) ) {
50+ return this . #childrens[ i ] . findChild ( path ) ;
51+ }
52+ }
53+ }
54+ openFolder ( path ) {
55+ let openedChildrens = this . findChild ( path ) ;
56+ let arr = [ ] ;
57+ for ( let i = 0 ; i < openedChildrens . length ; i ++ ) {
58+ arr . push ( {
59+ name :openedChildrens [ i ] . currentName ,
60+ path :openedChildrens [ i ] . currentPath ,
61+ size :openedChildrens [ i ] . _size ,
62+ type :openedChildrens [ i ] . type ,
63+
64+ } ) ;
65+ }
66+ let isLast = false ;
67+ if ( path == this . currentPath && this . parent == null ) {
68+ isLast = true ;
69+ }
70+ globalVariables . window . webContents . send ( 'getFileList' , path , arr , isLast ) ;
71+ }
72+ get getChildrens ( ) {
73+ return this . #childrens;
74+ }
75+ constructor ( parent , path ) {
76+ super ( parent , path , "folder" ) ;
77+ if ( os . platform == "linux" ) {
78+ //Linux mount system can let you mount one drive in another
79+ //For example /media/PATH are mounted inside system partition(/)
80+ //So why do we need to rescan already scanned folders?
81+ if ( globalVariables . alreadyOpened . length != 0 && parent != null ) {
82+ for ( let i = 0 ; i < globalVariables . alreadyOpened . length ; i ++ ) {
83+ if ( globalVariables . alreadyOpened [ i ] . currentPath == path ) {
84+ globalVariables . alreadyOpened [ i ] . _setParent ( parent ) ;
85+ this . _size = globalVariables . alreadyOpened [ i ] . _size ;
86+ parent . _size += this . _size ;
87+ this . #childrens = globalVariables . alreadyOpened [ i ] . getChildrens ;
88+ return ;
89+ }
90+ }
91+ }
92+ }
93+ if ( this . canRead ) {
94+ this . #setChildrens( ) ;
95+ }
96+ if ( parent != null ) {
97+ parent . _size += this . _size ;
98+ }
99+ else {
100+ globalVariables . window . webContents . send ( 'fileEnd' ) ;
101+ this . openFolder ( this . currentPath ) ;
102+ }
103+ }
104+
105+
106+ }
0 commit comments