-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgs-utils-files.js
More file actions
66 lines (56 loc) · 1.73 KB
/
Copy pathgs-utils-files.js
File metadata and controls
66 lines (56 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"use strict";
function GSUloadJSFile( src ) { return $.$loadJS( src ); }
function GSUdownloadURL( name, url ) { $.$downloadURL( name, url ); }
function GSUdownloadBlob( name, blob ) { $.$downloadBlob( name, blob ); }
function GSUgetFileContent( file, format ) {
return new Promise( res => {
const rd = new FileReader();
rd.onload = e => res( e.target.result );
switch ( format ) {
case "text": rd.readAsText( file ); break;
case "array": rd.readAsArrayBuffer( file ); break;
}
} );
}
const GSUopenFileManager = () => new Promise( res => {
GSUopenFileManager.$inputRes = res;
GSUopenFileManager.$input.$click();
} );
GSUopenFileManager.$inputRes = null;
GSUopenFileManager.$callback = e => GSUopenFileManager.$inputRes( e.target.files );
GSUopenFileManager.$input = $( "<input>" )
.$setAttr( "type", "file" )
.$onchange( GSUopenFileManager.$callback );
function GSUgetFilesDataTransfert_rec( files, item, path = "" ) {
return new Promise( res => {
if ( item.isFile ) {
item.file( f => {
f.filepath = path + f.name;
files.push( f );
res( f );
} );
} else if ( item.isDirectory ) {
const dirReader = item.createReader();
dirReader.readEntries( entries => {
const proms = [];
for ( const ent of entries ) {
proms.push( GSUgetFilesDataTransfert_rec( files, ent, `${ path }${ item.name }/` ) );
}
res( Promise.all( proms ) );
} );
}
} );
}
function GSUgetFilesDataTransfert( dataTransferItems ) {
const files = [];
return new Promise( res => {
const proms = [];
for ( const it of dataTransferItems ) {
const ent = it.webkitGetAsEntry();
if ( ent ) {
proms.push( GSUgetFilesDataTransfert_rec( files, ent ) );
}
}
Promise.all( proms ).then( () => res( files ) );
} );
}