@@ -61,18 +61,45 @@ export class UUIFileDropzoneElement extends LabelMixin('', LitElement) {
61
61
@query ( '#dropzone' )
62
62
private _dropzone ! : HTMLElement ;
63
63
64
+ private _acceptedFileExtensions : string [ ] = [ ] ;
65
+ private _acceptedMimeTypes : string [ ] = [ ] ;
66
+
64
67
/**
65
- * Comma-separated list of accepted filetypes. Will allow all types if empty.
68
+ * Comma-separated list of accepted mime types or file extensions (denoted with a `.`).
69
+ * If this is left empty, it will allow all types.
70
+ *
66
71
* @type {string }
67
72
* @attr
68
- * @default false
69
73
* @examples [
70
- * "image/png,image/jpeg,image/gif ",
71
- * "gif,png,jpg,jpeg",
74
+ * "image/*,application/pdf ",
75
+ * ". gif,. png,. jpg,. jpeg,.pdf ",
72
76
* ]
73
77
*/
74
78
@property ( { type : String } )
75
- public accept : string = '' ;
79
+ public set accept ( value : string ) {
80
+ if ( value ) {
81
+ const mimetypes : string [ ] = [ ] ;
82
+ const fileextensions : string [ ] = [ ] ;
83
+
84
+ // Create the arrays defined above
85
+ value . split ( ',' ) . forEach ( item => {
86
+ item = item . trim ( ) . toLowerCase ( ) ;
87
+
88
+ // If the item is a mime type, add it to the accept list
89
+ if ( / [ a - z ] + \/ [ a - z * ] / s. test ( item ) ) {
90
+ mimetypes . push ( item ) ;
91
+ } else {
92
+ fileextensions . push ( item . replace ( / ^ \. / , '' ) ) ;
93
+ }
94
+ } ) ;
95
+
96
+ this . _acceptedMimeTypes = mimetypes ;
97
+ this . _acceptedFileExtensions = fileextensions ;
98
+ } else {
99
+ this . _acceptedMimeTypes = [ ] ;
100
+ this . _acceptedFileExtensions = [ ] ;
101
+ }
102
+ }
76
103
77
104
/**
78
105
* Allows for multiple files to be selected.
@@ -105,40 +132,20 @@ export class UUIFileDropzoneElement extends LabelMixin('', LitElement) {
105
132
demandCustomElement ( this , 'uui-symbol-file-dropzone' ) ;
106
133
}
107
134
108
- protected _checkIsItDirectory ( dtItem : DataTransferItem ) : boolean {
109
- // @ts -ignore // TODO: fix typescript error
110
- return ! dtItem . type ? dtItem . webkitGetAsEntry ( ) . isDirectory : false ;
111
- }
112
-
113
135
private async _getAllFileEntries (
114
136
dataTransferItemList : DataTransferItemList
115
137
) : Promise < File [ ] > {
116
138
const fileEntries : File [ ] = [ ] ;
117
139
// Use BFS to traverse entire directory/file structure
118
140
const queue = [ ...dataTransferItemList ] ;
119
141
120
- const acceptList : string [ ] = [ ] ;
121
- const wildcards : string [ ] = [ ] ;
122
-
123
- // if the accept filer is set
124
- if ( this . accept ) {
125
- // Create the arrays defined above
126
- this . accept . split ( ',' ) . forEach ( item => {
127
- if ( item . includes ( '*' ) ) {
128
- wildcards . push ( item . split ( '*' ) [ 0 ] . trim ( ) . toLowerCase ( ) ) ;
129
- } else {
130
- acceptList . push ( item . trim ( ) . toLowerCase ( ) ) ;
131
- }
132
- } ) ;
133
- }
134
-
135
142
while ( queue . length > 0 ) {
136
143
const entry = queue . shift ( ) ! ;
137
144
138
145
if ( entry . kind === 'file' ) {
139
146
const file = entry . getAsFile ( ) ;
140
147
if ( ! file ) continue ;
141
- if ( this . _isAccepted ( acceptList , wildcards , file ) ) {
148
+ if ( this . _isAccepted ( file ) ) {
142
149
fileEntries . push ( file ) ;
143
150
}
144
151
} else if ( entry . kind === 'directory' ) {
@@ -179,24 +186,33 @@ export class UUIFileDropzoneElement extends LabelMixin('', LitElement) {
179
186
}
180
187
}
181
188
182
- private _isAccepted ( acceptList : string [ ] , wildcards : string [ ] , file : File ) {
183
- if ( acceptList . length === 0 && wildcards . length === 0 ) {
189
+ private _isAccepted ( file : File ) {
190
+ if (
191
+ this . _acceptedFileExtensions . length === 0 &&
192
+ this . _acceptedMimeTypes . length === 0
193
+ ) {
184
194
return true ;
185
195
}
186
196
187
197
const fileType = file . type . toLowerCase ( ) ;
188
- const fileExtension = '.' + file . name . split ( '.' ) [ 1 ] . toLowerCase ( ) ;
189
-
190
- if ( acceptList . includes ( fileExtension ) ) {
191
- return true ;
192
- }
198
+ const fileExtension = file . name . split ( '.' ) . pop ( ) ;
193
199
194
- if ( acceptList . includes ( fileType ) ) {
200
+ if (
201
+ fileExtension &&
202
+ this . _acceptedFileExtensions . includes ( fileExtension . toLowerCase ( ) )
203
+ ) {
195
204
return true ;
196
205
}
197
206
198
- if ( wildcards . some ( wildcard => fileType . startsWith ( wildcard ) ) ) {
199
- return true ;
207
+ for ( const mimeType in this . _acceptedMimeTypes ) {
208
+ if ( fileType === mimeType ) {
209
+ return true ;
210
+ } else if (
211
+ mimeType . endsWith ( '/*' ) &&
212
+ fileType . startsWith ( mimeType . replace ( '/*' , '' ) )
213
+ ) {
214
+ return true ;
215
+ }
200
216
}
201
217
202
218
return false ;
0 commit comments