diff --git a/README.md b/README.md index 6b909e19c..6c8203c93 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ and - [x] Preload images. - [x] GIF support. - [x] Border radius. +- [x] Get image path from cache. ## Usage @@ -237,6 +238,14 @@ Clear all images from memory cache. Clear all images from disk cache. +### `FastImage.getCachePath: (source) => void` + +Get image path from cache by `source` + +```js +FastImage.getCachePath({ uri: 'https://facebook.github.io/react/img/logo_og.png' }) +``` + ## Troubleshooting If you have any problems using this library try the steps in [troubleshooting](docs/troubleshooting.md) and see if they fix it. diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java b/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java index d9dbd9933..8c60b5881 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java @@ -107,6 +107,12 @@ public Headers getHeaders() { } public GlideUrl getGlideUrl() { + Uri uriVal = getUri(); + + if (Uri.EMPTY.equals(uriVal)) { + return null; + } + return new GlideUrl(getUri().toString(), getHeaders()); } } diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java b/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java index f9d6faad7..b8703c6a0 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java @@ -3,9 +3,14 @@ import android.app.Activity; import androidx.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.Glide; +import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.load.model.GlideUrl; +import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.target.Target; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; @@ -14,9 +19,12 @@ import com.facebook.react.bridge.ReadableMap; import com.facebook.react.views.imagehelper.ImageSource; +import java.io.File; + class FastImageViewModule extends ReactContextBaseJavaModule { private static final String REACT_CLASS = "FastImageView"; + private static final String ERROR_LOAD_FAILED = "ERROR_LOAD_FAILED"; FastImageViewModule(ReactApplicationContext reactContext) { super(reactContext); @@ -86,4 +94,47 @@ public void clearDiskCache(Promise promise) { Glide.get(activity.getApplicationContext()).clearDiskCache(); promise.resolve(null); } + + @ReactMethod + public void getCachePath(final ReadableMap source, final Promise promise) { + final Activity activity = getCurrentActivity(); + if (activity == null) { + promise.resolve(null); + return; + } + + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + + final FastImageSource imageSource = FastImageViewConverter.getImageSource(activity, source); + final GlideUrl glideUrl = imageSource.getGlideUrl(); + + if (glideUrl == null) { + promise.resolve(null); + return; + } + + Glide + .with(activity.getApplicationContext()) + .asFile() + .load(glideUrl) + .apply(FastImageViewConverter.getOptions(activity, imageSource, source)) + .listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + promise.reject(ERROR_LOAD_FAILED, e); + return false; + } + + @Override + public boolean onResourceReady(File resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + promise.resolve(resource.getAbsolutePath()); + return false; + } + }) + .submit(); + } + }); + } } diff --git a/ios/FastImage/FFFastImageViewManager.m b/ios/FastImage/FFFastImageViewManager.m index 84ca94e26..07c225970 100644 --- a/ios/FastImage/FFFastImageViewManager.m +++ b/ios/FastImage/FFFastImageViewManager.m @@ -3,6 +3,7 @@ #import #import +#import @implementation FFFastImageViewManager @@ -49,4 +50,20 @@ - (FFFastImageView*)view { }]; } +RCT_EXPORT_METHOD(getCachePath:(nonnull FFFastImageSource *)source + withResolver:(RCTPromiseResolveBlock)resolve + andRejecter:(RCTPromiseRejectBlock)reject) +{ + SDWebImageManager *imageManager = [SDWebImageManager sharedManager]; + NSString *key = [imageManager cacheKeyForURL:source.url]; + BOOL isCached = [[SDImageCache sharedImageCache] diskImageDataExistsWithKey:key]; + + if (isCached) { + NSString *cachePath = [[SDImageCache sharedImageCache] cachePathForKey:key]; + resolve(cachePath); + } else { + resolve([NSNull null]); + } +} + @end diff --git a/src/index.tsx b/src/index.tsx index 478c629e9..c1e1f2576 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -234,6 +234,7 @@ export interface FastImageStaticProperties { preload: (sources: Source[]) => void clearMemoryCache: () => Promise clearDiskCache: () => Promise + getCachePath: (source: Source) => Promise } const FastImage: React.ComponentType & @@ -253,6 +254,8 @@ FastImage.clearMemoryCache = () => FastImage.clearDiskCache = () => NativeModules.FastImageView.clearDiskCache() +FastImage.getCachePath = (source: Source) => NativeModules.FastImageView.getCachePath(source) + const styles = StyleSheet.create({ imageContainer: { overflow: 'hidden',