File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,16 @@ describe('toFile()', () => {
29
29
expect ( fileWithPath . path ) . toBe ( name ) ;
30
30
} ) ;
31
31
32
+ it ( 'uses the File {webkitRelativePath} as {path} if it exists' , ( ) => {
33
+ const path = 'test/test.json' ;
34
+ const file = new File ( [ ] , name ) ;
35
+ Object . defineProperty ( file , 'webkitRelativePath' , {
36
+ value : path
37
+ } ) ;
38
+ const fileWithPath = toFileWithPath ( file ) ;
39
+ expect ( fileWithPath . path ) . toBe ( path ) ;
40
+ } ) ;
41
+
32
42
it ( 'sets the {type} from extension' , ( ) => {
33
43
const types = Array . from ( COMMON_MIME_TYPES . values ( ) ) ;
34
44
const files = Array . from ( COMMON_MIME_TYPES . keys ( ) )
Original file line number Diff line number Diff line change @@ -17,8 +17,16 @@ export const COMMON_MIME_TYPES = new Map([
17
17
18
18
export function toFileWithPath ( file : File , path ?: string ) : FileWithPath {
19
19
const f = withMimeType ( file ) ;
20
+ const { webkitRelativePath} = file as FileWithWebkitPath ;
20
21
Object . defineProperty ( f , 'path' , {
21
- value : typeof path === 'string' ? path : file . name ,
22
+ value : typeof path === 'string'
23
+ ? path
24
+ // If <input webkitdirectory> is set,
25
+ // the File will have a {webkitRelativePath} property
26
+ // https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
27
+ : typeof webkitRelativePath === 'string' && webkitRelativePath . length > 0
28
+ ? webkitRelativePath
29
+ : file . name ,
22
30
writable : false ,
23
31
configurable : false
24
32
} ) ;
@@ -29,6 +37,9 @@ export interface FileWithPath extends File {
29
37
readonly path ?: string ;
30
38
}
31
39
40
+ interface FileWithWebkitPath extends File {
41
+ readonly webkitRelativePath ?: string ;
42
+ }
32
43
33
44
function withMimeType ( file : File ) {
34
45
const { name} = file ;
You can’t perform that action at this time.
0 commit comments