Skip to content

Commit 4b5d0d0

Browse files
Copilotfarfromrefug
andcommitted
Fix isInDiskCacheAsync for local file:// URIs - check file existence instead of Glide cache
Co-authored-by: farfromrefug <[email protected]>
1 parent 16161b7 commit 4b5d0d0

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

packages/image/platforms/android/java/com/nativescript/image/EvictionManager.java

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -580,20 +580,52 @@ public void isInDiskCacheAsync(final String id, final DiskPresenceCallback callb
580580

581581
try {
582582
if (hasValidStoredKeys(s)) {
583-
CustomDataCacheKey cachekey = new CustomDataCacheKey(s.sourceKey, s.signature);
584583
Log.d("JS", "EvictionManager isInDiskCacheAsync " + id + " " + s.sourceKey+ " " + s.sourceKey.getClass().getName()+ " " + s.signature);
585584

586-
sourcePresent = dc.get(cachekey) != null;
585+
// Special handling for local file:// URIs (identified by ObjectKey sourceKey)
586+
// For local files, check if the file exists on disk instead of Glide's cache
587+
// since local files are not copied to Glide's disk cache
588+
if (s.sourceKey instanceof com.bumptech.glide.signature.ObjectKey && id.startsWith("file://")) {
589+
try {
590+
// Extract file path from file:// URI
591+
String filePath = id.substring(7); // Remove "file://" prefix
592+
java.io.File file = new java.io.File(filePath);
593+
sourcePresent = file.exists() && file.isFile();
594+
Log.d("JS", "EvictionManager local file check: " + filePath + " exists=" + sourcePresent);
595+
} catch (Exception e) {
596+
Log.w("JS", "EvictionManager failed to check local file: " + id, e);
597+
sourcePresent = false;
598+
}
599+
} else {
600+
// For network URLs, check Glide's disk cache
601+
CustomDataCacheKey cachekey = new CustomDataCacheKey(s.sourceKey, s.signature);
602+
sourcePresent = dc.get(cachekey) != null;
603+
}
604+
605+
// Always check for transformed versions in cache
587606
byte[] transformationBytes = getTransformationBytesFromStoredKeys(s);
588607
Key resourceKey = new RecreatedResourceKey(s.sourceKey, s.signature, s.width, s.height, transformationBytes,
589608
s.decodedResourceClass, s.optionsKeyBytes);
590609
transformedPresent = dc.get(resourceKey) != null;
591610
} else {
592611
// fallback: check ObjectKey(id) as source
593-
Key fallback = new com.bumptech.glide.signature.ObjectKey(id);
594-
sourcePresent = dc.get(fallback) != null;
612+
// Also handle file:// URIs in fallback case
613+
if (id.startsWith("file://")) {
614+
try {
615+
String filePath = id.substring(7);
616+
java.io.File file = new java.io.File(filePath);
617+
sourcePresent = file.exists() && file.isFile();
618+
Log.d("JS", "EvictionManager fallback local file check: " + filePath + " exists=" + sourcePresent);
619+
} catch (Exception e) {
620+
sourcePresent = false;
621+
}
622+
} else {
623+
Key fallback = new com.bumptech.glide.signature.ObjectKey(id);
624+
sourcePresent = dc.get(fallback) != null;
625+
}
595626
}
596627
} catch (Exception ignored) {
628+
Log.w("JS", "EvictionManager isInDiskCacheAsync exception for " + id, ignored);
597629
}
598630

599631
final boolean finalSource = sourcePresent;
@@ -619,16 +651,38 @@ public boolean[] isInDiskCacheBlocking(final String id) {
619651
return new boolean[] { false, false };
620652
try {
621653
if (hasValidStoredKeys(s)) {
622-
// Check source data using DataCacheKey
623-
CustomDataCacheKey dataCacheKey = new CustomDataCacheKey(s.sourceKey, s.signature);
624-
sourcePresent = dc.get(dataCacheKey) != null;
654+
// Special handling for local file:// URIs
655+
if (s.sourceKey instanceof com.bumptech.glide.signature.ObjectKey && id.startsWith("file://")) {
656+
try {
657+
String filePath = id.substring(7);
658+
java.io.File file = new java.io.File(filePath);
659+
sourcePresent = file.exists() && file.isFile();
660+
} catch (Exception e) {
661+
sourcePresent = false;
662+
}
663+
} else {
664+
// For network URLs, check Glide's disk cache
665+
CustomDataCacheKey dataCacheKey = new CustomDataCacheKey(s.sourceKey, s.signature);
666+
sourcePresent = dc.get(dataCacheKey) != null;
667+
}
625668
// Check transformed resource
626669
Key resourceKey = new RecreatedResourceKey(s.sourceKey, s.signature, s.width, s.height,
627670
getTransformationBytesFromStoredKeys(s), s.decodedResourceClass, s.optionsKeyBytes);
628671
transformedPresent = dc.get(resourceKey) != null;
629672
} else {
630-
Key fallback = new com.bumptech.glide.signature.ObjectKey(id);
631-
sourcePresent = dc.get(fallback) != null;
673+
// Fallback case - also handle file:// URIs
674+
if (id.startsWith("file://")) {
675+
try {
676+
String filePath = id.substring(7);
677+
java.io.File file = new java.io.File(filePath);
678+
sourcePresent = file.exists() && file.isFile();
679+
} catch (Exception e) {
680+
sourcePresent = false;
681+
}
682+
} else {
683+
Key fallback = new com.bumptech.glide.signature.ObjectKey(id);
684+
sourcePresent = dc.get(fallback) != null;
685+
}
632686
}
633687
} catch (Exception ignored) {
634688
}

0 commit comments

Comments
 (0)