Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
图片缓存支持。
Browse files Browse the repository at this point in the history
  • Loading branch information
MerrickZ committed Jan 14, 2014
1 parent c9fe976 commit cb279cf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Binary file added dist/one-0.1.9.bar
Binary file not shown.
58 changes: 58 additions & 0 deletions src/www/js/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
blackberry.io.sandbox = false;
var cache = {
get: function(url, strdate, callback) {
/*
* url : 图片网址
* strdate : 当前日期
* callback(string) :回调,返回本地地址或者网址。
*/
if (this.isExisted(strdate)){
callback(this.buildfileurl(strdate));
return;
}
/*
* 缓存中不存在,开始下载
*/
console.log("downloading " + url);
try {
blackberry.io.filetransfer.download(
url,
this.buildurl(strdate),
function(result) {
console.log("file downloaded,fullPath: " + result.fullPath);
callback("file://" + result.fullPath);
},
function(result) {
console.log("Download failed");
console.log("Error code: " + result.code);
console.log("HTTP status: " + result.http_status);
console.log("Source: " + result.source);
console.log("Target: " + result.target);
callback(url);
});
}
catch (e) {
console.log(e);
callback(url);
}
},
isExisted: function(strdate) {
/*
* 确定是否已缓存
*/
var url = this.buildfileurl(strdate);
console.log("Checking: " + url);
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send(null);
console.log(http.status);
if (http.status == 0) return false;
return true;
},
buildurl: function(str) {
return blackberry.io.home + "/" + str + ".jpg";
},
buildfileurl: function(str) {
return "file://" + this.buildurl(str);
}
};

0 comments on commit cb279cf

Please sign in to comment.