Skip to content
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
40 changes: 29 additions & 11 deletions src/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@ export class CacheEntry {

options: DownloadOptions;

constructor(uri: string, options: DownloadOptions) {
cacheKey: string;

downloadPromise: Promise<string | undefined> | undefined;

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

async getPath(): Promise<string | undefined> {
const { uri, options } = this;
const { path, exists, tmpPath } = await getCacheEntry(uri);
const { cacheKey } = this;
const { path, exists, tmpPath } = await getCacheEntry(cacheKey);
if (exists) {
return path;
}
if (!this.downloadPromise) {
this.downloadPromise = this.download(path, tmpPath);
}
return this.downloadPromise;
}

private async download(path: string, tmpPath: string): Promise<string | undefined> {
const { uri, options } = this;
const result = await FileSystem.createDownloadResumable(uri, tmpPath, options).downloadAsync();
// If the image download failed, we don't cache anything
if (result && result.status !== 200) {
this.downloadPromise = undefined;
return undefined;
}
await FileSystem.moveAsync({ from: tmpPath, to: path });
Expand All @@ -39,11 +53,11 @@ export class CacheEntry {
export default class CacheManager {
static entries: { [uri: string]: CacheEntry } = {};

static get(uri: string, options: DownloadOptions): CacheEntry {
if (!CacheManager.entries[uri]) {
CacheManager.entries[uri] = new CacheEntry(uri, options);
static get(uri: string, options: DownloadOptions, cacheKey: string): CacheEntry {
if (!CacheManager.entries[cacheKey]) {
CacheManager.entries[cacheKey] = new CacheEntry(uri, options, cacheKey);
}
return CacheManager.entries[uri];
return CacheManager.entries[cacheKey];
}

static async clearCache(): Promise<void> {
Expand All @@ -60,11 +74,15 @@ export default class CacheManager {
}
}

const getCacheEntry = async (uri: string): Promise<{ exists: boolean; path: string; tmpPath: string }> => {
const filename = uri.substring(uri.lastIndexOf("/"), uri.indexOf("?") === -1 ? uri.length : uri.indexOf("?"));
const getCacheEntry = async (cacheKey: string): Promise<{ exists: boolean; path: string; tmpPath: string }> => {
const filename = cacheKey.substring(
cacheKey.lastIndexOf("/"),
cacheKey.indexOf("?") === -1 ? cacheKey.length : cacheKey.indexOf("?")
);
const ext = filename.indexOf(".") === -1 ? ".jpg" : filename.substring(filename.lastIndexOf("."));
const path = `${BASE_DIR}${SHA1(uri)}${ext}`;
const tmpPath = `${BASE_DIR}${SHA1(uri)}-${_.uniqueId()}${ext}`;
const sha = SHA1(cacheKey);
const path = `${BASE_DIR}${sha}${ext}`;
const tmpPath = `${BASE_DIR}${sha}-${_.uniqueId()}${ext}`;
// TODO: maybe we don't have to do this every time
try {
await FileSystem.makeDirectoryAsync(BASE_DIR);
Expand Down
5 changes: 3 additions & 2 deletions src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface ImageProps {
preview?: ImageSourcePropType;
options?: DownloadOptions;
uri: string;
cacheKey?: string;
transitionDuration?: number;
tint?: "dark" | "light";
}
Expand Down Expand Up @@ -66,9 +67,9 @@ export default class Image extends React.Component<ImageProps, ImageState> {
this.mounted = false;
}

async load({ uri, options = {} }: ImageProps): Promise<void> {
async load({ uri, options = {}, cacheKey }: ImageProps): Promise<void> {
if (uri) {
const path = await CacheManager.get(uri, options).getPath();
const path = await CacheManager.get(uri, options, cacheKey || uri).getPath();
if (this.mounted) {
this.setState({ uri: path });
}
Expand Down