遍历收集所有文件信息
var query_param = new Array();
var href_string = window.location.href.replace(/\?/g,"&").split("&");
for (var i=0; i<href_string.length; i++) {
var kv = href_string[i].split("=");
query_param[kv[0]] = kv[1];
}
var hash = {}; //哈希指纹
var file = {}; //文件路径
function ergodic(dir) {
$.ajax({
url: "//pan.baidu.com/api/list?page=1&num=50000&dir="+dir,
dataType: "json", async: false,
success: function(data) {
var list = data.list;
for (var x=0; x<list.length; x++) {
console.log(list[x].path);
if (list[x].isdir == 1) {
ergodic(encodeURIComponent(list[x].path));
} else {
if (!Array.isArray(hash[list[x].md5]))
hash[list[x].md5] = [];
hash[list[x].md5].push(list[x].path);
file[list[x].path] = {};
file[list[x].path]["md5"] = list[x].md5;
file[list[x].path]["size"] = list[x].size;
file[list[x].path]["mtime"] = list[x].local_mtime;
}
}
}
});
}
ergodic(query_param["path"]);
显示相同散列值的文件
var same = {};
for (let h in hash) {
if (hash[h].length>1)
same[h] = hash[h];
}
console.log(JSON.stringify(same));
统计目录占用空间大小
function dirbase(path) {
var slash = path.lastIndexOf("/");
return [path.substring(0,slash), path.substring(slash+1)];
}
var size = {};
for (let fn in file) {
fdir=fn; do { //累加父目录
fdir = dirbase(fdir)[0];
if (isNaN(size[fdir])) size[fdir]=0;
size[fdir] += file[fn].size;
} while (fdir != decodeURIComponent(query_param["path"]));
}
for (let m in size) {
size[m] = Math.round(size[m]/1048576) + " MB ";
//console.log(size[m].toString().padStart(10,' ')+m);
}
console.log(JSON.stringify(size, Object.keys(size).sort()));