|
1 | 1 | /*! imgcache.js
|
2 |
| - Copyright 2012-2017 Christophe BENOIT |
| 2 | + Copyright 2012-2018 Christophe BENOIT |
3 | 3 |
|
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | you may not use this file except in compliance with the License.
|
|
18 | 18 | /*global console,LocalFileSystem,device,FileTransfer,define,module,cordova,phonegap*/
|
19 | 19 |
|
20 | 20 | var ImgCache = {
|
21 |
| - version: '2.0.0', |
| 21 | + version: '2.1.0', |
22 | 22 | // options to override before using the library (but after loading this script!)
|
23 | 23 | options: {
|
24 | 24 | debug: false, /* call the log method ? */
|
@@ -485,40 +485,49 @@ LOG_LEVEL_ERROR = 3;
|
485 | 485 | return img_src.replace(/(['"])/g, '');
|
486 | 486 | };
|
487 | 487 |
|
| 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 | + |
488 | 511 | Private.loadCachedFile = function ($element, img_src, set_path_callback, success_callback, error_callback) {
|
489 | 512 | if (!Private.isImgCacheLoaded()) {
|
490 | 513 | return;
|
491 | 514 | }
|
492 | 515 |
|
493 | 516 | if (!$element) {
|
| 517 | + ImgCache.overridables.log('First parameter of loadCachedFile is empty, should be a DOM element', LOG_LEVEL_ERROR); |
494 | 518 | return;
|
495 | 519 | }
|
496 | 520 |
|
497 | 521 | var filename = Helpers.URIGetFileName(img_src);
|
498 | 522 |
|
499 | 523 | var _gotFileEntry = function (entry) {
|
500 | 524 | 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 () { |
518 | 529 | if (error_callback) { error_callback($element); }
|
519 |
| - }; |
520 |
| - |
521 |
| - entry.file(_win, _fail); |
| 530 | + }); |
522 | 531 | } else {
|
523 | 532 | // using src="filesystem:" kind of url
|
524 | 533 | var new_url = Helpers.EntryGetURL(entry);
|
@@ -730,16 +739,29 @@ LOG_LEVEL_ERROR = 3;
|
730 | 739 | // Returns the local url of a file already available in the cache
|
731 | 740 | ImgCache.getCachedFileURL = function (img_src, success_callback, error_callback) {
|
732 | 741 | var _getURL = function (img_src, entry) {
|
733 |
| - if (!entry) { |
734 |
| - if (error_callback) { error_callback(img_src); } |
735 |
| - } else { |
| 742 | + if (entry) { |
736 | 743 | success_callback(img_src, Helpers.EntryGetURL(entry));
|
| 744 | + } else { |
| 745 | + if (error_callback) { error_callback(img_src); } |
737 | 746 | }
|
738 | 747 | };
|
739 | 748 |
|
740 | 749 | ImgCache.getCachedFile(img_src, _getURL);
|
741 | 750 | };
|
742 | 751 |
|
| 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 | + }; |
743 | 765 |
|
744 | 766 | // checks if a copy of the file has already been cached
|
745 | 767 | // Reminder: this is an asynchronous method!
|
@@ -771,7 +793,9 @@ LOG_LEVEL_ERROR = 3;
|
771 | 793 | return;
|
772 | 794 | }
|
773 | 795 |
|
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); |
775 | 799 | };
|
776 | 800 |
|
777 | 801 | // When the source url is not the 'src' attribute of the given img element
|
|
0 commit comments