@@ -20,23 +20,16 @@ Licensed to the Apache Software Foundation (ASF) under one
20
20
21
21
import android .Manifest ;
22
22
import android .app .Activity ;
23
- import android .content .ContentResolver ;
24
23
import android .content .Context ;
25
24
import android .content .pm .PackageManager ;
26
25
import android .net .Uri ;
27
26
import android .os .Build ;
28
27
import android .os .Environment ;
29
28
import android .util .Base64 ;
30
- import android .util .Log ;
31
- import android .webkit .MimeTypeMap ;
32
- import android .webkit .WebResourceResponse ;
33
-
34
- import androidx .webkit .WebViewAssetLoader ;
35
29
36
30
import org .apache .cordova .CallbackContext ;
37
31
import org .apache .cordova .CordovaInterface ;
38
32
import org .apache .cordova .CordovaPlugin ;
39
- import org .apache .cordova .CordovaPluginPathHandler ;
40
33
import org .apache .cordova .CordovaWebView ;
41
34
import org .apache .cordova .LOG ;
42
35
import org .apache .cordova .PermissionHelper ;
@@ -46,16 +39,12 @@ Licensed to the Apache Software Foundation (ASF) under one
46
39
import org .json .JSONException ;
47
40
import org .json .JSONObject ;
48
41
49
- import java .io .BufferedInputStream ;
50
42
import java .io .ByteArrayOutputStream ;
51
43
import java .io .File ;
52
- import java .io .FileInputStream ;
53
44
import java .io .FileNotFoundException ;
54
45
import java .io .IOException ;
55
46
import java .io .InputStream ;
56
- import java .net .HttpURLConnection ;
57
47
import java .net .MalformedURLException ;
58
- import java .net .URL ;
59
48
import java .security .Permission ;
60
49
import java .util .ArrayList ;
61
50
import java .util .HashMap ;
@@ -99,6 +88,8 @@ public class FileUtils extends CordovaPlugin {
99
88
100
89
private PendingRequests pendingRequests ;
101
90
91
+
92
+
102
93
/*
103
94
* We need both read and write when accessing the storage, I think.
104
95
*/
@@ -146,10 +137,10 @@ protected void registerExtraFileSystems(String[] filesystems, HashMap<String, St
146
137
if (fsRoot != null ) {
147
138
File newRoot = new File (fsRoot );
148
139
if (newRoot .mkdirs () || newRoot .isDirectory ()) {
149
- registerFilesystem (new LocalFilesystem (fsName , webView .getContext (), webView .getResourceApi (), newRoot , preferences ));
140
+ registerFilesystem (new LocalFilesystem (fsName , webView .getContext (), webView .getResourceApi (), newRoot ));
150
141
installedFileSystems .add (fsName );
151
142
} else {
152
- LOG .d (LOG_TAG , "Unable to create root dir for filesystem \" " + fsName + "\" , skipping" );
143
+ LOG .d (LOG_TAG , "Unable to create root dir for filesystem \" " + fsName + "\" , skipping" );
153
144
}
154
145
} else {
155
146
LOG .d (LOG_TAG , "Unrecognized extra filesystem identifier: " + fsName );
@@ -227,10 +218,10 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
227
218
// Note: The temporary and persistent filesystems need to be the first two
228
219
// registered, so that they will match window.TEMPORARY and window.PERSISTENT,
229
220
// per spec.
230
- this .registerFilesystem (new LocalFilesystem ("temporary" , webView .getContext (), webView .getResourceApi (), tmpRootFile , preferences ));
231
- this .registerFilesystem (new LocalFilesystem ("persistent" , webView .getContext (), webView .getResourceApi (), persistentRootFile , preferences ));
232
- this .registerFilesystem (new ContentFilesystem (webView .getContext (), webView .getResourceApi (), preferences ));
233
- this .registerFilesystem (new AssetFilesystem (webView .getContext ().getAssets (), webView .getResourceApi (), preferences ));
221
+ this .registerFilesystem (new LocalFilesystem ("temporary" , webView .getContext (), webView .getResourceApi (), tmpRootFile ));
222
+ this .registerFilesystem (new LocalFilesystem ("persistent" , webView .getContext (), webView .getResourceApi (), persistentRootFile ));
223
+ this .registerFilesystem (new ContentFilesystem (webView .getContext (), webView .getResourceApi ()));
224
+ this .registerFilesystem (new AssetFilesystem (webView .getContext ().getAssets (), webView .getResourceApi ()));
234
225
235
226
registerExtraFileSystems (getExtraFileSystemsPreference (activity ), getAvailableFileSystems (activity ));
236
227
@@ -259,15 +250,13 @@ public Uri remapUri(Uri uri) {
259
250
if (!LocalFilesystemURL .FILESYSTEM_PROTOCOL .equals (uri .getScheme ())) {
260
251
return null ;
261
252
}
262
-
263
253
try {
264
254
LocalFilesystemURL inputURL = LocalFilesystemURL .parse (uri );
265
255
Filesystem fs = this .filesystemForURL (inputURL );
266
256
if (fs == null ) {
267
257
return null ;
268
258
}
269
259
String path = fs .filesystemPathForURL (inputURL );
270
-
271
260
if (path != null ) {
272
261
return Uri .parse ("file://" + fs .filesystemPathForURL (inputURL ));
273
262
}
@@ -282,7 +271,6 @@ public boolean execute(String action, final String rawArgs, final CallbackContex
282
271
callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR , "File plugin is not configured. Please see the README.md file for details on how to update config.xml" ));
283
272
return true ;
284
273
}
285
-
286
274
if (action .equals ("testSaveLocationExists" )) {
287
275
threadhelper (new FileOp () {
288
276
public void run (JSONArray args ) {
@@ -473,24 +461,18 @@ else if (action.equals("getFile")) {
473
461
public void run (JSONArray args ) throws FileExistsException , IOException , TypeMismatchException , EncodingException , JSONException {
474
462
String dirname = args .getString (0 );
475
463
String path = args .getString (1 );
464
+ String nativeURL = resolveLocalFileSystemURI (dirname ).getString ("nativeURL" );
465
+ boolean containsCreate = (args .isNull (2 )) ? false : args .getJSONObject (2 ).optBoolean ("create" , false );
476
466
477
- if (dirname .contains (LocalFilesystemURL .CDVFILE_KEYWORD ) == true ) {
467
+ if (containsCreate && needPermission (nativeURL , WRITE )) {
468
+ getWritePermission (rawArgs , ACTION_GET_FILE , callbackContext );
469
+ }
470
+ else if (!containsCreate && needPermission (nativeURL , READ )) {
471
+ getReadPermission (rawArgs , ACTION_GET_FILE , callbackContext );
472
+ }
473
+ else {
478
474
JSONObject obj = getFile (dirname , path , args .optJSONObject (2 ), false );
479
475
callbackContext .success (obj );
480
- } else {
481
- String nativeURL = resolveLocalFileSystemURI (dirname ).getString ("nativeURL" );
482
- boolean containsCreate = (args .isNull (2 )) ? false : args .getJSONObject (2 ).optBoolean ("create" , false );
483
-
484
- if (containsCreate && needPermission (nativeURL , WRITE )) {
485
- getWritePermission (rawArgs , ACTION_GET_FILE , callbackContext );
486
- }
487
- else if (!containsCreate && needPermission (nativeURL , READ )) {
488
- getReadPermission (rawArgs , ACTION_GET_FILE , callbackContext );
489
- }
490
- else {
491
- JSONObject obj = getFile (dirname , path , args .optJSONObject (2 ), false );
492
- callbackContext .success (obj );
493
- }
494
476
}
495
477
}
496
478
}, rawArgs , callbackContext );
@@ -904,7 +886,6 @@ private boolean remove(String baseURLstr) throws NoModificationAllowedException,
904
886
private JSONObject getFile (String baseURLstr , String path , JSONObject options , boolean directory ) throws FileExistsException , IOException , TypeMismatchException , EncodingException , JSONException {
905
887
try {
906
888
LocalFilesystemURL inputURL = LocalFilesystemURL .parse (baseURLstr );
907
-
908
889
Filesystem fs = this .filesystemForURL (inputURL );
909
890
if (fs == null ) {
910
891
throw new MalformedURLException ("No installed handlers for this URL" );
@@ -1258,68 +1239,4 @@ public void run(JSONArray args) throws FileNotFoundException, JSONException, Mal
1258
1239
LOG .d (LOG_TAG , "Received permission callback for unknown request code" );
1259
1240
}
1260
1241
}
1261
-
1262
- private String getMimeType (Uri uri ) {
1263
- String fileExtensionFromUrl = MimeTypeMap .getFileExtensionFromUrl (uri .toString ()).toLowerCase ();
1264
- return MimeTypeMap .getSingleton ().getMimeTypeFromExtension (fileExtensionFromUrl );
1265
- }
1266
-
1267
- public CordovaPluginPathHandler getPathHandler () {
1268
- WebViewAssetLoader .PathHandler pathHandler = path -> {
1269
- String targetFileSystem = null ;
1270
-
1271
- if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("persistent" ))) {
1272
- targetFileSystem = "persistent" ;
1273
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("temporary" ))) {
1274
- targetFileSystem = "temporary" ;
1275
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("files" ))) {
1276
- targetFileSystem = "files" ;
1277
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("documents" ))) {
1278
- targetFileSystem = "documents" ;
1279
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("cache" ))) {
1280
- targetFileSystem = "cache" ;
1281
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("root" ))) {
1282
- targetFileSystem = "root" ;
1283
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("files-external" ))) {
1284
- targetFileSystem = "files-external" ;
1285
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("sdcard" ))) {
1286
- targetFileSystem = "sdcard" ;
1287
- } else if (path .startsWith (LocalFilesystemURL .fsNameToCdvKeyword ("cache-external" ))) {
1288
- targetFileSystem = "cache-external" ;
1289
- }
1290
-
1291
- if (targetFileSystem != null ) {
1292
- // Loop the registered file systems to find the target.
1293
- for (Filesystem fileSystem : filesystems ) {
1294
-
1295
- /*
1296
- * When target is discovered:
1297
- * 1. Transform the url path to the native path
1298
- * 2. Load the file contents
1299
- * 3. Get the file mime type
1300
- * 4. Return the file & mime information back we Web Resources
1301
- */
1302
- if (fileSystem .name .equals (targetFileSystem )) {
1303
- // E.g. replace __cdvfile_persistent__ with native path "/data/user/0/com.example.file/files/files/"
1304
- String fileSystemNativeUri = fileSystem .rootUri .toString ().replace ("file://" , "" );
1305
- String fileTarget = path .replace (LocalFilesystemURL .fsNameToCdvKeyword (targetFileSystem ) + "/" , fileSystemNativeUri );
1306
-
1307
- File file = new File (fileTarget );
1308
-
1309
- try {
1310
- InputStream in = new FileInputStream (file );
1311
- String mimeType = getMimeType (Uri .parse (file .toString ()));
1312
- return new WebResourceResponse (mimeType , null , in );
1313
- } catch (FileNotFoundException e ) {
1314
- Log .e (LOG_TAG , e .getMessage ());
1315
- }
1316
- }
1317
- }
1318
- }
1319
-
1320
- return null ;
1321
- };
1322
-
1323
- return new CordovaPluginPathHandler (pathHandler );
1324
- }
1325
1242
}
0 commit comments