npomfret / react-native-cloud-fs

82 stars 62 forks source link

react-native-cloud-fs

A react-native library for reading and writing files to iCloud Drive (iOS) and Google Drive (Android).

Getting started

Usage

import RNCloudFs from 'react-native-cloud-fs';

fileExists (options)

Returns a promise which when resolved returns a boolean value indicating if the specified path already exists.

const destinationPath = "foo-bar/docs/info.pdf";
const scope = 'visible';

RNCloudFs.fileExists({
  targetPath: destinationPath, 
  scope: scope
})
  .then((exists) => {
    console.log(exists ? "this file exists" : "this file does not exist");
  })
  .catch((err) => {
    console.warn("it failed", err);
  })

targetPath: a path

scope: determines if the user-visible documents (visible) or the app-visible documents (hidden) are searched for the specified path

copyToCloud (options)

Copies the content of a file (or uri) to the target file system. The files will appear in a either a user visible directory, or a dectory that only the app can see. The directory is named after destinationPath. The directory hierarchy for the destination path will be created if it doesn't already exist. If the target file already exists it a new filename is chosen and returned when the promise is resolved.

const sourceUri = {uri: 'https://foo.com/bar.pdf'};
const destinationPath = "foo-bar/docs/info.pdf";
const mimeType = null;
const scope = 'visible';
const update = false;

RNCloudFs.copyToCloud({
  sourcePath: sourceUri, 
  targetPath: destinationPath, 
  mimeType, 
  scope,
  update
})
  .then((path) => {
    console.log("it worked", path);
  })
  .catch((err) => {
    console.warn("it failed", err);
  })

sourceUri: object with any uri or an absolute file path and optional http headers, e.g:

targetPath: a relative path including a filename under which the file will be placed, e.g:

mimeType: a mime type to store the file with or null (android only) , e.g:

scope: a string to specify if the user can access the document (visible) or not (hidden)

update: a boolean to specify if we want to update an existing item instead of creating a new one

listFiles (options)

Lists files in a directory along with some file metadata. The scope determines if the file listing takes place in the app folder or the public user documents folder.

const path = "dirA/dirB";
const scope = 'hidden';

RNCloudFs.listFiles({targetPath: path, scope: scope})
  .then((res) => {
    console.log("it worked", res);
  })
  .catch((err) => {
    console.warn("it failed", err);
  })

targetPath: a path representing a folder to list files from

scope: a string to specify if the files are the user-visible documents (visible) or the app-visible documents (hidden)

readFileContent (options)

Reads a file content. The scope determines if the file listing takes place in the app folder or the public user documents folder.

const path = "dirA/dirB";
const scope = 'hidden';

RNCloudFs.readFileContent({targetPath: path, scope: scope})
  .then((res) => {
    console.log("it worked", res);
  })
  .catch((err) => {
    console.warn("it failed", err);
  })

targetPath: a path representing a file which content you want to read

scope: a string to specify if the files are the user-visible documents (visible) or the app-visible documents (hidden)