jellyfin-web/dashboard-ui/thirdparty/cordova/imagestore.js

195 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-05-15 08:46:20 -07:00
(function () {
function setImageIntoElement(elem, url) {
if (elem.tagName === "DIV") {
elem.style.backgroundImage = "url('" + url + "')";
} else {
elem.setAttribute("src", url);
}
}
function onDbOpened(imageStore, db) {
imageStore._db = db;
window.ImageStore = imageStore;
}
function openDb(imageStore) {
// Create/open database
var db = window.sqlitePlugin.openDatabase({ name: "my.db" });
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS images (id text primary key, data text)');
tx.executeSql('create index if not exists idx_images on images(id)');
onDbOpened(imageStore, db);
});
}
function sqliteImageStore() {
var self = this;
2015-05-29 16:51:33 -07:00
self.addImageToDatabase = function (blob, key, deferred) {
2015-05-15 08:46:20 -07:00
console.log("addImageToDatabase");
self.db().transaction(function (tx) {
2015-05-29 16:51:33 -07:00
tx.executeSql("REPLACE INTO images (id, data) VALUES (?,?)", [key, blob], function (tx, res) {
2015-05-15 08:46:20 -07:00
deferred.resolve();
}, function (e) {
deferred.reject();
});
});
};
self.db = function () {
return self._db;
};
self.get = function (key) {
var deferred = DeferredBuilder.Deferred();
self.db().transaction(function (tx) {
tx.executeSql("SELECT data from images where id=?", [key], function (tx, res) {
if (res.rows.length) {
deferred.resolveWith(null, [res.rows.item(0).data]);
} else {
deferred.reject();
}
}, function (e) {
deferred.reject();
});
});
return deferred.promise();
};
2015-06-01 07:49:23 -07:00
function getCacheKey(url) {
// Try to strip off the domain to share the cache between local and remote connections
var index = url.indexOf('://');
if (index != -1) {
url = url.substring(index + 3);
index = url.indexOf('/');
if (index != -1) {
url = url.substring(index + 1);
}
}
return CryptoJS.MD5(url).toString();
}
2015-05-15 08:46:20 -07:00
self.getImageUrl = function (originalUrl) {
console.log('getImageUrl:' + originalUrl);
2015-06-01 07:49:23 -07:00
var key = getCacheKey(originalUrl);
2015-05-15 08:46:20 -07:00
var deferred = DeferredBuilder.Deferred();
self.get(key).done(function (url) {
deferred.resolveWith(null, [url]);
}).fail(function () {
self.downloadImage(originalUrl, key).done(function () {
self.get(key).done(function (url) {
deferred.resolveWith(null, [url]);
}).fail(function () {
deferred.reject();
});
}).fail(function () {
deferred.reject();
});
});
return deferred.promise();
};
self.downloadImage = function (url, key) {
var deferred = DeferredBuilder.Deferred();
console.log('downloadImage:' + url);
// Create XHR
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
// Set the responseType to blob
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
console.log("Image retrieved");
try {
var arr = new Uint8Array(this.response);
// Convert the int array to a binary string
// We have to use apply() as we are converting an *array*
// and String.fromCharCode() takes one or more single values, not
// an array.
var raw = String.fromCharCode.apply(null, arr);
// This works!!!
var b64 = btoa(raw);
var dataURL = "data:image/jpeg;base64," + b64;
// Put the received blob into the database
2015-05-29 16:51:33 -07:00
self.addImageToDatabase(dataURL, key, deferred);
2015-05-15 08:46:20 -07:00
} catch (err) {
console.log("Error adding image to database");
deferred.reject();
}
} else {
deferred.reject();
}
}, false);
// Send XHR
xhr.send();
return deferred.promise();
};
self.setImageInto = function (elem, url) {
function onFail() {
setImageIntoElement(elem, url);
}
self.getImageUrl(url).done(function (localUrl) {
setImageIntoElement(elem, localUrl);
}).fail(onFail);
};
openDb(self);
}
new sqliteImageStore();
})();