diff --git a/To.ImageCache.js b/To.ImageCache.js index 19e30f5..eb9209e 100644 --- a/To.ImageCache.js +++ b/To.ImageCache.js @@ -13,20 +13,15 @@ var fileList = Ti.App.Properties.getList('To.ImageCache.ImageList',[]); */ var config = function(config){ if (!config){ - var copy = {}; - for (var attr in c) { - if (c.hasOwnProperty(attr)) copy[attr] = c[attr]; - } - - return copy; + return; } if (config.debug){ Ti.API.info('TIC - setting config'); } _.each(c, function(value, key){ if (config.hasOwnProperty(key)){ - c[key] = config[key]; - Ti.API.info('TIC - setting ' + key + ' to ' + config[key]); + c[key] = value; + Ti.API.info('TIC - setting ' + key + ' to ' + value); }; }); }; @@ -59,11 +54,11 @@ var cacheSize = function(){ var bytes = 0; if (c.debug) Ti.API.info('TIC - calculating cache size'); - + _.each(fileList, function(file){ bytes += file.fileSize; }); - + return bytes; }; @@ -73,7 +68,7 @@ var cacheSize = function(){ var clearCache = function(){ if (c.debug) Ti.API.info('TIC - Completely emtying cache'); - + _.each(fileList, function(file){ removeFile(file.filename); }); @@ -89,15 +84,17 @@ var flushExpired = function(){ var removeFiles = []; _.each(fileList, function(file){ if (Date.now() - (file.added + (file.expireTime * 1000)) > 0){ - + if (c.debug) Ti.API.info('TIC - found expired file, removing'); - + removeFiles.push(file.filename); } }); - + _.each(removeFiles, removeFile); + + }; /** @@ -108,30 +105,32 @@ var flushExpired = function(){ var removeFile = function(filename){ if (c.debug) Ti.API.info('TIC - removing file ' + filename); - + var file = hasFile(filename); if (!file){ return false; } - + var path = Ti.Filesystem.applicationDataDirectory + file.folder; var f = Ti.Filesystem.getFile(path, file.filename); - + if (!f.exists()){ fileList = _.without(fileList, file); Ti.App.Properties.setList('To.ImageCache.ImageList', fileList); if (c.debug) Ti.API.info('TIC - file has aleady been removed'); - + return false; } - + if (f.deleteFile()){ if (c.debug) Ti.API.info('TIC - file has been removed'); + } else { fileList = _.without(fileList, file); Ti.App.Properties.setList('To.ImageCache.ImageList', fileList); } + }; function md5FileName(url){ @@ -157,22 +156,22 @@ var removeRemote = function(url){ * @param {String} filename (needs to be unique, otherwise will overwrite) * @param {Blob} Blob of the image */ -var storeFile = function(filename, blob){ +var storeFile = function(filename, blob, overwrite){ if (c.debug){ Ti.API.info('TIC - store file ' + filename); } // check if directory has been created checkDir(); - + // we already have this file - if (hasFile(filename)){ + if (!overwrite && hasFile(filename)){ blob = null; return; } - + var path = Ti.Filesystem.applicationDataDirectory + c.folder; var file = Ti.Filesystem.getFile(path, filename); - + if (file.write(blob)){ // do we need to cache the file? if (Ti.Platform.name == 'iPhone OS' && c.hasOwnProperty('remoteBackup')){ @@ -180,9 +179,16 @@ var storeFile = function(filename, blob){ } } + var nPath = file.nativePath; + // destroy file after it has been saved file = null; - + + // looks like there is no blob + if (!blob || !blob.length){ + return; + } + fileList.push({ filename: filename, added: Date.now(), @@ -190,12 +196,12 @@ var storeFile = function(filename, blob){ expireTime: c.expireTime, folder: c.folder }); - + // add file to collection Ti.App.Properties.setList('To.ImageCache.ImageList', fileList); - // destroy blob blob = null; + return readFile(filename); }; /** @@ -206,17 +212,21 @@ var readFile = function(filename){ Ti.API.info('TIC - reading file from system ' + filename); } var file = hasFile(filename); - + + if (!file){ + return false; + } + var path = Ti.Filesystem.applicationDataDirectory + file.folder; - var file = Ti.Filesystem.getFile(path, filename); - return file.read(); + var file = Ti.Filesystem.getFile(path, filename).read(); + return file; }; /** * this function will always return a blob, wether it was cached or not * in case it wasn't cached, it will do so after first fetching it. * in case it was cached, it will just read the file and return the blob - * Therefore, only use this function if you want to cache it. + * Therefore, only use this function if you want to cache it. * @param {String} url */ var remoteImage = function(url){ @@ -227,18 +237,18 @@ var remoteImage = function(url){ // calculate local filename var filename = md5FileName(url); Ti.API.info(filename); - + if (hasFile(filename)){ if (c.debug){ Ti.API.info('TIC - has file in system'); Ti.API.info('TIC - *************'); } - + // get file return readFile(filename); } Ti.API.info('TIC - doesn\'t have file yet'); - + // generate a blob var image = Ti.UI.createImageView({ image : url, @@ -247,9 +257,9 @@ var remoteImage = function(url){ }); var blob = image.toBlob(); image = null; - + storeFile(filename, blob); - + Ti.API.info('TIC - *************'); return blob; }; @@ -269,10 +279,10 @@ var cache = function(url, timeout, cb){ if (hasFile(filename)){ if (c.debug) Ti.API.info('TIC - file already cached'); - + return false; } - + var xhr = Titanium.Network.createHTTPClient({ onload: function() { storeFile(filename, this.responseData); @@ -285,6 +295,20 @@ var cache = function(url, timeout, cb){ return true; }; +var storeBlob = function(name, blob, cb){ + var filename = md5FileName('blob-' + name); + var nPath = storeFile(filename, blob, true); + cb && cb(nPath); + blob = null; + return readFile(filename); +}; + +var getBlob = function(name){ + var filename = md5FileName('blob-' + name); + + return readFile(filename); +}; + /** * only export what is needed externally */ @@ -296,5 +320,7 @@ module.exports = { removeFile: removeFile, removeRemote: removeRemote, remoteImage: remoteImage, - cache: cache + cache: cache, + storeBlob: storeBlob, + getBlob: getBlob }; diff --git a/appc.json b/appc.json new file mode 100644 index 0000000..07b0afa --- /dev/null +++ b/appc.json @@ -0,0 +1,7 @@ +{ + "type": "timodule", + "access": "public", + "owners": [ + "info@topener.nl", + ] +} \ No newline at end of file diff --git a/dist/To.ImageCache-commonjs-0.2.2.zip b/dist/To.ImageCache-commonjs-0.2.2.zip new file mode 100644 index 0000000..771ad9d Binary files /dev/null and b/dist/To.ImageCache-commonjs-0.2.2.zip differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..b5e2cf1 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "To.ImageCache", + "description": "A simple image cacher module for Titanium Applications if you want to maintain it manually", + "version": "0.2.2", + "main": "To.ImageCache.js", + "keywords": ["cache","titanium","images"], + "author": "Rene Pot", + "repository": { + "type": "git", + "url": "github.com/Topener/To.ImageCache" + }, + "bugs": { + "url": "github.com/Topener/To.ImageCache/issues" + }, + "homepage": "http://renepot.net" +} \ No newline at end of file