Skip to content

Commit b220b9c

Browse files
committed
Migrated cache fixing on the .Output() function for IndexedDB
Now if you call for media using indexedDB by using`$.fn.cacheImages.Output` it will seek to correct any broken/missing media.
1 parent 01fece1 commit b220b9c

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

jquery.cacheImages.indexeddb.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,50 @@
124124
/*
125125
* Retreive the encoded string from local storage, passes the value to the callback function
126126
*/
127-
$.fn.cacheImages.Output = function( url, callbackFunction, storagePrefix ){
127+
$.fn.cacheImages.Output = function( url, callbackFunction, storagePrefix, secondTry ){
128128
if( typeof storagePrefix === 'undefined' || typeof storagePrefix === 'null' ){ storagePrefix = $.fn.cacheImages.defaults.storagePrefix; }
129+
if( typeof secondTry !== 'boolean' ){ secondTry = false; }
129130
var tempKey = storagePrefix + ':' + url,
130131
thisElem = this;
131132

132133
var request = window.cacheImagesDb.transaction("offlineImages", "readonly").objectStore("offlineImages").get( tempKey );
133134
request.onsuccess = function(e) {
134135
var image = '';
135-
if( typeof e.target.result !== 'undefined' && typeof e.target.result.image === 'string' && /^data:image/.test( e.target.result.image ) === true ){
136+
137+
// Lets try out this image
138+
if( typeof e.target.result !== 'undefined' && typeof e.target.result.image === 'string' ){
136139
image = e.target.result.image;
140+
if( $.fn.cacheImages.testOutput( image, true ) == false ){
141+
delete image; // reset the variable to trigger default
142+
if( secondTry == false ){
143+
// - Force Fetch the URL again
144+
// - Output the new Image
145+
$('body').append(
146+
$('<img style="display: none;" />')
147+
.addClass('cacheImagesRemove')
148+
.cacheImages({
149+
url: url,
150+
forceSave: true,
151+
storagePrefix: storagePrefix,
152+
done: function( image ){
153+
if( typeof callbackFunction == 'function' ){
154+
$.fn.cacheImages.Output( url, callbackFunction, storagePrefix, true );
155+
}
156+
}
157+
})
158+
);
159+
return;
160+
}
161+
}
137162
}
138163

139-
if( image == '' ){
140-
if( /^data:image/.test( $.fn.cacheImages.defaults.defaultImage ) === true ){
164+
165+
166+
//
167+
// Try to grab the default image
168+
if( $.fn.cacheImages.testOutput( image, true ) == false ){
169+
if( $.fn.cacheImages.defaults.debug ){ console.log( 'FV.cacheImage.Output: Failed to load image ' + url ); }
170+
if( $.fn.cacheImages.testOutput( $.fn.cacheImages.defaults.defaultImage, true ) ){
141171
image = $.fn.cacheImages.defaults.defaultImage; // this is an encoded string
142172
}else{
143173
$.fn.cacheImages.Output( $.fn.cacheImages.defaults.defaultImage, callbackFunction, storagePrefix ); // pass the callback through

0 commit comments

Comments
 (0)