Last active
April 13, 2020 03:30
-
-
Save huanggm/a5c145da930cef26108a93b02385600c to your computer and use it in GitHub Desktop.
获取百度云文件夹大小
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getCurrentSize() { | |
let dirCount = 0, fileCount = 0; | |
let path = location.href.match(/path=([^&]+)/); | |
path = decodeURIComponent(path[1]) | |
console.log('starting...') | |
const res = await getSize(path) | |
console.log(res, dirCount, fileCount) | |
async function getSize(path) { | |
const res = await getInfo(path) | |
const retNode = {size:0, list:{}} | |
for(var i = 0; i < res.list.length; i ++) { | |
const file = res.list[i] | |
if(file.isdir === 1) { | |
dirCount++ | |
const temp = await getSize(file.path) | |
retNode.size += temp.size | |
retNode.list[file.path] = temp | |
} else { | |
fileCount++ | |
retNode.size += file.size | |
} | |
} | |
retNode.newSize = transSize(retNode.size) | |
return retNode | |
} | |
async function getInfo(path) { | |
const params = '&bdstoken=c3a36b87ebe9c49fc63cc45ecf974635&logid=MTU4NTc0NjA3OTc1MTAuNDE4MTQ0ODA2MDk2Nzc3ODQ=&num=10000&order=size&desc=1&clienttype=0&showempty=0&web=1&page=1&channel=chunlei&web=1&app_id=250528' | |
const url = 'https://pan.baidu.com/api/list' | |
const allurl = url + '?dir=' + encodeURIComponent(path) + params | |
return jQuery.get(allurl) | |
} | |
function transSize(size) { | |
if(size >= 1024 * 1024 * 1024) { | |
return size / (1024 * 1024 * 1024) + 'GB' | |
} else if(size >= 1024 * 1024) { | |
return size / (1024 * 1024) + 'MB' | |
} else if(size >= 1024) { | |
return size / 1024 + 'KB' | |
} else { | |
return size + 'B' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment