diff --git a/README.md b/README.md index a29a068..0b05b00 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Read and save files using the Storage Access Framework and Mediastore -This plugin allows you to read and save files using the Storage Access Framework and Mediastore on Android only. +This plugin allows you to read and save files using the Storage Access Framework and Mediastore on Android only. Added copy from file method and some fixes. ## Available methods @@ -41,6 +41,16 @@ writeFile(params:{ ``` Writes a file to a specific filename, with the folder and subfolder being optional. The subfolder will be created if it does not exist, and the default folder is the Downloads folder (saved via Mediastore). Returns the content URI. ```data``` is a Base 64 string. +```typescript +copyFile(params:{ + srcfile:string, + filename:string, + folder?:string, + subFolder?:string +}):Promise +``` +Copies srcfile's contents to a specific filename, with the folder and subfolder being optional. The subfolder will be created if it does not exist, and the default folder is the Downloads folder (saved via Mediastore). Returns the content URI. ```srcfile``` is cordova source file name. It may be more memory friendly than handling hundreds of MB of base64. + ```typescript overwriteFile(params:{ uri:string, @@ -81,4 +91,4 @@ To call methods: ```typescript cordova.plugins.safMediastore.(params); //returns a Promise await cordova.plugins.safMediastore.(params); //in an async function -``` \ No newline at end of file +``` diff --git a/plugin.xml b/plugin.xml index 5d91df8..d0a684f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -29,4 +29,5 @@ + diff --git a/src/android/com/customautosys/saf_mediastore/SafMediastore.java b/src/android/com/customautosys/saf_mediastore/SafMediastore.java index 8e61077..59df7db 100644 --- a/src/android/com/customautosys/saf_mediastore/SafMediastore.java +++ b/src/android/com/customautosys/saf_mediastore/SafMediastore.java @@ -32,6 +32,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; +import java.io.FileNotFoundException; import java.io.StringWriter; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -153,6 +154,147 @@ public boolean readFile(JSONArray args,CallbackContext callbackContext){ } } + public boolean copyFile(JSONArray args,CallbackContext callbackContext){ + try{ + JSONObject params=args.getJSONObject(0); + String filename=params.getString("filename"); + String mimeType=MimeTypeMap.getSingleton().getMimeTypeFromExtension(filename.substring(filename.lastIndexOf('.')+1)); + boolean forceoverwrite = false; + try { + forceoverwrite = params.getBoolean("overwrite"); + } catch(Exception ex) { ; } + //debugLog(forceoverwrite ? "saf OVERWRITE T " : "saf OVERWRITE F "); + if(mimeType==null)mimeType="*/*"; + String folder=null; + try{ + if(!params.isNull("folder"))folder=params.getString("folder"); + }catch(Exception e){ + debugLog(e); + } + String subFolder=""; + try{ + if(!params.isNull("subFolder"))subFolder=params.getString("subFolder"); + }catch(Exception e){ + debugLog(e); + } + Uri uri=null; + if(folder!=null&&!folder.trim().equals("")){ + DocumentFile documentFile=DocumentFile.fromTreeUri( + cordovaInterface.getContext(), + Uri.parse(folder) + ); + if(subFolder!=""){ + String subFolders[]=subFolder.split("/"); + for(int i=0;i 0) { + outputStream.write(buff, off, rr); + } + //debugLog("E X X X 5"); + callbackContext.success(uri.toString()); + return true; + }catch(Exception e) { + debugLog("saf copy exc copy empty " + e.toString()); + callbackContext.error(e.toString()); + return false; + } + } + } + catch (FileNotFoundException e) { + debugLog("saf copy filenotfound " + e.toString()); + int off = 0; + int len = 0; + int rr = -1; + byte[] buff = new byte[1000000]; + try( + InputStream inputStream=cordovaInterface.getContext().getContentResolver().openInputStream(Uri.parse(params.getString("srcfile"))); + OutputStream outputStream=cordovaInterface.getContext().getContentResolver().openOutputStream(uri,"wt")){ + while ((rr = inputStream.read(buff, off, 1000000)) > 0) { + outputStream.write(buff, off, rr); + } + callbackContext.success(uri.toString()); + return true; + }catch(Exception ex2) { + debugLog("saf copy exc copy fnf " + ex2.toString()); + callbackContext.error(ex2.toString()); + return false; + } + } + }catch(Exception e){ + debugLog("saf copy exception"); + debugLog(e); + callbackContext.error(e.toString()); + return false; + } + } + public boolean writeFile(JSONArray args,CallbackContext callbackContext){ try{ JSONObject params=args.getJSONObject(0); @@ -177,7 +319,7 @@ public boolean writeFile(JSONArray args,CallbackContext callbackContext){ cordovaInterface.getContext(), Uri.parse(folder) ); - if(subFolder!=null){ + if(subFolder!=""){ String subFolders[]=subFolder.split("/"); for(int i=0;i, openFile(uri:string):Promise, readFile(uri:string):Promise, + copyFile(params:{ + data:string, + filename:string, + folder?:string, + subFolder?:string + }):Promise, writeFile(params:{ data:string, filename:string, @@ -30,4 +36,4 @@ interface SafMediastore{ interface CordovaPlugins{ safMediastore:SafMediastore -} \ No newline at end of file +} diff --git a/www/safMediastore.js b/www/safMediastore.js index 8e1699f..0eb8122 100644 --- a/www/safMediastore.js +++ b/www/safMediastore.js @@ -25,6 +25,7 @@ module.exports=(function(){ 'openFile', 'readFile', 'writeFile', + 'copyFile', 'overwriteFile', 'saveFile', 'deleteFile', @@ -33,4 +34,4 @@ module.exports=(function(){ ].forEach(action=>exports[action]=callPromise(action)); return exports; -})(); \ No newline at end of file +})();