Geen omschrijving

fs.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Copyright 2016 wkh237@github. All rights reserved.
  2. // Use of this source code is governed by a MIT-style license that can be
  3. // found in the LICENSE file.
  4. // @flow
  5. import {
  6. NativeModules,
  7. DeviceEventEmitter,
  8. Platform,
  9. NativeAppEventEmitter,
  10. } from 'react-native'
  11. import RNFetchBlobSession from './class/RNFetchBlobSession'
  12. import RNFetchBlobWriteStream from './class/RNFetchBlobWriteStream'
  13. import RNFetchBlobReadStream from './class/RNFetchBlobReadStream'
  14. import RNFetchBlobFile from './class/RNFetchBlobFile'
  15. import type {
  16. RNFetchBlobNative,
  17. RNFetchBlobConfig,
  18. RNFetchBlobStream
  19. } from './types'
  20. const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
  21. const emitter = DeviceEventEmitter
  22. const dirs = {
  23. DocumentDir : RNFetchBlob.DocumentDir,
  24. CacheDir : RNFetchBlob.CacheDir,
  25. PictureDir : RNFetchBlob.PictureDir,
  26. MusicDir : RNFetchBlob.MusicDir,
  27. MovieDir : RNFetchBlob.MovieDir,
  28. DownloadDir : RNFetchBlob.DownloadDir,
  29. DCIMDir : RNFetchBlob.DCIMDir
  30. }
  31. /**
  32. * Get a file cache session
  33. * @param {string} name Stream ID
  34. * @return {RNFetchBlobSession}
  35. */
  36. function session(name:string):RNFetchBlobSession {
  37. let s = RNFetchBlobSession.getSession(name)
  38. if(s)
  39. return new RNFetchBlobSession(name)
  40. else {
  41. RNFetchBlobSession.setSession(name, [])
  42. return new RNFetchBlobSession(name, [])
  43. }
  44. }
  45. function asset(path:string):string {
  46. if(Platform.OS === 'ios') {
  47. // path from camera roll
  48. if(/^assets-library\:\/\//.test(path))
  49. return path
  50. }
  51. return 'bundle-assets://' + path
  52. }
  53. function createFile(path:string, data:string, encoding: 'base64' | 'ascii' | 'utf8'):Promise {
  54. encoding = encoding || 'utf8'
  55. return new Promise((resolve, reject) => {
  56. let handler = (err) => {
  57. if(err)
  58. reject(err)
  59. else
  60. resolve()
  61. }
  62. if(encoding.toLowerCase() === 'ascii') {
  63. if(Array.isArray(data))
  64. RNFetchBlob.createFileASCII(path, data, handler)
  65. else
  66. reject('`data` of ASCII file must be an array contains numbers')
  67. }
  68. else {
  69. RNFetchBlob.createFile(path, data, encoding, handler)
  70. }
  71. })
  72. }
  73. /**
  74. * Create write stream to a file.
  75. * @param {string} path Target path of file stream.
  76. * @param {string} encoding Encoding of input data.
  77. * @param {bool} append A flag represent if data append to existing ones.
  78. * @return {Promise<WriteStream>} A promise resolves a `WriteStream` object.
  79. */
  80. function writeStream(
  81. path : string,
  82. encoding : 'utf8' | 'ascii' | 'base64',
  83. append? : ?bool,
  84. ):Promise<RNFetchBlobWriteStream> {
  85. if(!path)
  86. throw Error('RNFetchBlob could not open file stream with empty `path`')
  87. encoding = encoding || 'utf8'
  88. append = append || false
  89. return new Promise((resolve, reject) => {
  90. RNFetchBlob.writeStream(path, encoding || 'base64', append || false, (err, streamId:string) => {
  91. if(err)
  92. reject(err)
  93. else
  94. resolve(new RNFetchBlobWriteStream(streamId, encoding))
  95. })
  96. })
  97. }
  98. /**
  99. * Create file stream from file at `path`.
  100. * @param {string} path The file path.
  101. * @param {string} encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
  102. * @param {boolean} bufferSize Size of stream buffer.
  103. * @return {RNFetchBlobStream} RNFetchBlobStream stream instance.
  104. */
  105. function readStream(
  106. path : string,
  107. encoding : 'utf8' | 'ascii' | 'base64',
  108. bufferSize? : ?number
  109. ):Promise<RNFetchBlobReadStream> {
  110. return Promise.resolve(new RNFetchBlobReadStream(path, encoding, bufferSize))
  111. }
  112. /**
  113. * Create a directory.
  114. * @param {string} path Path of directory to be created
  115. * @return {Promise}
  116. */
  117. function mkdir(path:string):Promise {
  118. return new Promise((resolve, reject) => {
  119. RNFetchBlob.mkdir(path, (err, res) => {
  120. if(err)
  121. reject(err)
  122. else
  123. resolve()
  124. })
  125. })
  126. }
  127. /**
  128. * Wrapper method of readStream.
  129. * @param {string} path Path of the file.
  130. * @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
  131. * @return {Promise<Array<number> | string>}
  132. */
  133. function readFile(path:string, encoding:string, bufferSize:?number):Promise<any> {
  134. if(typeof path !== 'string')
  135. return Promise.reject('Invalid argument "path" ')
  136. return RNFetchBlob.readFile(path, encoding)
  137. }
  138. /**
  139. * Write data to file.
  140. * @param {string} path Path of the file.
  141. * @param {string | number[]} data Data to write to the file.
  142. * @param {string} encoding Encoding of data (Optional).
  143. * @return {Promise}
  144. */
  145. function writeFile(path:string, data:string | Array<number>, encoding:?string):Promise {
  146. encoding = encoding || 'utf8'
  147. if(typeof path !== 'string')
  148. return Promise.reject('Invalid argument "path" ')
  149. if(encoding.toLocaleLowerCase() === 'ascii') {
  150. if(!Array.isArray(data))
  151. Promise.reject(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`)
  152. else
  153. return RNFetchBlob.writeFileArray(path, data, false);
  154. } else {
  155. if(typeof data !== 'string')
  156. Promise.reject(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`)
  157. else
  158. return RNFetchBlob.writeFile(path, encoding, data, false);
  159. }
  160. }
  161. function appendFile(path:string, data:string | Array<number>, encoding:?string):Promise {
  162. encoding = encoding || 'utf8'
  163. if(typeof path !== 'string')
  164. return Promise.reject('Invalid argument "path" ')
  165. if(encoding.toLocaleLowerCase() === 'ascii') {
  166. if(!Array.isArray(data))
  167. Promise.reject(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`)
  168. else
  169. return RNFetchBlob.writeFileArray(path, data, true);
  170. } else {
  171. if(typeof data !== 'string')
  172. Promise.reject(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`)
  173. else
  174. return RNFetchBlob.writeFile(path, encoding, data, true);
  175. }
  176. }
  177. /**
  178. * Show statistic data of a path.
  179. * @param {string} path Target path
  180. * @return {RNFetchBlobFile}
  181. */
  182. function stat(path:string):Promise<RNFetchBlobFile> {
  183. return new Promise((resolve, reject) => {
  184. RNFetchBlob.stat(path, (err, stat) => {
  185. if(err)
  186. reject(err)
  187. else
  188. resolve(stat)
  189. })
  190. })
  191. }
  192. /**
  193. * Android only method, request media scanner to scan the file.
  194. * @param {Array<Object<string, string>>} Array contains Key value pairs with key `path` and `mime`.
  195. * @return {Promise}
  196. */
  197. function scanFile(pairs:any):Promise {
  198. return new Promise((resolve, reject) => {
  199. RNFetchBlob.scanFile(pairs, (err) => {
  200. if(err)
  201. reject(err)
  202. else
  203. resolve()
  204. })
  205. })
  206. }
  207. function cp(path:string, dest:string):Promise<boolean> {
  208. return new Promise((resolve, reject) => {
  209. RNFetchBlob.cp(path, dest, (err, res) => {
  210. if(err)
  211. reject(err)
  212. else
  213. resolve(res)
  214. })
  215. })
  216. }
  217. function mv(path:string, dest:string):Promise<boolean> {
  218. return new Promise((resolve, reject) => {
  219. RNFetchBlob.mv(path, dest, (err, res) => {
  220. if(err)
  221. reject(err)
  222. else
  223. resolve(res)
  224. })
  225. })
  226. }
  227. function lstat(path:string):Promise<Array<RNFetchBlobFile>> {
  228. return new Promise((resolve, reject) => {
  229. RNFetchBlob.lstat(path, (err, stat) => {
  230. if(err)
  231. reject(err)
  232. else
  233. resolve(stat)
  234. })
  235. })
  236. }
  237. function ls(path:string):Promise<Array<String>> {
  238. return new Promise((resolve, reject) => {
  239. RNFetchBlob.ls(path, (err, res) => {
  240. if(err)
  241. reject(err)
  242. else
  243. resolve(res)
  244. })
  245. })
  246. }
  247. /**
  248. * Remove file at path.
  249. * @param {string} path:string Path of target file.
  250. * @return {Promise}
  251. */
  252. function unlink(path:string):Promise {
  253. return new Promise((resolve, reject) => {
  254. RNFetchBlob.unlink(path, (err) => {
  255. if(err)
  256. reject(err)
  257. else
  258. resolve()
  259. })
  260. })
  261. }
  262. /**
  263. * Check if file exists and if it is a folder.
  264. * @param {string} path Path to check
  265. * @return {Promise<bool, bool>}
  266. */
  267. function exists(path:string):Promise<bool, bool> {
  268. return new Promise((resolve, reject) => {
  269. try {
  270. RNFetchBlob.exists(path, (exist) => {
  271. resolve(exist)
  272. })
  273. } catch(err) {
  274. reject(err)
  275. }
  276. })
  277. }
  278. function isDir(path:string):Promise<bool, bool> {
  279. return new Promise((resolve, reject) => {
  280. try {
  281. RNFetchBlob.exists(path, (exist, isDir) => {
  282. resolve(isDir)
  283. })
  284. } catch(err) {
  285. reject(err)
  286. }
  287. })
  288. }
  289. export default {
  290. RNFetchBlobSession,
  291. unlink,
  292. mkdir,
  293. session,
  294. ls,
  295. readStream,
  296. mv,
  297. cp,
  298. writeStream,
  299. writeFile,
  300. appendFile,
  301. readFile,
  302. exists,
  303. createFile,
  304. isDir,
  305. stat,
  306. lstat,
  307. scanFile,
  308. dirs,
  309. asset
  310. }