1
1
module . exports = {
2
2
methods : {
3
- loadFoldersAndFiles : function ( path , fileExtension , filterMedia , callback ) {
4
- this . loadAllFolders ( path , callback , true , fileExtension , filterMedia ) ;
5
- } ,
6
- loadSubFoldersAndFiles : function ( path , fileExtension , filterMedia , callback ) {
7
- this . loadSubFoldersNotRecursive ( path , callback , true , fileExtension , filterMedia ) ;
8
- } ,
9
- loadFolders : function ( path , callback ) {
10
- this . loadAllFolders ( path , callback , false , '' , false ) ;
3
+ loadSubFoldersAndFiles : function ( path , fileExtension , filterMedia , fileFilters , callback ) {
4
+ this . loadSubFoldersNotRecursive ( path , callback , true , fileExtension , filterMedia , fileFilters ) ;
11
5
} ,
12
6
loadSubFolders : function ( path , callback ) {
13
- this . loadSubFoldersNotRecursive ( path , callback , false , '' , false ) ;
14
- } ,
15
- loadAllFolders : function ( path , callback , includeFiles , fileExtension , filterMedia ) {
16
- var that = this ;
17
- let folderTree = { } ;
18
- this . context . getByPath ( path ) . thenApply ( function ( dirOpt ) {
19
- let dir = dirOpt . get ( ) ;
20
- let folderProperties = dir . getFileProperties ( ) ;
21
- if ( folderProperties . isDirectory && ! folderProperties . isHidden ) {
22
- that . walk ( dir , path , folderTree , includeFiles , fileExtension , filterMedia ) . thenApply ( ( ) => {
23
- callback ( folderTree ) ;
24
- } ) ;
25
- } else {
26
- callback ( folderTree ) ;
27
- }
28
- } ) . exceptionally ( function ( throwable ) {
29
- this . spinnerMessage = 'Unable to load folders...' ;
30
- throwable . printStackTrace ( ) ;
31
- } ) ;
7
+ this . loadSubFoldersNotRecursive ( path , callback , false , '' , false , null ) ;
32
8
} ,
33
9
34
- walk : function ( file , path , currentTreeData , includeFiles , fileExtension , filterMedia ) {
35
- currentTreeData . path = path . substring ( 0 , path . length - 1 ) ;
36
- currentTreeData . children = [ ] ;
37
- currentTreeData . isOpen = false ;
38
- let that = this ;
39
- let future = peergos . shared . util . Futures . incomplete ( ) ;
40
- file . getChildren ( that . context . crypto . hasher , that . context . network ) . thenApply ( function ( children ) {
41
- let arr = children . toArray ( ) ;
42
- let funcArray = [ ] ;
43
- arr . forEach ( function ( child , index ) {
44
- let childProps = child . getFileProperties ( ) ;
45
- let newPath = childProps . isDirectory ? path + child . getFileProperties ( ) . name + '/' : path ;
46
- if ( childProps . isDirectory && ! childProps . isHidden ) {
47
- let node = { } ;
48
- currentTreeData . children . push ( node ) ;
49
- funcArray . push ( ( ) => {
50
- return that . walk ( child , newPath , node , includeFiles , fileExtension , filterMedia ) ;
51
- } ) ;
52
- }
53
- if ( includeFiles === true && ! childProps . isDirectory && ! childProps . isHidden ) {
54
- let testAcceptAll = ( fileExtension == null || fileExtension . length == 0 ) && ! filterMedia ;
55
- var matchExtension = false ;
56
- if ( ! testAcceptAll ) {
57
- if ( filterMedia ) {
58
- let mimeType = childProps . mimeType ;
59
- matchExtension = mimeType . startsWith ( "image" ) || mimeType . startsWith ( "video" ) ;
60
- }
61
- if ( ! matchExtension ) {
62
- let extensions = fileExtension . split ( ',' ) . filter ( e => e . length > 0 ) . map ( i => i . toLowerCase ( ) . trim ( ) ) ;
63
- let matches = extensions . filter ( ext => child . getFileProperties ( ) . name . toLowerCase ( ) . endsWith ( ext ) ) ;
64
- matchExtension = matches . length > 0 ;
65
- }
66
- }
67
- if ( testAcceptAll || matchExtension ) {
68
- let node = { } ;
69
- currentTreeData . children . push ( node ) ;
70
- funcArray . push ( ( ) => {
71
- return that . addFile ( child , newPath , node ) ;
72
- } ) ;
73
- }
74
- }
75
- } ) ;
76
- if ( funcArray . length > 0 ) {
77
- let completed = { count : 0 } ;
78
- funcArray . forEach ( ( func , idx ) => {
79
- func ( ) . thenApply ( ( ) => {
80
- completed . count ++ ;
81
- if ( completed . count == funcArray . length ) {
82
- future . complete ( true ) ;
83
- }
84
- } ) ;
85
- } ) ;
86
- } else {
87
- future . complete ( true ) ;
88
- }
89
- } ) ;
90
- return future ;
91
- } ,
92
- addFile ( file , path , currentTreeData ) {
93
- let future = peergos . shared . util . Futures . incomplete ( ) ;
94
- currentTreeData . path = path + file . getName ( ) ;
95
- currentTreeData . children = [ ] ;
96
- currentTreeData . isLeaf = true ;
97
- currentTreeData . isOpen = false ;
98
- future . complete ( true ) ;
99
- return future ;
100
- } ,
101
- loadSubFoldersNotRecursive : function ( path , callback , includeFiles , fileExtension , filterMedia ) {
10
+ loadSubFoldersNotRecursive : function ( path , callback , includeFiles , fileExtension , filterMedia , fileFilters ) {
102
11
var that = this ;
103
12
let folderTree = { } ;
104
13
this . context . getByPath ( path ) . thenApply ( function ( dirOpt ) {
105
14
let dir = dirOpt . get ( ) ;
106
15
let folderProperties = dir . getFileProperties ( ) ;
107
16
if ( folderProperties . isDirectory && ! folderProperties . isHidden ) {
108
- that . walkNotRecursive ( dir , path , folderTree , includeFiles , fileExtension , filterMedia ) . thenApply ( ( ) => {
17
+ that . walkNotRecursive ( dir , path , folderTree , includeFiles , fileExtension , filterMedia , fileFilters ) . thenApply ( ( ) => {
109
18
callback ( folderTree ) ;
110
19
} ) ;
111
20
} else {
@@ -117,7 +26,7 @@ module.exports = {
117
26
} ) ;
118
27
} ,
119
28
120
- walkNotRecursive : function ( file , path , currentTreeData , includeFiles , fileExtension , filterMedia ) {
29
+ walkNotRecursive : function ( file , path , currentTreeData , includeFiles , fileExtension , filterMedia , fileFilters ) {
121
30
currentTreeData . path = path . substring ( 0 , path . length - 1 ) ;
122
31
currentTreeData . children = [ ] ;
123
32
currentTreeData . isOpen = false ;
@@ -137,20 +46,35 @@ module.exports = {
137
46
currentTreeData . children . push ( node ) ;
138
47
}
139
48
if ( includeFiles === true && ! childProps . isDirectory && ! childProps . isHidden ) {
140
- let testAcceptAll = ( fileExtension == null || fileExtension . length == 0 ) && ! filterMedia ;
141
- var matchExtension = false ;
142
- if ( ! testAcceptAll ) {
143
- if ( filterMedia ) {
144
- let mimeType = childProps . mimeType ;
145
- matchExtension = mimeType . startsWith ( "image" ) || mimeType . startsWith ( "video" ) ;
146
- }
147
- if ( ! matchExtension ) {
148
- let extensions = fileExtension . split ( ',' ) . filter ( e => e . length > 0 ) . map ( i => i . toLowerCase ( ) . trim ( ) ) ;
149
- let matches = extensions . filter ( ext => child . getFileProperties ( ) . name . toLowerCase ( ) . endsWith ( ext ) ) ;
150
- matchExtension = matches . length > 0 ;
49
+ var fileMatch = false ;
50
+ if ( fileFilters != null ) { //{fileExtensions: app.fileExtensions, mimeTypes: app.mimeTypes, fileTypes: app.fileTypes}
51
+ let wildcardInclude = fileFilters . fileExtensions . length == 1 && fileFilters . fileExtensions [ 0 ] == '*'
52
+ || fileFilters . mimeTypes . length == 1 && fileFilters . mimeTypes [ 0 ] == '*'
53
+ || fileFilters . fileTypes . length == 1 && fileFilters . fileTypes [ 0 ] == '*'
54
+ let props = child . getFileProperties ( ) ;
55
+ let filename = props . name . toLowerCase ( ) . trim ( ) ;
56
+ let extensionMatches = fileFilters . fileExtensions . filter ( ext => filename . endsWith ( ext . toLowerCase ( ) . trim ( ) ) ) . length > 0 ;
57
+ let mimeTypesMatches = fileFilters . mimeTypes . filter ( mimeType => props . mimeType == mimeType ) . length > 0 ;
58
+ let fileType = props . getType ( ) ;
59
+ let fileTypesMatches = fileFilters . fileTypes . filter ( ft => fileType == ft ) . length > 0 ;
60
+ fileMatch = wildcardInclude || extensionMatches || mimeTypesMatches || fileTypesMatches ;
61
+ } else {
62
+ let testAcceptAll = ( fileExtension == null || fileExtension . length == 0 ) && ! filterMedia ;
63
+ var matchExtension = false ;
64
+ if ( ! testAcceptAll ) {
65
+ if ( filterMedia ) {
66
+ let mimeType = childProps . mimeType ;
67
+ matchExtension = mimeType . startsWith ( "image" ) || mimeType . startsWith ( "video" ) ;
68
+ }
69
+ if ( ! matchExtension ) {
70
+ let extensions = fileExtension . split ( ',' ) . filter ( e => e . length > 0 ) . map ( i => i . toLowerCase ( ) . trim ( ) ) ;
71
+ let matches = extensions . filter ( ext => child . getFileProperties ( ) . name . toLowerCase ( ) . endsWith ( ext ) ) ;
72
+ matchExtension = matches . length > 0 ;
73
+ }
151
74
}
75
+ fileMatch = testAcceptAll || matchExtension ;
152
76
}
153
- if ( testAcceptAll || matchExtension ) {
77
+ if ( fileMatch ) {
154
78
let node = { } ;
155
79
node . path = newPath + child . getName ( ) ;
156
80
node . children = [ ] ;
0 commit comments