Skip to content

Instantly share code, notes, and snippets.

@yume-chan
Last active May 25, 2016 14:33
Show Gist options
  • Save yume-chan/3f62b1ed906deb037929cf2dbca74515 to your computer and use it in GitHub Desktop.
Save yume-chan/3f62b1ed906deb037929cf2dbca74515 to your computer and use it in GitHub Desktop.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};
Promise.delay = function (time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
};
function downloadAsync(mode, infos, cancelToken) {
return __awaiter(this, void 0, void 0, function* () {
if (cancelToken.isCanceled)
return;
var delay = parseInt(localStorage.getItem("rpc_delay")) || 300;
var folders = [], files = [];
for (let item of infos) {
if (item.isdir)
folders.push(item);
else
files.push(item);
}
var completedCount = 0;
while (folders.length > 0) {
completedCount++;
showToast("正在获取文件列表... " + completedCount + "/" + (completedCount + folders.length - 1), "MODE_SUCCESS");
var path = folders.pop().path;
try {
var list = yield $.getJSON("/share/list", {
"dir": path,
"bdstoken": yunData.MYBDSTOKEN,
"uk": yunData.SHARE_UK,
"shareid": yunData.SHARE_ID,
"channel": "chunlei",
"clienttype": 0,
"web": 1
});
if (cancelToken.isCanceled)
return;
if (list.errno != 0) {
showToast("未知错误", "MODE_FAILURE");
console.log(list);
}
for (let item of list.list) {
if (item.isdir)
folders.push(item);
else
files.push(item);
}
}
catch (ex) {
if (cancelToken.isCanceled)
return;
showToast("网络请求失败", "MODE_FAILURE");
console.log(ex);
}
yield Promise.delay(delay);
}
if (files.length == 0) {
showToast("一个文件都没有哦", "MODE_CAUTION");
return;
}
// 设置要请求文件的POST数据
var data = {
"encrypt": "0",
"product": "share",
"uk": yunData.SHARE_UK,
"primaryid": yunData.SHARE_ID,
"fid_list": JSON.stringify(files.map(item => item.fs_id))
};
if (!yunData.SHARE_PUBLIC)
data["extra"] = JSON.stringify({ sekey: cookies });
try {
var link = yield $.post("/api/sharedownload?" + $.param({
"timestamp": yunData.TIMESTAMP,
"sign": yunData.SIGN,
"bdstoken": yunData.MYBDSTOKEN,
"app_id": yunData.FILEINFO[0].app_id,
"channel": "chunlei",
"clienttype": 0,
"web": 1
}), data, null, "json");
if (cancelToken.isCanceled)
return;
while (link.errno == -20) {
var captcha = yield $.getJSON("/api/getcaptcha", {
"prod": "share",
"bdstoken": yunData.MYBDSTOKEN,
"app_id": yunData.FILEINFO[0].app_id,
"channel": "chunlei",
"clienttype": 0,
"web": 1
});
if (cancelToken.isCanceled)
return;
if (captcha.errno != 0) {
showToast("未知错误", "MODE_FAILURE");
console.log(captcha);
return;
}
showToast("请输入验证码以继续下载", "MODE_CAUTION");
yield alertDialog(captcha, data);
if (cancelToken.isCanceled)
return;
}
if (link.errno != 0) {
showToast("未知错误", "MODE_FAILURE");
console.log(list);
}
var file_list = [];
if (yunData.SHAREPAGETYPE == "single_file_page") {
var single = link.list[0];
// For single file, save to filename.
file_list.push({ name: yunData.FILENAME, link: single.dlink });
}
else {
// For multiple files, save relates to share base folder.
for (let item of link.list) {
file_list.push({ name: item.path.substr(pathPrefixLength), link: item.dlink });
}
}
if (mode == "TXT") {
// Show download dialog when we got the first download link.
CORE.dataBox.show();
CORE.dataBox.fillData(file_list);
}
else {
var paths = CORE.parseAuth(RPC_PATH);
var rpc_list = CORE.aria2Data(file_list, paths[0], paths[2]);
generateParameter(rpc_list);
}
}
catch (ex) {
if (cancelToken.isCanceled)
return;
showToast("网络请求失败", "MODE_FAILURE");
console.log(ex);
}
return;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment