Installations
npm install react-native-file-access
Developer
alpha0010
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
22.5.1
NPM Version
10.8.2
Statistics
313 Stars
155 Commits
26 Forks
7 Watching
6 Branches
9 Contributors
Updated on 23 Nov 2024
Languages
TypeScript (27.29%)
Kotlin (23.17%)
Swift (17.22%)
Objective-C++ (10.1%)
Java (9.95%)
Objective-C (5.51%)
Ruby (3.98%)
JavaScript (2.63%)
C (0.15%)
Total Downloads
Cumulative downloads
Total Downloads
958,394
Last day
7.7%
2,893
Compared to previous day
Last week
5.7%
14,641
Compared to previous week
Last month
13.1%
65,510
Compared to previous month
Last year
107.5%
549,149
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
2
Dev Dependencies
19
react-native-file-access
Filesystem access for React Native. Supports saving network requests directly to the filesystem. Supports Android scoped storage, a requirement when targeting API 30 or higher.
Installation
1npm install react-native-file-access 2cd ios && pod install
Apple restricts usage of certain privacy sensitive API calls. If you do not use disk space measurements or file timestamps, define the following variable in your Podfile to exclude restricted API calls. More details.
1$RNFANoPrivacyAPI = true
If the app does not use autolinking, continue to the manual install instructions in the wiki.
Compatibility
React Native | react-native-file-access |
---|---|
<= 0.64 | 1.x.x |
0.65+, old arch | 2.x.x, 3.x.x |
0.71+, new arch | 3.x.x |
Usage
1import { Dirs, FileSystem } from 'react-native-file-access'; 2 3// ... 4 5const text = await FileSystem.readFile(Dirs.CacheDir + '/test.txt');
Directory constants.
Dirs.CacheDir
Dirs.DatabaseDir
(Android only)Dirs.DocumentDir
Dirs.LibraryDir
(iOS & MacOS only)Dirs.MainBundleDir
Dirs.SDCardDir
(Android only)- Prefer
FileSystem.cpExternal()
when possible.
- Prefer
Functions.
FileSystem.appendFile(path: string, data: string, encoding?: 'utf8' | 'base64'): Promise<void>
- Append content to a file.
- Default encoding of
data
is assumed utf8.
- Default encoding of
FileSystem.concatFiles(source: string, target: string): Promise<number>
- Append a file to another file. Returns number of bytes written.
FileSystem.cp(source: string, target: string, onProgress?: (bytesCopied: number, contentLength: number, done: boolean) => void): Promise<void>
- Copy a file.
FileSystem.cpAsset(asset: string, target: string, type?: 'asset' | 'resource'): Promise<void>
- Copy a bundled asset file.
- Default
type
isasset
. Prefer this when possible. resource
uses the Androidres/
folder, and inherits the associated naming restrictions.
- Default
FileSystem.cpExternal(source: string, targetName: string, dir: 'audio' | 'downloads' | 'images' | 'video'): Promise<void>
- Copy a file to an externally controlled location.
- On Android API level < 29, may require permission WRITE_EXTERNAL_STORAGE.
- On iOS, consider using
Dirs.DocumentDir
withUIFileSharingEnabled
andLSSupportsOpeningDocumentsInPlace
enabled.
FileSystem.df(): Promise<{ internal_free: number, internal_total: number, external_free?: number, external_total?: number }>
- Check device available space.
FileSystem.exists(path: string): Promise<boolean>
- Check if a path exists.
FilesSystem.fetch(
resource: string,
init: { body?: string, headers?: { [key: string]: string }, method?: string, network?: 'any' | 'unmetered', path?: string },
onProgress?: (bytesRead: number, contentLength: number, done: boolean) => void
): Promise<FetchResult>
type FetchResult = {
headers: { [key: string]: string };
ok: boolean;
redirected: boolean;
status: number;
statusText: string;
url: string;
}
- Save a network request to a file.
resource
- URL to fetch.init.path
- Optional filesystem location to save the response.init.network
- Optional restriction on network type. Specifyingunmetered
will reject the request if unmetered connections (most likely WiFi) are unavailable.onProgress
- Optional callback to listen to download progress. Events are rate limited, so do not rely ondone
becomingtrue
.contentLength
is only accurate if the server sends the correct headers.
FilesSystem.fetchManaged(
resource: string,
init: { body?: string, headers?: { [key: string]: string }, method?: string, network?: 'any' | 'unmetered', path?: string },
onProgress?: (bytesRead: number, contentLength: number, done: boolean) => void
): ManagedFetchResult
type ManagedFetchResult = {
cancel: () => Promise<void>;
result: Promise<FetchResult>;
}
- Save a network request to a file.
- Similar to
fetch()
, with the option to cancel before completion.
- Similar to
FilesSystem.getAppGroupDir(groupName: string): Promise<string>
- Get the directory for your app group (iOS & MacOS only).
- App groups are used on iOS/MacOS for storing content, which is shared between apps.
- This is e.g. useful for sharing data between your iOS/MacOS app and a widget or a watch app.
FilesSystem.hash(path: string, algorithm: 'MD5' | 'SHA-1' | 'SHA-224' | 'SHA-256' | 'SHA-384' | 'SHA-512'): Promise<string>
- Hash the file content.
FilesSystem.isDir(path: string): Promise<boolean>
- Check if a path is a directory.
FileSystem.ls(path: string): Promise<string[]>
- List files in a directory.
FileSystem.mkdir(path: string): Promise<string>
- Make a new directory.
- Returns path of created directory.
FileSystem.mv(source: string, target: string): Promise<void>
- Move a file.
FileSystem.readFile(path: string, encoding?: 'utf8' | 'base64'): Promise<string>
- Read the content of a file.
- Default encoding of returned string is utf8.
FileSystem.readFileChunk(path: string, offset: number, length: number, encoding?: 'utf8' | 'base64'): Promise<string>
- Read a chunk of the content of a file, starting from byte at
offset
, reading forlength
bytes.- Default encoding of returned string is utf8.
FileSystem.stat(path: string): Promise<FileStat>
type FileStat = {
filename: string;
lastModified: number;
path: string;
size: number;
type: 'directory' | 'file';
}
- Read file metadata.
FileSystem.statDir(path: string): Promise<FileStat[]>
- Read metadata of all files in a directory.
FileSystem.unlink(path: string): Promise<void>
- Delete a file.
FileSystem.unzip(source: string, target: string): Promise<void>
- Extract a zip archive.
FileSystem.writeFile(path: string, data: string, encoding?: 'utf8' | 'base64'): Promise<void>
- Write content to a file.
- Default encoding of
data
is assumed utf8.
- Default encoding of
Utility functions.
Util.basename(path: string, separator?: string): string
- Get the file/folder name from the end of the path.
- Default path
separator
is/
.
- Default path
Util.dirname(path: string, separator?: string): string
- Get the path containing the file/folder.
- Default path
separator
is/
.
- Default path
Util.extname(path: string, separator?: string): string
- Get the file extension.
- Default path
separator
is/
.
- Default path
Scoped storage.
For simple usage, use FileSystem.cpExternal()
to submit files to general
scoped storage categories.
Most functions in this library work with content://
Android resource uris.
To gain access to a resource uri, currently use a library such as
react-native-document-picker or
react-native-scoped-storage.
Eventually this library will incorporate file/folder selector functionality
(pull requests welcome).
Note:
- When generating a scoped storage resource uri, use the helper
AndroidScoped.appendPath(dir, 'data.txt')
, notdir + '/data.txt'
. - Android may change the name of created files/folders.
AndroidScoped.appendPath(basePath: string, segment: string): string
- Append a path segment to an Android scoped storage content uri.
Testing
For ease of testing, this library contains a mock implementation:
jest/react-native-file-access.ts.
To use, copy it into the __mocks__
folder, modifying if needed.
Alternatives
This library aims to be a modern implementation of filesystem api, using Kotlin/Swift and latest best practices. For a more established library, consider:
- expo-file-system
- Well supported, a good option if already using Expo.
- react-native-blob-util
- Often a dependency of other libraries.
- Forked from, and compatible with, the popular but deprecated rn-fetch-blob.
- react-native-fs
- Large feature set.
- Low maintenance, aging codebase.
For more greater control over network requests, consider react-native-blob-courier.
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
binaries present in source code
Details
- Warn: binary detected: example/android/gradle/wrapper/gradle-wrapper.jar:1
- Warn: binary detected: exampleMacos/android/gradle/wrapper/gradle-wrapper.jar:1
Reason
4 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 2/26 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/node.js.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/alpha0010/react-native-file-access/node.js.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/alpha0010/react-native-file-access/node.js.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/alpha0010/react-native-file-access/node.js.yml/master?enable=pin
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 6 are checked with a SAST tool
Reason
43 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-f5x2-xv93-4p23
- Warn: Project is vulnerable to: GHSA-gmpm-xp43-f7g6
- Warn: Project is vulnerable to: GHSA-pf27-929j-9pmm
- Warn: Project is vulnerable to: GHSA-327c-qx3v-h673
- Warn: Project is vulnerable to: GHSA-x4cf-6jr3-3qvp
- Warn: Project is vulnerable to: GHSA-mph8-6787-r8hw
- Warn: Project is vulnerable to: GHSA-7mhc-prgv-r3q4
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-4cpg-3vgw-4877
- Warn: Project is vulnerable to: GHSA-rxrc-rgv4-jpvx
- Warn: Project is vulnerable to: GHSA-7f53-fmmv-mfjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-gff7-g5r8-mg8m
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-5fg8-2547-mr8q
- Warn: Project is vulnerable to: GHSA-crh6-fp67-6883
- Warn: Project is vulnerable to: GHSA-6w63-h3fj-q4vw
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-3j8f-xvm3-ffx4
- Warn: Project is vulnerable to: GHSA-j9fq-vwqv-2fm2
- Warn: Project is vulnerable to: GHSA-pqw5-jmp5-px4v
- Warn: Project is vulnerable to: GHSA-cchq-frgv-rjh5
- Warn: Project is vulnerable to: GHSA-g644-9gfx-q4q4
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
3
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-native-file-access
expo-file-system
Provides access to the local file system on the device.
react-native-blob-util
A module provides upload, download, and files access API. Supports file stream read/write for process large files.
react-native
A framework for building native apps using React
react-native-fs
Native filesystem access for react-native