From 4206bc6cc1b4426a5666279383c0bd3d0ff76b3c Mon Sep 17 00:00:00 2001 From: ChrisBen Date: Sun, 6 May 2018 10:42:13 +0200 Subject: [PATCH] 2.1.0 - see changelog --- CHANGELOG.md | 8 +++++ LICENSE.md | 2 +- README.md | 9 ++++- bower.json | 2 +- index.html | 22 ++++++++++++ lib/imgcache-promise.js | 18 ++++++++++ lib/imgcache.js | 76 +++++++++++++++++++++++++++-------------- package-lock.json | 2 +- package.js | 2 +- package.json | 2 +- 10 files changed, 111 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f1c11b..3763256 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## master ## +## 2.1.0 ## + +### NEW ### +* Add ImgCache.getCachedFileBase64Data (#107 thanks begrossi) + +### FIXED ### +* Fix spaces in src URLs (#201 thanks henkkelder) + ## 2.0.0 ## ### NEW ### diff --git a/LICENSE.md b/LICENSE.md index 8b5db39..726199c 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ # Apache License -Copyright 2012-2016 (c) Christophe BENOIT +Copyright 2012-2018 (c) Christophe BENOIT *Version 2.0, January 2004* diff --git a/README.md b/README.md index 6a5c5ed..1970d1f 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ This library works with Phonegap/Cordova (v >= 1.7), the supported platforms bei Most methods are **ASYNCHRONOUS** : use callbacks if required. +This library uses plain old ES5 JavaScript with no transpiler and has no dependency. + Using imgcache.js ================= @@ -173,6 +175,7 @@ High level API * ImgCache.**isCached**() *-- checks if a the given image exists in the cache - does not check if the latest version of that file is cached* * ImgCache.**getCachedFile**() *-- returns the cached file* * ImgCache.**getCachedFileURL**() *-- returns the URL of the cached version of a file* +* ImgCache.**getCachedFileBase64Data**() *-- returns the base64 data of a cached file* * ImgCache.**useCachedFile**() *-- replaces the img src with the cached version* * ImgCache.**useCachedFileWithSource**() *-- similar to useCachedFile but with the image source url as extra parameter* * ImgCache.**useOnlineFile**() *-- replaces back the img src with the original (online) version // synchronous method* @@ -276,9 +279,13 @@ Wrapper for Ionic Framework: * [ionic-img-cache](https://github.com/vitaliy-bobrov/ionic-img-cache) +Ionic example: + +* [offline-ionic](https://github.com/mahcr/offline-ionic) + License ------- -Copyright 2012-2017 (c) Christophe BENOIT +Copyright 2012-2018 (c) Christophe BENOIT Apache License - see LICENSE.md diff --git a/bower.json b/bower.json index 135cdb4..91459a0 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "imgcache.js", - "version": "2.0.0", + "version": "2.1.0", "homepage": "https://github.com/chrisben/imgcache.js", "authors": [ { diff --git a/index.html b/index.html index 0730408..fe608bc 100644 --- a/index.html +++ b/index.html @@ -175,6 +175,28 @@ } } }, + { + name: 'getCachedFileBase64Data', + test: function (ok, nok) { + var img_src = getBackgroundImageUrl($test_div); + if (img_src) { + ImgCache.getCachedFileBase64Data(img_src, function (src, base64) { + if (base64) { + logger('Cached File base64 data: ' + base64.slice(0, 80) + '[...]', LOG_LEVEL_INFO); + ok(); + } else { + nok(); + } + }, function (img_src) { + logger('Failed to retrieve cache base64 data for file: ' + img_src, LOG_LEVEL_ERROR); + nok(); + }); + } + else { + nok(); + } + } + }, { name: 'useCachedFile', test: function (ok, nok) { diff --git a/lib/imgcache-promise.js b/lib/imgcache-promise.js index 8ab487f..f138f57 100644 --- a/lib/imgcache-promise.js +++ b/lib/imgcache-promise.js @@ -91,6 +91,24 @@ var ImgCachePromise = {}; }); }; + ImgCachePromise.getCachedFileBase64Data = function (url) { + return initCheck() + .then(function () { + return new Promise(function (resolve, reject) { + ImgCache.getCachedFileBase64Data( + url, + function(img_src, file_url) { + if (file_url === null) { + reject(); + } else { + resolve(file_url); + } + } + ); + }); + }); + }; + // $img: jQuery or DOM element for the element ImgCachePromise.useCachedFile = function ($img) { return initCheck() diff --git a/lib/imgcache.js b/lib/imgcache.js index 204a35f..659590d 100644 --- a/lib/imgcache.js +++ b/lib/imgcache.js @@ -1,5 +1,5 @@ /*! imgcache.js - Copyright 2012-2017 Christophe BENOIT + Copyright 2012-2018 Christophe BENOIT Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ /*global console,LocalFileSystem,device,FileTransfer,define,module,cordova,phonegap*/ var ImgCache = { - version: '2.0.0', + version: '2.1.0', // options to override before using the library (but after loading this script!) options: { debug: false, /* call the log method ? */ @@ -485,12 +485,36 @@ LOG_LEVEL_ERROR = 3; return img_src.replace(/(['"])/g, ''); }; + Private.getBase64DataFromEntry = function (entry, filename, success_callback, error_callback) { + var _success = function (file) { + var reader = new FileReader(); + reader.onloadend = function (e) { + var base64content = e.target.result; + if (base64content) { + ImgCache.overridables.log('File ' + filename + ' loaded from cache', LOG_LEVEL_INFO); + if (success_callback) { success_callback(base64content); } + } else { + ImgCache.overridables.log('File in cache ' + filename + ' is empty', LOG_LEVEL_WARNING); + if (error_callback) { error_callback(filename); } + } + }; + reader.readAsDataURL(file); + }; + var _failure = function (error) { + ImgCache.overridables.log('Failed to read file ' + error.code, LOG_LEVEL_ERROR); + if (error_callback) { error_callback(filename); } + }; + + entry.file(_success, _failure); + }; + Private.loadCachedFile = function ($element, img_src, set_path_callback, success_callback, error_callback) { if (!Private.isImgCacheLoaded()) { return; } if (!$element) { + ImgCache.overridables.log('First parameter of loadCachedFile is empty, should be a DOM element', LOG_LEVEL_ERROR); return; } @@ -498,27 +522,12 @@ LOG_LEVEL_ERROR = 3; var _gotFileEntry = function (entry) { if (ImgCache.options.useDataURI) { - var _win = function (file) { - var reader = new FileReader(); - reader.onloadend = function (e) { - var base64content = e.target.result; - if (!base64content) { - ImgCache.overridables.log('File in cache ' + filename + ' is empty', LOG_LEVEL_WARNING); - if (error_callback) { error_callback($element); } - return; - } - set_path_callback($element, base64content, img_src); - ImgCache.overridables.log('File ' + filename + ' loaded from cache', LOG_LEVEL_INFO); - if (success_callback) { success_callback($element); } - }; - reader.readAsDataURL(file); - }; - var _fail = function (error) { - ImgCache.overridables.log('Failed to read file ' + error.code, LOG_LEVEL_ERROR); + Private.getBase64DataFromEntry(entry, filename, function (base64content) { + set_path_callback($element, base64content, img_src); + if (success_callback) { success_callback($element); } + }, function () { if (error_callback) { error_callback($element); } - }; - - entry.file(_win, _fail); + }); } else { // using src="filesystem:" kind of url var new_url = Helpers.EntryGetURL(entry); @@ -730,16 +739,29 @@ LOG_LEVEL_ERROR = 3; // Returns the local url of a file already available in the cache ImgCache.getCachedFileURL = function (img_src, success_callback, error_callback) { var _getURL = function (img_src, entry) { - if (!entry) { - if (error_callback) { error_callback(img_src); } - } else { + if (entry) { success_callback(img_src, Helpers.EntryGetURL(entry)); + } else { + if (error_callback) { error_callback(img_src); } } }; ImgCache.getCachedFile(img_src, _getURL); }; + ImgCache.getCachedFileBase64Data = function (img_src, success_callback, error_callback) { + var _getData = function(img_src, entry) { + if (entry) { + Private.getBase64DataFromEntry(entry, img_src, function (base64content) { + success_callback(img_src, base64content); + }, error_callback); + } else { + if (error_callback) { error_callback(img_src); } + } + }; + + ImgCache.getCachedFile(img_src, _getData); + }; // checks if a copy of the file has already been cached // Reminder: this is an asynchronous method! @@ -771,7 +793,9 @@ LOG_LEVEL_ERROR = 3; return; } - Private.loadCachedFile($img, DomHelpers.getAttribute($img, 'src'), Private.setNewImgPath, success_callback, error_callback); + var img_url = Helpers.sanitizeURI(DomHelpers.getAttribute($img, 'src')); + + Private.loadCachedFile($img, img_url, Private.setNewImgPath, success_callback, error_callback); }; // When the source url is not the 'src' attribute of the given img element diff --git a/package-lock.json b/package-lock.json index 4983a03..d9a986c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "imgcache.js", + "name": "@chrisben/imgcache.js", "version": "2.0.0", "lockfileVersion": 1, "requires": true, diff --git a/package.js b/package.js index 6e89baf..a3a2e74 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ summary: "JS library that stores images locally for offline apps using PhoneGap/Cordova or browsers supporting the new html5 File API", - version: "2.0.0", + version: "2.1.0", git: "https://github.com/chrisben/imgcache.js" }); diff --git a/package.json b/package.json index 942be14..ddde6e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@chrisben/imgcache.js", - "version": "2.0.0", + "version": "2.1.0", "description": "JS library based on the File API to cache images for offline recovery (target: cordova/phonegap & chrome)", "main": "lib/imgcache.js", "directories": {