Permalink
Browse files

upgraded to v0.2 : Cached filenames are now using hash of source url …

…(SHA-1) - this prevents two files having the same filename from being overwritten and ensures filename does not contain illegal/too many characters for the target filesystem
  • Loading branch information...
1 parent 203d97b commit 86ed1354a81951a799ec7a76a70355242a9fb516 @chrisben committed May 20, 2012
Showing with 25 additions and 9 deletions.
  1. +4 −1 README.md
  2. +21 −8 js/imgcache.js
View
5 README.md
@@ -30,13 +30,17 @@ Requirements
Release Notes
-------------
+* v0.2 Cached filenames are now using hash of source url (SHA-1)
* v0.1 Initial release
License
-------
Copyright 2012 (c) Christophe BENOIT
Apache License http://www.apache.org/licenses/LICENSE-2.0
+Code from http://code.google.com/p/tiny-sha1/ is being used which is under the MIT License.
+The copyright for this part belongs to the creator of this work.
+
High level API
--------------
* ImgCache.init() *initialises the local cache*
@@ -77,5 +81,4 @@ otherwise the local filesystem will not be accessible (security error)
Todo
----
* Automated tests
-* Local files should be stored uniquely depending on their original full path: currently if two files have the same name, they will be overwritten
* Find a solution for cache invalidation in case an image changes
View
29 js/imgcache.js
@@ -1,4 +1,4 @@
-/*! imgcache.js v0.1
+/*! imgcache.js v0.2
Copyright 2012 Christophe BENOIT
Licensed under the Apache License, Version 2.0 (the "License");
@@ -47,6 +47,7 @@ var ImgCache = {
return (typeof(cordova) !== 'undefined' || typeof(phonegap) !== 'undefined');
};
+
var logging = function(str) {
if (ImgCache.options.debug)
console.log(str);
@@ -70,17 +71,29 @@ var ImgCache = {
var uri = URI(str);
return uri.path.toLowerCase();
};
+
+ // returns extension from filename (without leading '.')
+ var FileGetExtension = function(filename) {
+ if (!filename)
+ return '';
+ return filename.split('.').pop();
+ };
+
+ /***********************************************
+ tiny-sha1 r4
+ MIT License
+ http://code.google.com/p/tiny-sha1/
+ ***********************************************/
+ function SHA1(s){function U(a,b,c){while(0<c--)a.push(b)}function L(a,b){return(a<<b)|(a>>>(32-b))}function P(a,b,c){return a^b^c}function A(a,b){var c=(b&0xFFFF)+(a&0xFFFF),d=(b>>>16)+(a>>>16)+(c>>>16);return((d&0xFFFF)<<16)|(c&0xFFFF)}var B="0123456789abcdef";return(function(a){var c=[],d=a.length*4,e;for(var i=0;i<d;i++){e=a[i>>2]>>((3-(i%4))*8);c.push(B.charAt((e>>4)&0xF)+B.charAt(e&0xF))}return c.join('')}((function(a,b){var c,d,e,f,g,h=a.length,v=0x67452301,w=0xefcdab89,x=0x98badcfe,y=0x10325476,z=0xc3d2e1f0,M=[];U(M,0x5a827999,20);U(M,0x6ed9eba1,20);U(M,0x8f1bbcdc,20);U(M,0xca62c1d6,20);a[b>>5]|=0x80<<(24-(b%32));a[(((b+65)>>9)<<4)+15]=b;for(var i=0;i<h;i+=16){c=v;d=w;e=x;f=y;g=z;for(var j=0,O=[];j<80;j++){O[j]=j<16?a[j+i]:L(O[j-3]^O[j-8]^O[j-14]^O[j-16],1);var k=(function(a,b,c,d,e){var f=(e&0xFFFF)+(a&0xFFFF)+(b&0xFFFF)+(c&0xFFFF)+(d&0xFFFF),g=(e>>>16)+(a>>>16)+(b>>>16)+(c>>>16)+(d>>>16)+(f>>>16);return((g&0xFFFF)<<16)|(f&0xFFFF)})(j<20?(function(t,a,b){return(t&a)^(~t&b)}(d,e,f)):j<40?P(d,e,f):j<60?(function(t,a,b){return(t&a)^(t&b)^(a&b)}(d,e,f)):P(d,e,f),g,M[j],O[j],L(c,5));g=f;f=e;e=L(d,30);d=c;c=k}v=A(v,c);w=A(w,d);x=A(x,e);y=A(y,f);z=A(z,g)}return[v,w,x,y,z]}((function(t){var a=[],b=255,c=t.length*8;for(var i=0;i<c;i+=8){a[i>>5]|=(t.charCodeAt(i/8)&b)<<(24-(i%32))}return a}(s)).slice(),s.length*8))))}
+ /***********************************************/
+
/****************************************************************************/
// if no local_root set, set relative path
var _getCachedFilePath = function(img_src, local_root) {
- var filename = URIGetFileName(img_src);
- //TODO: we should here return the file path with subfolders matching its url to avoid problem when two files have same name
- // for now not done because that would mean be sure that the subfolders are created (recursively) before writing the file
- // An example of how to recursively create subfolders:
- // http://www.html5rocks.com/en/tutorials/file/filesystem/
- // var path = URIGetPath(img_src);
- //return (local_root ? local_root + '/' : '') + path + '/' + filename;
+ var hash= SHA1(img_src);
+ var ext = FileGetExtension(URIGetFileName(img_src));
+ var filename = hash + (ext ? ('.' + ext) : '');
return (local_root ? local_root + '/' : '') + filename;
};

0 comments on commit 86ed135

Please sign in to comment.