How to use the expo-file-system.cacheDirectory function in expo-file-system

To help you get started, we’ve selected a few expo-file-system examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github expo / expo / apps / native-component-list / src / screens / SharingScreen.tsx View on Github external
_shareLocalImage = async () => {
    const asset = Asset.fromModule(image);
    await asset.downloadAsync();
    const tmpFile = FileSystem.cacheDirectory + 'chapeau.png';

    try {
      // sharing only works with `file://` urls on Android so we need to copy it out of assets
      await FileSystem.copyAsync({ from: asset.localUri!, to: tmpFile });
      await Sharing.shareAsync(tmpFile, {
        dialogTitle: 'Is it a snake or a hat?',
      });
    } catch (e) {
      console.error(e);
    }
  }
github Nohac / redux-persist-expo-fs-storage / index.js View on Github external
/* @flow */
import * as FileSystem from 'expo-file-system';

export const DocumentDir = FileSystem.documentDirectory;
export const CacheDir = FileSystem.cacheDirectory;

const resolvePath = (...paths: Array) =>
  paths
    .join('/')
    .split('/')
    .filter(part => part && part !== '.')
    .join('/');

// Wrap function to support both Promise and callback
async function withCallback(
  callback?: ?(error: ?Error, result: R | void) => void,
  func: () => Promise,
): Promise {
  try {
    const result = await func();
    if (callback) {
github Nohac / redux-persist-expo-fs-storage / lib / index.js View on Github external
import * as FileSystem from 'expo-file-system';

export const DocumentDir = FileSystem.documentDirectory;
export const CacheDir = FileSystem.cacheDirectory;

const resolvePath = (...paths) => paths.join('/').split('/').filter(part => part && part !== '.').join('/');

// Wrap function to support both Promise and callback
async function withCallback(callback, func) {
  try {
    const result = await func();
    if (callback) {
      callback(null, result);
    }
    return result;
  } catch (err) {
    if (callback) {
      callback(err);
    } else {
      throw err;
github wcandillon / react-native-expo-image-cache / src / CacheManager.ts View on Github external
// @flow
import * as _ from "lodash";
import * as FileSystem from "expo-file-system";
import SHA1 from "crypto-js/sha1";

export interface DownloadOptions {
    md5?: boolean;
    headers?: { [name: string]: string };
}

const BASE_DIR = `${FileSystem.cacheDirectory}expo-image-cache/`;

export class CacheEntry {
    uri: string;

    options: DownloadOptions;

    path: string;

    constructor(uri: string, options: DownloadOptions) {
        this.uri = uri;
        this.options = options;
    }

    async getPath(): Promise {
        const { uri, options } = this;
        const { path, exists, tmpPath } = await getCacheEntry(uri);
github expo / expo / packages / expo-asset / build / Asset.js View on Github external
async _downloadAsyncUnmanagedEnv() {
        // Bail out if it was bundled with the app
        if (this.uri.startsWith('file://')) {
            this.localUri = this.uri;
            return;
        }
        const localUri = `${FileSystem.cacheDirectory}ExponentAsset-${this.hash}.${this.type}`;
        // We don't check the FileSystem for an existing version of the asset and we
        // also don't perform an integrity check!
        await FileSystem.downloadAsync(this.uri, localUri);
        this.localUri = localUri;
    }
    async downloadAsync() {
github expo / expo-asset-utils / src / fileInfoAsync.js View on Github external
async function fileInfoAsync(url: ?string, name: string): Promise {
  if (!url) {
    throw new Error('expo-asset-utils: fileInfoAsync(): cannot load from empty url!');
    return null;
  }
  name = name || filenameFromUri(url);

  if (Platform.OS === 'web') {
    return { uri: url, name, hash: null };
  }

  const localUri = FileSystem.cacheDirectory + name;

  if (isAssetLibraryUri(url)) {
    /// ios asset: we need to copy this over and then get the hash
    await FileSystem.copyAsync({
      from: url,
      to: localUri,
    });
    const hash = await getHashAsync(localUri);
    return { uri: localUri, name, hash };
  } else if (isLocalUri(url)) {
    /// local image: we just need the hash
    let file = await resolveLocalFileAsync({ uri: url, name });
    if (!file) {
      file = await resolveLocalFileAsync({ uri: localUri, name });
      if (!file) {
        throw new Error(
github bugsnag / bugsnag-js / packages / delivery-expo / queue.js View on Github external
const FileSystem = require('expo-file-system')

const MAX_ITEMS = 64
const PAYLOAD_PATH = `${FileSystem.cacheDirectory}bugsnag`
const filenameRe = /^bugsnag-.*\.json$/

/*
 * This class resembles FIFO queue in which to store undelivered payloads.
 */
module.exports = class UndeliveredPayloadQueue {
  constructor (resource, onerror = () => {}) {
    this._resource = resource
    this._path = `${PAYLOAD_PATH}/${this._resource}`
    this._onerror = onerror
    this._truncating = false
    this._initCall = null
  }

  /*
   * Calls _init(), ensuring it only does that task once returning
github expo / expo / packages / expo-asset / src / Asset.ts View on Github external
async _downloadAsyncManagedEnv(): Promise {
    const localUri = `${FileSystem.cacheDirectory}ExponentAsset-${this.hash}.${this.type}`;
    let { exists, md5 } = await FileSystem.getInfoAsync(localUri, {
      md5: true,
    });
    if (!exists || md5 !== this.hash) {
      ({ md5 } = await FileSystem.downloadAsync(this.uri, localUri, {
        md5: true,
      }));
      if (md5 !== this.hash) {
        throw new Error(
          `Downloaded file for asset '${this.name}.${this.type}' ` +
            `Located at ${this.uri} ` +
            `failed MD5 integrity check`
        );
      }
    }
github sysgears / apollo-universal-starter-kit / modules / chat / client-react / containers / withImage.jsx View on Github external
import { ReactNativeFile } from 'apollo-upload-client';
import * as mime from 'react-native-mime-types';
import url from 'url';
import PropTypes from 'prop-types';
import { View, Platform } from 'react-native';

import settings from '@gqlapp/config';

import ModalNotify from '../components/ModalNotify';

const { protocol, port, hostname } = url.parse(__API_URL__);
const serverUrl = `${protocol}//${
  hostname === 'localhost' ? url.parse(Constants.manifest.bundleUrl).hostname : hostname
}${port ? ':' + port : ''}`;

const imageDir = FileSystem.cacheDirectory + 'ImagePicker/';

export default Component => {
  return class MessageImage extends React.Component {
    static propTypes = {
      messages: PropTypes.object,
      t: PropTypes.func
    };

    state = {
      edges: [],
      endCursor: 0,
      allowImages: settings.chat.allowImages,
      notify: null
    };

    static getDerivedStateFromProps({ messages }, { allowImages, edges: stateEdges, endCursor }) {
github expo / expo / apps / native-component-list / src / screens / FileSystemScreen.tsx View on Github external
_copyAndReadAsset = async () => {
    const asset = Asset.fromModule(require('../../assets/index.html'));
    await asset.downloadAsync();
    const tmpFile = FileSystem.cacheDirectory + 'test.html';
    try {
      await FileSystem.copyAsync({ from: asset.localUri!, to: tmpFile });
      const result = await FileSystem.readAsStringAsync(tmpFile);
      Alert.alert('Result', result);
    } catch (e) {
      Alert.alert('Error', e.message);
    }
  }