alpha0010 / react-native-file-access

Filesystem access for React Native
MIT License
298 stars 18 forks source link

Add support for reading files from resource id returned by require() #67

Closed meypod closed 1 year ago

meypod commented 1 year ago

It would be nice if we could read a file from the id returned by require() this would simplify loading assets such as a text file

the only problem is, in development mode, the resource resolves to a web url that points to development server

it would be nice to support the dev mode as well

meypod commented 1 year ago

well I have written a helper function myself, I haven't test the production build but I guess it should work on that as well

async function loadFile(resourceId: number) {
  const uri = Image.resolveAssetSource(resourceId).uri;

  let resourcePath: string | undefined = undefined;
  if (uri.startsWith('http')) {
    resourcePath = Dirs.CacheDir + '/resource-' + resourceId;
    await FileSystem.fetch(uri, {
      path: resourcePath,
    });
  }

  return FileSystem.readFile(resourcePath ? resourcePath : uri);
}
meypod commented 1 year ago

Nope, it returns a string in production build sadly I think I have to copy the asset first then load it