Skip to content
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

Feature: getCachePath #961

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ and
- [x] Preload images.
- [x] GIF support.
- [x] Border radius.
- [x] Get image path from cache.

## Usage

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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<File>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
promise.reject(ERROR_LOAD_FAILED, e);
return false;
}

@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
promise.resolve(resource.getAbsolutePath());
return false;
}
})
.submit();
}
});
}
}
17 changes: 17 additions & 0 deletions ios/FastImage/FFFastImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/SDWebImagePrefetcher.h>
#import <SDWebImage/SDImageCache.h>

@implementation FFFastImageViewManager

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export interface FastImageStaticProperties {
preload: (sources: Source[]) => void
clearMemoryCache: () => Promise<void>
clearDiskCache: () => Promise<void>
getCachePath: (source: Source) => Promise<string>
}

const FastImage: React.ComponentType<FastImageProps> &
Expand All @@ -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',
Expand Down