Skip to content

Export getCachedImageFilePath #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2017

Conversation

mattvot
Copy link
Contributor

@mattvot mattvot commented May 23, 2017

Exposing these methods allows us to take a set of URLs, generate the appropriate filepaths for each URL and compare against a list of cached files for a synchronisation function.

getCachedImagePath stats each file individually. We hope to implement a function in our code to list all the files in the cache directory, thus avoiding the erroneous stats.

For example something like this:

import RNFetchBlob from 'react-native-fetch-blob';
import CachedImage from 'react-native-cached-image';
import R from 'ramda';

const {fs} = RNFetchBlob;
const ImageCacheProvider = CachedImage.ImageCacheProvider;

function discardFile(path: string) {
  return fs.unlink(path).catch(() => Promise.resolve());
}

function discardFiles(paths: string[]) {
  let chain = Promise.resolve();

  for (let i = 0, c = paths.length; i < c; i += 1) {
    chain = chain.then(() => discardFile(paths[i]));
  }

  return chain;
}

function sync(imageUrls: string[]) {
  let cachedImagesToRemove;
  let imagesToBeCached;

  return ImageCacheProvider.getCacheInfo()
    .then(info => info.files.map(f => f.path))
    .then(previouslyCachedImagePaths => {
      const previouslyCachedImages = previouslyCachedImagePaths.map(path => ({
        url: undefined,
        path,
      }));

      const currentImagesToBeCached = imageUrls.map(url => ({
        url,
        path: ImageCacheProvider.getCachedImageFilePath(url, {}),
      }));

      cachedImagesToRemove = R.differenceWith(
        (p, c) => p.path === c.path,
        previouslyCachedImages,
        currentImagesToBeCached
      );

      imagesToBeCached = R.differenceWith((c, p) => c.path === p.path, currentImagesToBeCached, previouslyCachedImages);
    })
    .then(() => discardFiles(cachedImagesToRemove.map(f => f.path)))
    .then(() => ImageCacheProvider.cacheMultipleImages(imagesToBeCached.map(f => f.url)));
}

export default sync;

@mattvot mattvot changed the title Export generateCacheKey & getCachedImageFilePath Export getCachedImageFilePath May 23, 2017
@kfiroo kfiroo merged commit c8f567f into kfiroo:master Jun 5, 2017
@kfiroo
Copy link
Owner

kfiroo commented Jun 5, 2017

@mattvot I see no harm in exporting this method.
But keep in mind, I would rather not expose the underlying cache layer to the user as it's an implementation detail.
When dealing with urls only it becomes a lot easier to reason about the data (either this specific url is cached or not, without having to translate it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants