mirror of
https://github.com/jellyfin/jellyfin-web.git
synced 2024-11-19 03:48:18 -07:00
1 line
3.9 KiB
JavaScript
1 line
3.9 KiB
JavaScript
define(["cryptojs-md5"],function(){"use strict";function loadImage(elem,url){return"IMG"!==elem.tagName?(elem.style.backgroundImage="url('"+url+"')",Promise.resolve(elem)):(elem.setAttribute("src",url),Promise.resolve(elem))}function createDir(rootDirEntry,folders,callback,errorCallback){"."!==folders[0]&&""!==folders[0]||(folders=folders.slice(1)),rootDirEntry.getDirectory(folders[0],{create:!0},function(dirEntry){folders.length>1?createDir(dirEntry,folders.slice(1),callback,errorCallback):callback(dirEntry)},errorCallback)}function toArray(list){return Array.prototype.slice.call(list||[],0)}function cleanCache(){var dirReader=imageCacheDirectoryEntry.createReader(),entries=[],onReadFail=function(){console.log("dirReader.readEntries failed")},readEntries=function(){dirReader.readEntries(function(results){results.length?(entries=entries.concat(toArray(results)),readEntries()):entries.forEach(cleanFile)},onReadFail)};readEntries()}function cleanFile(fileEntry){fileEntry.isFile&&fileEntry.file(function(file){getLastModified(file,fileEntry).then(function(lastModifiedDate){var elapsed=(new Date).getTime()-lastModifiedDate,maxElapsed=3456e6;if(elapsed>=maxElapsed){var fullPath=fileEntry.fullPath;console.log("deleting file: "+fullPath),fileEntry.remove(function(){console.log("File deleted: "+fullPath)},function(){console.log("Failed to delete file: "+fullPath)})}})})}function getLastModified(file,fileEntry){var lastModifiedDate=file.lastModified||file.lastModifiedDate||file.modificationTime;return lastModifiedDate?(lastModifiedDate.getTime&&(lastModifiedDate=lastModifiedDate.getTime()),Promise.resolve(lastModifiedDate)):new Promise(function(resolve,reject){fileEntry.getMetadata(function(metadata){var lastModifiedDate=metadata.lastModified||metadata.lastModifiedDate||metadata.modificationTime;lastModifiedDate&&lastModifiedDate.getTime&&(lastModifiedDate=lastModifiedDate.getTime()),resolve(lastModifiedDate)})})}function getCacheKey(url){var index=url.indexOf("://");return index!==-1&&(url=url.substring(index+3),index=url.indexOf("/"),index!==-1&&(url=url.substring(index+1))),CryptoJS.MD5(url).toString()}function downloadToFile(url,dir,filename,callback,errorCallback){console.log("Downloading "+url);var xhr=new XMLHttpRequest;xhr.open("GET",url,!0),xhr.responseType="arraybuffer",xhr.onload=function(e){200===this.status?writeData(dir,filename,this.getResponseHeader("Content-Type"),this.response,callback,errorCallback):errorCallback()},xhr.send()}function writeData(dir,filename,fileType,data,callback,errorCallback){dir.getFile(filename,{create:!0},function(fileEntry){fileEntry.createWriter(function(fileWriter){fileWriter.onwriteend=function(e){callback(fileEntry)},fileWriter.onerror=errorCallback;var blob=new Blob([data],{type:fileType});fileWriter.write(blob)},errorCallback)},errorCallback)}function getImageUrl(originalUrl){return new Promise(function(resolve,reject){originalUrl.indexOf("tag=")!==-1&&(originalUrl+="&accept=webp");var key=getCacheKey(originalUrl),fileEntryCallback=function(fileEntry){resolve(fileEntry.toURL())},errorCallback=function(e){console.log("Imagestore error: "+e.name),reject()};if(!fileSystem||!imageCacheDirectoryEntry)return void errorCallback("");var path="/"+imageCacheFolder+"/"+key;fileSystem.root.getFile(path,{create:!1},fileEntryCallback,function(){downloadToFile(originalUrl,imageCacheDirectoryEntry,key,fileEntryCallback,errorCallback)})})}var imageCacheDirectoryEntry,requestedBytes=1572864e3,imageCacheFolder="images";navigator.webkitPersistentStorage.requestQuota(requestedBytes,function(grantedBytes){var requestMethod=window.webkitRequestFileSystem||window.requestFileSystem;requestMethod(PERSISTENT,grantedBytes,function(fs){fileSystem=fs,createDir(fileSystem.root,imageCacheFolder.split("/"),function(dirEntry){imageCacheDirectoryEntry=dirEntry,setTimeout(cleanCache,6e4)})})});var fileSystem;return{loadImage:function(elem,url){return getImageUrl(url).then(function(localUrl){return loadImage(elem,localUrl)},function(){return loadImage(elem,url)})}}}); |