diff --git a/examples/.npmignore b/examples/.npmignore new file mode 100644 index 000000000..326b259a8 --- /dev/null +++ b/examples/.npmignore @@ -0,0 +1,2 @@ +# Make sure we don't publish examples +RNFetchBlobWin/ diff --git a/examples/RNFetchBlobWin/.buckconfig b/examples/RNFetchBlobWin/.buckconfig new file mode 100644 index 000000000..934256cb2 --- /dev/null +++ b/examples/RNFetchBlobWin/.buckconfig @@ -0,0 +1,6 @@ + +[android] + target = Google Inc.:Google APIs:23 + +[maven_repositories] + central = https://repo1.maven.org/maven2 diff --git a/examples/RNFetchBlobWin/.eslintrc.js b/examples/RNFetchBlobWin/.eslintrc.js new file mode 100644 index 000000000..40c6dcd05 --- /dev/null +++ b/examples/RNFetchBlobWin/.eslintrc.js @@ -0,0 +1,4 @@ +module.exports = { + root: true, + extends: '@react-native-community', +}; diff --git a/examples/RNFetchBlobWin/.flowconfig b/examples/RNFetchBlobWin/.flowconfig new file mode 100644 index 000000000..b274ad1d6 --- /dev/null +++ b/examples/RNFetchBlobWin/.flowconfig @@ -0,0 +1,73 @@ +[ignore] +; We fork some components by platform +.*/*[.]android.js + +; Ignore "BUCK" generated dirs +/\.buckd/ + +; Ignore polyfills +node_modules/react-native/Libraries/polyfills/.* + +; These should not be required directly +; require from fbjs/lib instead: require('fbjs/lib/warning') +node_modules/warning/.* + +; Flow doesn't support platforms +.*/Libraries/Utilities/LoadingView.js + +[untyped] +.*/node_modules/@react-native-community/cli/.*/.* + +[include] + +[libs] +node_modules/react-native/interface.js +node_modules/react-native/flow/ + +[options] +emoji=true + +esproposal.optional_chaining=enable +esproposal.nullish_coalescing=enable + +module.file_ext=.js +module.file_ext=.json +module.file_ext=.ios.js + +munge_underscores=true + +module.name_mapper='^react-native/\(.*\)$' -> '/node_modules/react-native/\1' +module.name_mapper='^@?[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> '/node_modules/react-native/Libraries/Image/RelativeImageStub' + +suppress_type=$FlowIssue +suppress_type=$FlowFixMe +suppress_type=$FlowFixMeProps +suppress_type=$FlowFixMeState + +suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\) +suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\)?:? #[0-9]+ +suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError + +[lints] +sketchy-null-number=warn +sketchy-null-mixed=warn +sketchy-number=warn +untyped-type-import=warn +nonstrict-import=warn +deprecated-type=warn +unsafe-getters-setters=warn +unnecessary-invariant=warn +signature-verification-failure=warn +deprecated-utility=error + +[strict] +deprecated-type +nonstrict-import +sketchy-null +unclear-type +unsafe-getters-setters +untyped-import +untyped-type-import + +[version] +^0.122.0 diff --git a/examples/RNFetchBlobWin/.gitattributes b/examples/RNFetchBlobWin/.gitattributes new file mode 100644 index 000000000..d42ff1835 --- /dev/null +++ b/examples/RNFetchBlobWin/.gitattributes @@ -0,0 +1 @@ +*.pbxproj -text diff --git a/examples/RNFetchBlobWin/.gitignore b/examples/RNFetchBlobWin/.gitignore new file mode 100644 index 000000000..ad572e632 --- /dev/null +++ b/examples/RNFetchBlobWin/.gitignore @@ -0,0 +1,59 @@ +# OSX +# +.DS_Store + +# Xcode +# +build/ +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +xcuserdata +*.xccheckout +*.moved-aside +DerivedData +*.hmap +*.ipa +*.xcuserstate + +# Android/IntelliJ +# +build/ +.idea +.gradle +local.properties +*.iml + +# node.js +# +node_modules/ +npm-debug.log +yarn-error.log + +# BUCK +buck-out/ +\.buckd/ +*.keystore +!debug.keystore + +# fastlane +# +# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the +# screenshots whenever they are needed. +# For more information about the recommended setup visit: +# https://docs.fastlane.tools/best-practices/source-control/ + +*/fastlane/report.xml +*/fastlane/Preview.html +*/fastlane/screenshots + +# Bundle artifact +*.jsbundle + +# CocoaPods +/ios/Pods/ diff --git a/examples/RNFetchBlobWin/.prettierrc.js b/examples/RNFetchBlobWin/.prettierrc.js new file mode 100644 index 000000000..5c4de1a4f --- /dev/null +++ b/examples/RNFetchBlobWin/.prettierrc.js @@ -0,0 +1,6 @@ +module.exports = { + bracketSpacing: false, + jsxBracketSameLine: true, + singleQuote: true, + trailingComma: 'all', +}; diff --git a/examples/RNFetchBlobWin/.watchmanconfig b/examples/RNFetchBlobWin/.watchmanconfig new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/examples/RNFetchBlobWin/.watchmanconfig @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/examples/RNFetchBlobWin/App.js b/examples/RNFetchBlobWin/App.js new file mode 100644 index 000000000..a0b9468bb --- /dev/null +++ b/examples/RNFetchBlobWin/App.js @@ -0,0 +1,957 @@ +/** + * Sample React Native App + * https://github.com/facebook/react-native + * + * @format + * @flow strict-local + */ + +import React, { useState } from 'react'; +import { + SafeAreaView, + StyleSheet, + ScrollView, + View, + Text, + StatusBar, + TextInput, + Button, + Alert, + Picker, +} from 'react-native'; + +import { + Colors, +} from 'react-native/Libraries/NewAppScreen'; + +import RNFetchBlob from 'rn-fetch-blob'; + +const App: () => React$Node = () => { + +// Variables ****************************************************************** + const [existsParam, setExistsParam] = useState(''); + const [lsParam, setLSParam] = useState(''); + + const [cpSourceParam, setCPSourceParam] = useState(''); + const [cpDestParam, setCPDestParam] = useState(''); + + const [unlinkParam, setUnlinkParam] = useState(''); + + const [statParam, setStatParam] = useState(''); + + const [mkdirParam, setMkdirParam] = useState(''); + const [mkdirURIParam, setMkdirURIParam] = useState(''); + + const [readParam, setReadParam] = useState(''); + + const [hashPathParam, setHashPathParam] = useState(''); + const [hashAlgValue, setHashAlgValue] = useState('md5'); + + const [writeParam, setWriteParam] = useState(''); + const [writeURIParam, setWriteURIParam] = useState(''); + const [writeEncodeParam, setWriteEncodeParam] = useState('utf8'); + + const [writeStreamParam, setWriteStreamParam] = useState(''); + const [writeEncodeStreamParam, setWriteStreamEncodeParam] = useState('utf8'); + + const [readStreamParam, setReadStreamParam] = useState(''); + const [readEncodeStreamParam, setReadStreamEncodeParam] = useState('utf8'); + +// Methods ******************************************************************** + // exists() + const existsCall = () => { + RNFetchBlob.fs.exists(RNFetchBlob.fs.dirs.DocumentDir + '/' + existsParam) + .then((result) => { + Alert.alert('Exists: ' + result) + }) + .catch((err) => { + Alert.alert(err.message); + }) + } + + const isDirCall = () => { + RNFetchBlob.fs.exists(RNFetchBlob.fs.dirs.DocumentDir + '/' + existsParam) + .then((result) => { + Alert.alert('isDir: ' + result); + }) + .catch((err) => { + Alert.alert(err.message); + }) + } + + // df() + const dfCall = () => { + RNFetchBlob.fs.df() + .then((result) => { + Alert.alert('Free space: ' + result.free + ' bytes\nTotal space: ' + result.total + ' bytes'); + }) + .catch((err) => { + Alert.alert(err.message); + }) + } + + // ls() + const lsCall = () => { + RNFetchBlob.fs.ls(RNFetchBlob.fs.dirs.DocumentDir + '/' + lsParam) + .then((files) => { + Alert.alert('Method finished: check debug console for results'); + console.log(files); + }) + } + + // cp() + const cpCall = () => { + RNFetchBlob.fs.cp(RNFetchBlob.fs.dirs.DocumentDir + '/' + cpSourceParam, + RNFetchBlob.fs.dirs.DocumentDir + '/' + cpDestParam) + .then( + Alert.alert('File successfully copied') + ) + .catch((err) => { + Alert.alert(err.message); + }); + } + + // mv() + const mvCall = () => { + RNFetchBlob.fs.mv(RNFetchBlob.fs.dirs.DocumentDir + '/' + cpSourceParam, + RNFetchBlob.fs.dirs.DocumentDir + '/' + cpDestParam) + .then( + Alert.alert('File successfully moved') + ) + .catch((err) => { + Alert.alert(err.message); + }); + } + + // unlink() + const unlinkCall = () => { + RNFetchBlob.fs.unlink(RNFetchBlob.fs.dirs.DocumentDir + '/' + unlinkParam) + .then( + Alert.alert('file/directory successfully unlinked') + ) + .catch((err) => { + Alert.alert(err.message); + }) + } + + // stat(), lstat() + const statCall = () => { + RNFetchBlob.fs.stat(RNFetchBlob.fs.dirs.DocumentDir + '/' + statParam) + .then((stats) => { + console.log(stats); + Alert.alert("stat() result (others logged in console)", + "filename: " + stats.filename + + "\nlastModified: " + stats.lastModified + + "\npath: " + stats.path + + "\nsize: " + stats.size + + "\ntype: " + stats.type) + }) + .catch((err) => { + Alert.alert(err.message); + }) + } + + const lstatCall = () => { + RNFetchBlob.fs.lstat(RNFetchBlob.fs.dirs.DocumentDir + '/' + statParam) + .then((stats) => { + console.log(stats); + Alert.alert("lstat() result (others logged in console)", "filename: " + stats[0].filename + + "\nlastModified: " + stats[0].lastModified + + "\npath: " + stats[0].path + + "\nsize: " + stats[0].size + + "\ntype: " + stats[0].type); + }) + .catch((err) => { + Alert.alert(err.message); + }) + } + + // mkdir() + const mkdirCall = () => { + if(mkdirParam.length > 0) + { + RNFetchBlob.fs.mkdir(RNFetchBlob.fs.dirs.DocumentDir + '/' + mkdirParam) + .then(() => { + Alert.alert('successfully created file:', RNFetchBlob.fs.dirs.DocumentDir + '/' + mkdirParam); + }) + .catch((err) => { + Alert.alert(err.message); + }) + } + else { + Alert.alert('Cannot make file with no name provided') + } + } + + // createFile() + const createFileUTF8Call = () => { + RNFetchBlob.fs.createFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + mkdirParam, 'foo', 'utf8'); + } + + const createFileASCIICall = () => { + RNFetchBlob.fs.createFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + mkdirParam, [102, 111, 111], 'ascii'); + } + + const createFileBase64Call = () => { + RNFetchBlob.fs.createFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + mkdirParam, 'Zm9v', 'base64'); + } + + const createFileURICall = () => { + RNFetchBlob.fs.createFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + mkdirParam, RNFetchBlob.fs.dirs.DocumentDir + '/' + mkdirURIParam, 'uri'); + } + + // readFile() + const readFileUTF8Call = () => { + RNFetchBlob.fs.readFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + readParam, 'utf8') + .then((data) => { + Alert.alert('UTF8 result of ' + readParam, data); + }); + } + + const readFileASCIICall = () => { + RNFetchBlob.fs.readFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + readParam, 'ascii') + .then((data) => { + Alert.alert('UTF8 result of ' + readParam, data); + }); + } + + const readFileBase64Call = () => { + RNFetchBlob.fs.readFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + readParam, 'base64') + .then((data) => { + Alert.alert('UTF8 result of ' + readParam, data); + }) + } + + // hash() + const hashCall = () => { + RNFetchBlob.fs.hash(RNFetchBlob.fs.dirs.DocumentDir + '/' + hashPathParam, hashAlgValue) + .then((hash) => { + Alert.alert(hashAlgValue, hash); + }) + .catch((err) => { + console.log(hashAlgValue + ': ' + err); + }); + } + + + // writeFile() + const writeFileCall = () => { + if(writeParam.length > 0) { + if(writeEncodeParam === 'uri') { + if(writeURIParam.length > 0) { + RNFetchBlob.fs.writeFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeParam, + RNFetchBlob.fs.dirs.DocumentDir + '/' + writeURIParam, + writeEncodeParam); + } + else { + Alert.alert('uri path undefined'); + } + } + else if(writeEncodeParam === 'ascii') { + RNFetchBlob.fs.writeFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeParam, [102,111,111], writeEncodeParam); + } + else { + RNFetchBlob.fs.writeFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeParam, 'foo', writeEncodeParam); + } + } + } + + // appendFile() + const appendFileCall = () => { + if(writeParam.length > 0) { + if(writeEncodeParam === 'uri') { + if(writeURIParam.length > 0) { + RNFetchBlob.fs.appendFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeParam, + RNFetchBlob.fs.dirs.DocumentDir + '/' + writeURIParam, + writeEncodeParam); + } + else { + Alert.alert('uri path undefined'); + } + } + else if(writeEncodeParam === 'ascii') { + RNFetchBlob.fs.appendFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeParam, [102,111,111], writeEncodeParam); + } + else { + RNFetchBlob.fs.appendFile(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeParam, 'foo', writeEncodeParam); + } + } + } + + const writeStreamCall = () => { + if(writeStreamParam.length > 0) { + if(writeEncodeStreamParam === 'base64') { + RNFetchBlob.fs.writeStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false) + .then((stream) => { + stream.write('Zm9vIChXcml0ZSBCYXNlNjQpMQ=='); + stream.write('Zm9vIChXcml0ZSBCYXNlNjQpMg=='); + return stream.close(); + }); + } + else if(writeEncodeStreamParam=== 'ascii') { + RNFetchBlob.fs.writeStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false) + .then((stream) => { + stream.write([102, 111, 111, 32, 40, 87, 114, 105, 116]); + stream.write([ 101, 32, 97, 115, 99, 105, 105, 41]); + return stream.close(); + }); + } + else { + RNFetchBlob.fs.writeStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, false) + .then((stream) => { + stream.write('foo (Write utf8)1'); + stream.write('foo (Write utf8)2'); + return stream.close(); + }); + } + } + } + + const appendStreamCall = () => { + if(writeStreamParam.length > 0) { + if(writeEncodeStreamParam === 'base64') { + RNFetchBlob.fs.writeStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true) + .then((stream) => { + stream.write('Zm9vIChBcHBlbmQgQmFzZTY0KTE='); + stream.write('Zm9vIChBcHBlbmQgQmFzZTY0KTI='); + return stream.close(); + }); + } + else if(writeEncodeStreamParam=== 'ascii') { + RNFetchBlob.fs.writeStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true) + .then((stream) => { + stream.write([102, 111, 111, 32, 40]); + stream.write([65, 112, 112, 101, 110, 100, 32, 65, 83, 67, 73, 73, 41]); + return stream.close(); + }); + } + else { + RNFetchBlob.fs.writeStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + writeStreamParam, writeEncodeStreamParam, true) + .then((stream) => { + stream.write('foo (Append utf8)1'); + stream.write('foo (Append utf8)2'); + return stream.close(); + }); + } + } + } + + // readStream + const readStreamCall = () => { + RNFetchBlob.fs.readStream(RNFetchBlob.fs.dirs.DocumentDir + '/' + readStreamParam, readEncodeStreamParam, 4000, 200) + .then((stream) => { + let data = ''; + stream.open(); + stream.onData((chunk) => { + data += chunk; + }) + stream.onEnd(() => { + console.log('data: ' + data); + }) + }); + } + + // fetchCall + const fetchCall = () => { + RNFetchBlob.config({ + // add this option that makes response data to be stored as a file, + // this is much more performant. + fileCache : true, + }) + .fetch( + 'GET', + 'https://upload.wikimedia.org/wikipedia/commons/c/c4/Change-5.png', + ) + .then((res) => { + // the temp file path + console.log('The file saved to ', res.path()) + }); + } + + // uploadFileFromStorage + const uploadFromStorageCall = () => { + RNFetchBlob.fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { + Authorization : "Bearer access-token...", + 'Dropbox-API-Arg': JSON.stringify({ + path : '/img-from-react-native.png', + mode : 'add', + autorename : true, + mute : false + }), + 'Content-Type' : 'application/octet-stream', + // here's the body you're going to send, should be a BASE64 encoded string + // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one). + // The data will be converted to "byte array"(say, blob) before request sent. + }, RNFetchBlob.wrap(RNFetchBlob.fs.dirs.DocumentDir + '\\ImageToUpload.jpg')) + .then((res) => { + console.log(res.text()); + }) + .catch((err) => { + // error handling .. + }) + } + + // uploadTextFromStorage + const uploadTextFromCall = () => { + RNFetchBlob.fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { + Authorization : "Bearer access-token...", + 'Dropbox-API-Arg': JSON.stringify({ + path : '/img-from-react-native.png', + mode : 'add', + autorename : true, + mute : false + }), + 'Content-Type' : 'application/octet-stream', + // here's the body you're going to send, should be a BASE64 encoded string + // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one). + // The data will be converted to "byte array"(say, blob) before request sent. + }, "Waka Flacka Flame goes very well with Thomas the Tank Engine.") + .then((res) => { + console.log(res.text()); + }) + .catch((err) => { + // error handling .. + }) + } + + // MultipartFileAndData + const MultipartFileAndData = () => { + RNFetchBlob.fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { + Authorization : "Bearer access-token...", + 'Dropbox-API-Arg': JSON.stringify({ + path : '/img-from-react-native.png', + mode : 'add', + autorename : true, + mute : false + }), + 'Content-Type' : 'application/octet-stream', + // here's the body you're going to send, should be a BASE64 encoded string + // (you can use "base64"(refer to the library 'mathiasbynens/base64') APIs to make one). + // The data will be converted to "byte array"(say, blob) before request sent. + }, "Waka Flacka Flame goes very well with Thomas the Tank Engine.") + .uploadProgress((received, total) => { + console.log('upload progress', received / total) + }) + .progress((received, total) => { + console.log('download progress', received / total) + }) + .then((res) => { + console.log(res.text()); + }) + .catch((err) => { + // error handling .. + }) + } + + // + const MakeRequestWithProgress = () => { + RNFetchBlob.config({ + // add this option that makes response data to be stored as a file, + // this is much more performant. + fileCache : true, + }).fetch('POST', 'https://enb954aqyumba.x.pipedream.net/', { + Authorization : "Bearer access-token", + otherHeader : "foo", + 'Content-Type' : 'multipart/form-data', + }, [ + // element with property `filename` will be transformed into `file` in form data + { name : 'avatar', filename : 'avatar.png', data: "Kentucky Fried Seth"}, + // custom content type + { name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: "whaddup my pickles"}, + // part file from storage + { name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(RNFetchBlob.fs.dirs.DocumentDir + '\\ImageToUpload.jpg')}, + // elements without property `filename` will be sent as plain text + { name : 'name', data : 'user'}, + { name : 'info', data : JSON.stringify({ + mail : 'example@example.com', + tel : '12345678' + })}, + ]).uploadProgress({ interval : 250 }, (written, total) => { + console.log('uploaded', written / total) + }).progress({ count : 10, interval: -1 }, (received, total) => { + console.log('progress', received / total) + }).then((res) => { + console.log(res.text()); + }).catch((err) => { + console.log(err.text()); + }) + } + +// App ************************************************************************ + return ( + <> + + + + {global.HermesInternal == null ? null : ( + + Engine: Hermes + + )} + + {"React Native Fetch Blob Windows Demo App"} + + + + + + {"DocumentDir: " + RNFetchBlob.fs.dirs.DocumentDir + '\n'} + {"CacheDir: " + RNFetchBlob.fs.dirs.CacheDir + '\n'} + {"PictureDir: " + RNFetchBlob.fs.dirs.PictureDir + '\n'} + {"MusicDir: " + RNFetchBlob.fs.dirs.MusicDir + '\n'} + {"DownloadDir: " + RNFetchBlob.fs.dirs.DownloadDir + '\n'} + {"DCIMDir: " + RNFetchBlob.fs.dirs.DCIMDir + '\n'} + {"SDCardDir: " + RNFetchBlob.fs.dirs.SDCardDir + '\n'} + {"SDCardApplicationDir: " + RNFetchBlob.fs.dirs.SDCardApplicationDir + '\n'} + {"MainBundleDir: " + RNFetchBlob.fs.dirs.MainBundleDir + '\n'} + {"LibraryDir: " + RNFetchBlob.fs.dirs.LibraryDir + '\n'} + + + + + + + + + {"exists - exists(), isDir()"} + + setExistsParam(existsParam)} + placeholderTextColor = "#9a73ef" + autoCapitalize = "none" + /> +