Skip to content

Commit

Permalink
fixing consistency and blob storage - 0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Topener committed Jun 20, 2016
1 parent d87b170 commit 0794eb4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 39 deletions.
104 changes: 65 additions & 39 deletions To.ImageCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
});
};
Expand Down Expand Up @@ -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;
};

Expand All @@ -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);
});
Expand All @@ -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);


};

/**
Expand All @@ -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){
Expand All @@ -157,45 +156,52 @@ 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')){
file.remoteBackup = c.remoteBackup;
}
}

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(),
fileSize: blob.length,
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);
};

/**
Expand All @@ -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){
Expand All @@ -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,
Expand All @@ -247,9 +257,9 @@ var remoteImage = function(url){
});
var blob = image.toBlob();
image = null;

storeFile(filename, blob);

Ti.API.info('TIC - *************');
return blob;
};
Expand All @@ -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);
Expand All @@ -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
*/
Expand All @@ -296,5 +320,7 @@ module.exports = {
removeFile: removeFile,
removeRemote: removeRemote,
remoteImage: remoteImage,
cache: cache
cache: cache,
storeBlob: storeBlob,
getBlob: getBlob
};
7 changes: 7 additions & 0 deletions appc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "timodule",
"access": "public",
"owners": [
"[email protected]",
]
}
Binary file added dist/To.ImageCache-commonjs-0.2.2.zip
Binary file not shown.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 0794eb4

Please sign in to comment.