Skip to content

Commit 4206bc6

Browse files
committed
2.1.0 - see changelog
1 parent e7b9a5a commit 4206bc6

10 files changed

+111
-32
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## master ##
44

5+
## 2.1.0 ##
6+
7+
### NEW ###
8+
* Add ImgCache.getCachedFileBase64Data (#107 thanks begrossi)
9+
10+
### FIXED ###
11+
* Fix spaces in src URLs (#201 thanks henkkelder)
12+
513
## 2.0.0 ##
614

715
### NEW ###

LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Apache License
22

3-
Copyright 2012-2016 (c) Christophe BENOIT
3+
Copyright 2012-2018 (c) Christophe BENOIT
44

55
*Version 2.0, January 2004*
66

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ This library works with Phonegap/Cordova (v >= 1.7), the supported platforms bei
2020

2121
Most methods are **ASYNCHRONOUS** : use callbacks if required.
2222

23+
This library uses plain old ES5 JavaScript with no transpiler and has no dependency.
24+
2325
Using imgcache.js
2426
=================
2527

@@ -173,6 +175,7 @@ High level API
173175
* ImgCache.**isCached**() *-- checks if a the given image exists in the cache - does not check if the latest version of that file is cached*
174176
* ImgCache.**getCachedFile**() *-- returns the cached file*
175177
* ImgCache.**getCachedFileURL**() *-- returns the URL of the cached version of a file*
178+
* ImgCache.**getCachedFileBase64Data**() *-- returns the base64 data of a cached file*
176179
* ImgCache.**useCachedFile**() *-- replaces the img src with the cached version*
177180
* ImgCache.**useCachedFileWithSource**() *-- similar to useCachedFile but with the image source url as extra parameter*
178181
* ImgCache.**useOnlineFile**() *-- replaces back the img src with the original (online) version // synchronous method*
@@ -276,9 +279,13 @@ Wrapper for Ionic Framework:
276279

277280
* [ionic-img-cache](https://github.com/vitaliy-bobrov/ionic-img-cache)
278281

282+
Ionic example:
283+
284+
* [offline-ionic](https://github.com/mahcr/offline-ionic)
285+
279286
License
280287
-------
281-
Copyright 2012-2017 (c) Christophe BENOIT
288+
Copyright 2012-2018 (c) Christophe BENOIT
282289

283290
Apache License - see LICENSE.md
284291

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imgcache.js",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"homepage": "https://github.com/chrisben/imgcache.js",
55
"authors": [
66
{

index.html

+22
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,28 @@
175175
}
176176
}
177177
},
178+
{
179+
name: 'getCachedFileBase64Data',
180+
test: function (ok, nok) {
181+
var img_src = getBackgroundImageUrl($test_div);
182+
if (img_src) {
183+
ImgCache.getCachedFileBase64Data(img_src, function (src, base64) {
184+
if (base64) {
185+
logger('Cached File base64 data: ' + base64.slice(0, 80) + '[...]', LOG_LEVEL_INFO);
186+
ok();
187+
} else {
188+
nok();
189+
}
190+
}, function (img_src) {
191+
logger('Failed to retrieve cache base64 data for file: ' + img_src, LOG_LEVEL_ERROR);
192+
nok();
193+
});
194+
}
195+
else {
196+
nok();
197+
}
198+
}
199+
},
178200
{
179201
name: 'useCachedFile',
180202
test: function (ok, nok) {

lib/imgcache-promise.js

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ var ImgCachePromise = {};
9191
});
9292
};
9393

94+
ImgCachePromise.getCachedFileBase64Data = function (url) {
95+
return initCheck()
96+
.then(function () {
97+
return new Promise(function (resolve, reject) {
98+
ImgCache.getCachedFileBase64Data(
99+
url,
100+
function(img_src, file_url) {
101+
if (file_url === null) {
102+
reject();
103+
} else {
104+
resolve(file_url);
105+
}
106+
}
107+
);
108+
});
109+
});
110+
};
111+
94112
// $img: jQuery or DOM element for the <img/> element
95113
ImgCachePromise.useCachedFile = function ($img) {
96114
return initCheck()

lib/imgcache.js

+50-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*! imgcache.js
2-
Copyright 2012-2017 Christophe BENOIT
2+
Copyright 2012-2018 Christophe BENOIT
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@
1818
/*global console,LocalFileSystem,device,FileTransfer,define,module,cordova,phonegap*/
1919

2020
var ImgCache = {
21-
version: '2.0.0',
21+
version: '2.1.0',
2222
// options to override before using the library (but after loading this script!)
2323
options: {
2424
debug: false, /* call the log method ? */
@@ -485,40 +485,49 @@ LOG_LEVEL_ERROR = 3;
485485
return img_src.replace(/(['"])/g, '');
486486
};
487487

488+
Private.getBase64DataFromEntry = function (entry, filename, success_callback, error_callback) {
489+
var _success = function (file) {
490+
var reader = new FileReader();
491+
reader.onloadend = function (e) {
492+
var base64content = e.target.result;
493+
if (base64content) {
494+
ImgCache.overridables.log('File ' + filename + ' loaded from cache', LOG_LEVEL_INFO);
495+
if (success_callback) { success_callback(base64content); }
496+
} else {
497+
ImgCache.overridables.log('File in cache ' + filename + ' is empty', LOG_LEVEL_WARNING);
498+
if (error_callback) { error_callback(filename); }
499+
}
500+
};
501+
reader.readAsDataURL(file);
502+
};
503+
var _failure = function (error) {
504+
ImgCache.overridables.log('Failed to read file ' + error.code, LOG_LEVEL_ERROR);
505+
if (error_callback) { error_callback(filename); }
506+
};
507+
508+
entry.file(_success, _failure);
509+
};
510+
488511
Private.loadCachedFile = function ($element, img_src, set_path_callback, success_callback, error_callback) {
489512
if (!Private.isImgCacheLoaded()) {
490513
return;
491514
}
492515

493516
if (!$element) {
517+
ImgCache.overridables.log('First parameter of loadCachedFile is empty, should be a DOM element', LOG_LEVEL_ERROR);
494518
return;
495519
}
496520

497521
var filename = Helpers.URIGetFileName(img_src);
498522

499523
var _gotFileEntry = function (entry) {
500524
if (ImgCache.options.useDataURI) {
501-
var _win = function (file) {
502-
var reader = new FileReader();
503-
reader.onloadend = function (e) {
504-
var base64content = e.target.result;
505-
if (!base64content) {
506-
ImgCache.overridables.log('File in cache ' + filename + ' is empty', LOG_LEVEL_WARNING);
507-
if (error_callback) { error_callback($element); }
508-
return;
509-
}
510-
set_path_callback($element, base64content, img_src);
511-
ImgCache.overridables.log('File ' + filename + ' loaded from cache', LOG_LEVEL_INFO);
512-
if (success_callback) { success_callback($element); }
513-
};
514-
reader.readAsDataURL(file);
515-
};
516-
var _fail = function (error) {
517-
ImgCache.overridables.log('Failed to read file ' + error.code, LOG_LEVEL_ERROR);
525+
Private.getBase64DataFromEntry(entry, filename, function (base64content) {
526+
set_path_callback($element, base64content, img_src);
527+
if (success_callback) { success_callback($element); }
528+
}, function () {
518529
if (error_callback) { error_callback($element); }
519-
};
520-
521-
entry.file(_win, _fail);
530+
});
522531
} else {
523532
// using src="filesystem:" kind of url
524533
var new_url = Helpers.EntryGetURL(entry);
@@ -730,16 +739,29 @@ LOG_LEVEL_ERROR = 3;
730739
// Returns the local url of a file already available in the cache
731740
ImgCache.getCachedFileURL = function (img_src, success_callback, error_callback) {
732741
var _getURL = function (img_src, entry) {
733-
if (!entry) {
734-
if (error_callback) { error_callback(img_src); }
735-
} else {
742+
if (entry) {
736743
success_callback(img_src, Helpers.EntryGetURL(entry));
744+
} else {
745+
if (error_callback) { error_callback(img_src); }
737746
}
738747
};
739748

740749
ImgCache.getCachedFile(img_src, _getURL);
741750
};
742751

752+
ImgCache.getCachedFileBase64Data = function (img_src, success_callback, error_callback) {
753+
var _getData = function(img_src, entry) {
754+
if (entry) {
755+
Private.getBase64DataFromEntry(entry, img_src, function (base64content) {
756+
success_callback(img_src, base64content);
757+
}, error_callback);
758+
} else {
759+
if (error_callback) { error_callback(img_src); }
760+
}
761+
};
762+
763+
ImgCache.getCachedFile(img_src, _getData);
764+
};
743765

744766
// checks if a copy of the file has already been cached
745767
// Reminder: this is an asynchronous method!
@@ -771,7 +793,9 @@ LOG_LEVEL_ERROR = 3;
771793
return;
772794
}
773795

774-
Private.loadCachedFile($img, DomHelpers.getAttribute($img, 'src'), Private.setNewImgPath, success_callback, error_callback);
796+
var img_url = Helpers.sanitizeURI(DomHelpers.getAttribute($img, 'src'));
797+
798+
Private.loadCachedFile($img, img_url, Private.setNewImgPath, success_callback, error_callback);
775799
};
776800

777801
// When the source url is not the 'src' attribute of the given img element

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
summary: "JS library that stores images locally for offline apps using PhoneGap/Cordova or browsers supporting the new html5 File API",
3-
version: "2.0.0",
3+
version: "2.1.0",
44
git: "https://github.com/chrisben/imgcache.js"
55
});
66

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chrisben/imgcache.js",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "JS library based on the File API to cache images for offline recovery (target: cordova/phonegap & chrome)",
55
"main": "lib/imgcache.js",
66
"directories": {

0 commit comments

Comments
 (0)