Skip to content

Commit

Permalink
feat(android): support reading from res/
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
alpha0010 committed Apr 27, 2021
1 parent 506ac25 commit 025e39d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ const text = await FileSystem.readFile(Dirs.CacheDir + '/test.txt');
`FileSystem.cp(source: string, target: string): Promise<void>`
- Copy a file.

`FileSystem.cpAsset(asset: string, target: string): Promise<void>`
`FileSystem.cpAsset(asset: string, target: string, type?: 'asset' | 'resource'): Promise<void>`
- Copy a bundled asset file.
- Default `type` is `asset`. Prefer this when possible.
- `resource` uses the Android `res/` folder, and inherits the associated
naming restrictions.

`FileSystem.cpExternal(source: string, targetName: string, dir: 'audio' | 'downloads' | 'images' | 'video'): Promise<void>`
- Copy a file to an externally controlled location.
Expand Down
13 changes: 11 additions & 2 deletions android/src/main/java/com/alpha0010/fs/FileAccessModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ class FileAccessModule(reactContext: ReactApplicationContext) : ReactContextBase
}

@ReactMethod
fun cpAsset(asset: String, target: String, promise: Promise) {
fun cpAsset(asset: String, target: String, type: String, promise: Promise) {
try {
reactApplicationContext.assets.open(asset).use { assetStream ->
if (type == "resource") {
val id = reactApplicationContext.resources.getIdentifier(
asset,
null,
reactApplicationContext.packageName
)
reactApplicationContext.resources.openRawResource(id)
} else {
reactApplicationContext.assets.open(asset)
}.use { assetStream ->
parsePathToFile(target).outputStream().use { assetStream.copyTo(it) }
}
promise.resolve(null)
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/res/raw/android_resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bundled raw resource.
20 changes: 20 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ export function App() {
})
);

if (Platform.OS === 'android') {
FileSystem.cpAsset(
'raw/android_resource',
Dirs.CacheDir + '/android_resource.txt',
'resource'
)
.then(() =>
FileSystem.readFile(Dirs.CacheDir + '/android_resource.txt')
)
.then((res) =>
setInfo((prev) => {
prev.push({
key: 'readFile(CacheDir/android_resource.txt)',
value: JSON.stringify(res),
});
return prev.slice();
})
);
}

// Disk usage.
FileSystem.df().then((res) =>
setInfo((prev) => {
Expand Down
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NativeModules, Platform } from 'react-native';
import { FileAccessNative } from './native';
import type {
AssetType,
Encoding,
ExternalDir,
FetchResult,
Expand All @@ -10,6 +11,7 @@ import type {
} from './types';

export type {
AssetType,
Encoding,
ExternalDir,
FetchResult,
Expand Down Expand Up @@ -46,9 +48,17 @@ export const FileSystem = {

/**
* Copy a bundled asset file.
*/
cpAsset(asset: string, target: string) {
return FileAccessNative.cpAsset(asset, target);
*
* When using Android asset type 'resource', include the folder, but skip the
* file extension. For example use 'raw/foo', for the file 'res/raw/foo.txt'.
* When possible, prefer using the 'assets/' folder; files in 'res/' have
* naming restrictions imposed by Android.
* https://developer.android.com/guide/topics/resources/providing-resources.html#OriginalFiles
*/
cpAsset(asset: string, target: string, type: AssetType = 'asset') {
return Platform.OS === 'android'
? FileAccessNative.cpAsset(asset, target, type)
: FileAccessNative.cpAsset(asset, target);
},

/**
Expand Down
6 changes: 5 additions & 1 deletion src/native.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NativeModules } from 'react-native';
import type {
AssetType,
Encoding,
ExternalDir,
FetchResult,
Expand All @@ -12,7 +13,10 @@ type FileAccessType = {
appendFile(path: string, data: string, encoding: Encoding): Promise<void>;
concatFiles(source: string, target: string): Promise<number>;
cp(source: string, target: string): Promise<void>;
cpAsset(asset: string, target: string): Promise<void>;
/**
* `type` only used on Android.
*/
cpAsset(asset: string, target: string, type?: AssetType): Promise<void>;
cpExternal(
source: string,
targetName: string,
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* asset - Android `assets/` folder or iOS main bundle.
* resource - Android `res/` folder.
*/
export type AssetType = 'asset' | 'resource';

export type Encoding = 'utf8' | 'base64';

export type ExternalDir = 'audio' | 'downloads' | 'images' | 'video';
Expand Down

0 comments on commit 025e39d

Please sign in to comment.