例子:
点击查看代码
/** 坚果云 */
let Jgy = JianGuoYunHelper();
// 初始化
Jgy.init("username", "password", "坚果云测试_数据备份");
// 文件名
Jgy.setThisFileName("测试文件");
// 上传数据 (覆盖)
let putRes = Jgy.put("测试数据 测试数据 测试数据");
// 获取数据
let remoteData = Jgy.get();
点击查看代码
/**
* 坚果云文本文件 上传/下载
*/
function JianGuoYunHelper() {
/**
* 坚果云SDK
*/
function JgySDK() {}
/**
* 初始化
* @param {*} username
* @param {*} password
* @param {*} folderName 操作的文件夹名称
*/
JgySDK.prototype.init = function (username, password, folderName) {
this.folderName = folderName;
this.jgyHost = "http://dav.jianguoyun.com/dav/" + this.folderName + "/";
this.key = this.getBase64(username + ":" + password);
this.header = {
Authorization: "Basic " + this.key,
"Content-Type": "text/plain;charset=UTF-8",
Connection: "Keep-Alive",
"Accept-Encoding": "gzip",
"User-Agent": "okhttp/3.12.1",
};
this.fileName = "";
this.createFolder();
};
/**
* 设置 操作的fileName, 之后的方法不再需要传 fileName
* @param {string} fileName
* @param {string?} fileExtension 可空, 默认 "txt"
*/
JgySDK.prototype.setThisFileName = function (fileName, fileExtension) {
if (fileExtension == void 0) {
fileExtension = "txt";
}
this.fileName = fileName + "." + fileExtension;
};
/**
* 读取文件夹的目录
* @returns {string[]}
*/
JgySDK.prototype.getFolderCatalog = function () {
let httpRes = http.request(this.jgyHost, {
method: "PROPFIND",
headers: this.header,
});
let resArr = [];
let xmlData = httpRes.body.string();
if (xmlData) {
let dataArr = xmlData.match(/<d:displayname>(.*?)<\/d:displayname>/g);
for (let item of dataArr) {
item = item.replace("<d:displayname>", "").replace("</d:displayname>", "");
if (item != this.folderName) {
resArr.push(item);
}
}
}
return resArr;
};
/**
* 创建文件夹
*
* @private 私有方法
* @returns {boolean}
*/
JgySDK.prototype.createFolder = function () {
let httpRes = http.request(this.jgyHost, {
method: "MKCOL",
headers: this.header,
});
return httpRes.statusCode == 201;
};
/**
* 获取thisFileName
* @private 私有方法
* @param {string} fileName
* @returns {string}
*/
JgySDK.prototype.getThisFileName = function (fileName) {
if (fileName == void 0) {
if (this.fileName != "") {
fileName = this.fileName;
} else {
throw "当前必须传fileName, 调用setThisFileName后,才可以不传fileName";
}
}
return fileName;
};
/**
* 删除一个文件
* @param {string?} fileName 可空, 需要提前调用 setThisFileName
*/
JgySDK.prototype.delete = function (fileName) {
let res = { res: false, msg: "删除失败" };
try {
fileName = this.getThisFileName(fileName);
let fileArr = this.getFolderCatalog();
if (fileArr.indexOf(fileName) > -1) {
http.request(this.jgyHost + fileName, {
method: "DELETE",
headers: this.header,
});
fileArr = this.getFolderCatalog();
if (fileArr.indexOf(fileName) < 0) {
res.res = true;
res.msg = "删除成功";
} else {
res.msg = "删除失败,文件依然在目录中";
}
} else {
res.res = true;
res.res = "文件不存在,无需删除";
}
} catch (error) {
res.msg = error;
}
return res;
};
/**
* 获取一个文件内容
* @param {string?} fileName 可空, 需要提前调用 setThisFileName
*/
JgySDK.prototype.get = function (fileName) {
let res = "";
try {
fileName = this.getThisFileName(fileName);
let httpRes = http.get(this.jgyHost + fileName, {
headers: this.header,
});
//log(httpRes);
if (httpRes.statusCode == 404) {
let strRes = httpRes.body.string();
if (strRes.indexOf("doesn't exist") > -1) {
let errorMsg = "无备份文件,文件名:" + fileName;
toast(errorMsg);
console.error(errorMsg);
} else {
throw strRes;
}
} else if (httpRes.statusCode == 200) {
res = httpRes.body.string();
} else {
throw httpRes;
}
} catch (error) {
console.error("JgySDK get error: " + error);
}
return res;
};
/**
* 上传一个文件
* @param {string} path
* @param {string?} fileName 可空, 需要提前调用 setThisFileName
*/
JgySDK.prototype.put = function (data, fileName) {
let res = { res: false, msg: "上传失败" };
try {
fileName = this.getThisFileName(fileName);
let httpRes = http.request(this.jgyHost + fileName, {
method: "PUT",
headers: this.header,
body: data,
});
let newdata = this.get(fileName);
if (newdata == data) {
res.res = true;
res.msg = "本地数据 推送到 坚果云 成功";
} else {
res.msg = "数据推送失败,推送后网络数据与本次数据不同,result:" + httpRes.body.string();
}
} catch (error) {
res.msg = error;
}
return res;
};
/**
* 获取base64 结果
* @private 私有方法
* @param {string} str
* @returns {string}
*/
JgySDK.prototype.getBase64 = function (str) {
return java.lang.String(android.util.Base64.encode(java.lang.String(str).getBytes(), 2));
};
return new JgySDK();
}