Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active August 29, 2015 14:22
Show Gist options
  • Save joshdholtz/33871e08e2fd83ea1b62 to your computer and use it in GitHub Desktop.
Save joshdholtz/33871e08e2fd83ea1b62 to your computer and use it in GitHub Desktop.
Caching locally created files with imgcache.js
app.config(['$compileProvider',
function ($compileProvider) {
$compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|local|data):/);
}]);
// I manually pasted these lines into imgcache.js to that "Private" and "Helpers" could be "public"
// These go as the first lines after "(function ($) {"
ImgCache.getPrivate = function() { return Private; };
ImgCache.getHelpers = function() { return Helpers; };
(function() {
Private = ImgCache.getPrivate();
Helpers = ImgCache.getHelpers();
Private.FileTransferWrapper.prototype.cacheLocal = function (imageData, localPath, success_callback, error_callback) {
var blob = this.dataURItoBlob(imageData);
var filesystem = this.filesystem;
filesystem.root.getFile(localPath, { create:true }, function (fileEntry) {
fileEntry.createWriter(function (writer) {
writer.onerror = error_callback;
writer.onwriteend = function () { success_callback(fileEntry); };
writer.write(blob, error_callback);
}, error_callback);
}, error_callback);
};
Private.FileTransferWrapper.prototype.dataURItoBlob = function (dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
ImgCache.cacheLocalFile = function (key, img_data, success_callback, error_callback, on_progress) {
if (!Private.isImgCacheLoaded() || !img_data) {
return;
}
var filePath = Private.getCachedFileFullPath(key);
var fileTransfer = new Private.FileTransferWrapper(ImgCache.attributes.filesystem);
fileTransfer.cacheLocal(
img_data,
filePath,
function (entry) {
entry.getMetadata(function (metadata) {
if (metadata && metadata.hasOwnProperty('size')) {
ImgCache.overridables.log('Cached file size: ' + metadata.size, LOG_LEVEL_INFO);
Private.setCurrentSize(ImgCache.getCurrentSize() + parseInt(metadata.size, 10));
} else {
ImgCache.overridables.log('No metadata size property available', LOG_LEVEL_INFO);
}
});
ImgCache.overridables.log('Download complete: ' + Helpers.EntryGetPath(entry), LOG_LEVEL_INFO);
// iOS: the file should not be backed up in iCloud
// new from cordova 1.8 only
if (entry.setMetadata) {
entry.setMetadata(
function () {
/* success*/
ImgCache.overridables.log('com.apple.MobileBackup metadata set', LOG_LEVEL_INFO);
},
function () {
/* failure */
ImgCache.overridables.log('com.apple.MobileBackup metadata could not be set', LOG_LEVEL_WARNING);
},
{ 'com.apple.MobileBackup': 1 }
// 1=NO backup oddly enough..
);
}
if (success_callback) { success_callback(); }
},
function (error) {
if (error.source) { ImgCache.overridables.log('Download error source: ' + error.source, LOG_LEVEL_ERROR); }
if (error.target) { ImgCache.overridables.log('Download error target: ' + error.target, LOG_LEVEL_ERROR); }
ImgCache.overridables.log('Download error code: ' + error.code, LOG_LEVEL_ERROR);
if (error_callback) { error_callback(); }
},
on_progress
);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment