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

119 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-05-15 08:46:20 -07:00
(function () {
function setImageIntoElement(elem, url) {
2015-06-27 20:29:50 -07:00
if (elem.tagName !== "IMG") {
2015-05-15 08:46:20 -07:00
elem.style.backgroundImage = "url('" + url + "')";
} else {
elem.setAttribute("src", url);
}
}
2015-06-14 21:17:12 -07:00
var fileSystem;
function getFileSystem() {
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
var deferred = DeferredBuilder.Deferred();
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
if (fileSystem) {
deferred.resolveWith(null, [fileSystem]);
} else {
requestFileSystem(PERSISTENT, 0, function (fs) {
fileSystem = fs;
deferred.resolveWith(null, [fileSystem]);
});
}
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
return deferred.promise();
2015-05-15 08:46:20 -07:00
}
2015-06-14 21:17:12 -07:00
function indexedDbBlobImageStore() {
2015-05-15 08:46:20 -07:00
var self = this;
2015-06-01 07:49:23 -07:00
function getCacheKey(url) {
2015-06-14 21:17:12 -07:00
2015-06-01 07:49:23 -07:00
// 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);
}
}
2015-06-14 21:17:12 -07:00
2015-06-01 07:49:23 -07:00
return CryptoJS.MD5(url).toString();
}
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
function normalizeReturnUrl(url) {
if ($.browser.safari) {
2015-06-19 10:22:54 -07:00
2015-06-19 15:01:47 -07:00
// Use the embedded server for iOS8, and also if we don't know the iOS version, just to be safe
2015-07-09 21:44:21 -07:00
//if (($.browser.iOSVersion || 0) < 9) {
2015-07-08 09:10:34 -07:00
return url.replace('file://', '');
2015-07-09 21:44:21 -07:00
//}
2015-06-14 21:17:12 -07:00
}
return url;
}
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
self.getImageUrl = function (originalUrl) {
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
if ($.browser.android && originalUrl.indexOf('tag=') != -1) {
originalUrl += "&format=webp";
}
2015-05-15 08:46:20 -07:00
var deferred = DeferredBuilder.Deferred();
2015-06-14 21:17:12 -07:00
var key = getCacheKey(originalUrl);
2015-05-15 08:46:20 -07:00
2015-09-11 19:18:09 -07:00
//Logger.log('getImageUrl:' + originalUrl);
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
getFileSystem().done(function (fileSystem) {
var path = fileSystem.root.toURL() + "/emby/cache/" + key;
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
resolveLocalFileSystemURL(path, function (fileEntry) {
var localUrl = normalizeReturnUrl(fileEntry.toURL());
2015-09-11 19:18:09 -07:00
//Logger.log('returning cached file: ' + localUrl);
2015-06-14 21:17:12 -07:00
deferred.resolveWith(null, [localUrl]);
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
}, function () {
2015-05-15 08:46:20 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('downloading: ' + originalUrl);
2015-06-14 21:17:12 -07:00
var ft = new FileTransfer();
ft.download(originalUrl, path, function (entry) {
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
var localUrl = normalizeReturnUrl(entry.toURL());
2015-05-15 08:46:20 -07:00
2015-06-26 20:27:38 -07:00
Logger.log(localUrl);
2015-06-14 21:17:12 -07:00
deferred.resolveWith(null, [localUrl]);
2015-05-15 08:46:20 -07:00
});
});
});
return deferred.promise();
};
self.setImageInto = function (elem, url) {
function onFail() {
setImageIntoElement(elem, url);
}
self.getImageUrl(url).done(function (localUrl) {
2015-06-14 21:17:12 -07:00
2015-05-15 08:46:20 -07:00
setImageIntoElement(elem, localUrl);
}).fail(onFail);
};
2015-06-14 21:17:12 -07:00
window.ImageStore = self;
2015-05-15 08:46:20 -07:00
}
2015-06-14 21:17:12 -07:00
new indexedDbBlobImageStore();
2015-05-15 08:46:20 -07:00
2015-06-14 21:17:12 -07:00
})();