@@ -20,23 +20,16 @@ Licensed to the Apache Software Foundation (ASF) under one
2020
2121import android .Manifest ;
2222import android .app .Activity ;
23- import android .content .ContentResolver ;
2423import android .content .Context ;
2524import android .content .pm .PackageManager ;
2625import android .net .Uri ;
2726import android .os .Build ;
2827import android .os .Environment ;
2928import android .util .Base64 ;
30- import android .util .Log ;
31- import android .webkit .MimeTypeMap ;
32- import android .webkit .WebResourceResponse ;
33-
34- import androidx .webkit .WebViewAssetLoader ;
3529
3630import org .apache .cordova .CallbackContext ;
3731import org .apache .cordova .CordovaInterface ;
3832import org .apache .cordova .CordovaPlugin ;
39- import org .apache .cordova .CordovaPluginPathHandler ;
4033import org .apache .cordova .CordovaWebView ;
4134import org .apache .cordova .LOG ;
4235import org .apache .cordova .PermissionHelper ;
@@ -46,16 +39,12 @@ Licensed to the Apache Software Foundation (ASF) under one
4639import org .json .JSONException ;
4740import org .json .JSONObject ;
4841
49- import java .io .BufferedInputStream ;
5042import java .io .ByteArrayOutputStream ;
5143import java .io .File ;
52- import java .io .FileInputStream ;
5344import java .io .FileNotFoundException ;
5445import java .io .IOException ;
5546import java .io .InputStream ;
56- import java .net .HttpURLConnection ;
5747import java .net .MalformedURLException ;
58- import java .net .URL ;
5948import java .security .Permission ;
6049import java .util .ArrayList ;
6150import java .util .HashMap ;
@@ -99,6 +88,8 @@ public class FileUtils extends CordovaPlugin {
9988
10089 private PendingRequests pendingRequests ;
10190
91+
92+
10293 /*
10394 * We need both read and write when accessing the storage, I think.
10495 */
@@ -146,10 +137,10 @@ protected void registerExtraFileSystems(String[] filesystems, HashMap<String, St
146137 if (fsRoot != null ) {
147138 File newRoot = new File (fsRoot );
148139 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 ));
150141 installedFileSystems .add (fsName );
151142 } 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" );
153144 }
154145 } else {
155146 LOG .d (LOG_TAG , "Unrecognized extra filesystem identifier: " + fsName );
@@ -227,10 +218,10 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
227218 // Note: The temporary and persistent filesystems need to be the first two
228219 // registered, so that they will match window.TEMPORARY and window.PERSISTENT,
229220 // 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 ()));
234225
235226 registerExtraFileSystems (getExtraFileSystemsPreference (activity ), getAvailableFileSystems (activity ));
236227
@@ -259,15 +250,13 @@ public Uri remapUri(Uri uri) {
259250 if (!LocalFilesystemURL .FILESYSTEM_PROTOCOL .equals (uri .getScheme ())) {
260251 return null ;
261252 }
262-
263253 try {
264254 LocalFilesystemURL inputURL = LocalFilesystemURL .parse (uri );
265255 Filesystem fs = this .filesystemForURL (inputURL );
266256 if (fs == null ) {
267257 return null ;
268258 }
269259 String path = fs .filesystemPathForURL (inputURL );
270-
271260 if (path != null ) {
272261 return Uri .parse ("file://" + fs .filesystemPathForURL (inputURL ));
273262 }
@@ -282,7 +271,6 @@ public boolean execute(String action, final String rawArgs, final CallbackContex
282271 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" ));
283272 return true ;
284273 }
285-
286274 if (action .equals ("testSaveLocationExists" )) {
287275 threadhelper (new FileOp () {
288276 public void run (JSONArray args ) {
@@ -473,24 +461,18 @@ else if (action.equals("getFile")) {
473461 public void run (JSONArray args ) throws FileExistsException , IOException , TypeMismatchException , EncodingException , JSONException {
474462 String dirname = args .getString (0 );
475463 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 );
476466
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 {
478474 JSONObject obj = getFile (dirname , path , args .optJSONObject (2 ), false );
479475 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- }
494476 }
495477 }
496478 }, rawArgs , callbackContext );
@@ -904,7 +886,6 @@ private boolean remove(String baseURLstr) throws NoModificationAllowedException,
904886 private JSONObject getFile (String baseURLstr , String path , JSONObject options , boolean directory ) throws FileExistsException , IOException , TypeMismatchException , EncodingException , JSONException {
905887 try {
906888 LocalFilesystemURL inputURL = LocalFilesystemURL .parse (baseURLstr );
907-
908889 Filesystem fs = this .filesystemForURL (inputURL );
909890 if (fs == null ) {
910891 throw new MalformedURLException ("No installed handlers for this URL" );
@@ -1258,68 +1239,4 @@ public void run(JSONArray args) throws FileNotFoundException, JSONException, Mal
12581239 LOG .d (LOG_TAG , "Received permission callback for unknown request code" );
12591240 }
12601241 }
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- }
13251242}
0 commit comments