Skip to content

Commit

Permalink
进行未登陆状态错误的检测
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Aug 4, 2020
1 parent 4e504a9 commit 72e02f0
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 121 deletions.
8 changes: 6 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import process from "process";
import { fileURLToPath } from "url";
import { argsobj } from "./argsobj.js";
import { start } from "./index.js";
const { input, dest } = argsobj;
const { input, dest, concurrent } = argsobj;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const helppath = path.join(__dirname, "../help.txt");
const helptxt = String(fs.readFileSync(helppath));
if (input && dest) {
console.log({ input, dest });
console.log({
input,
dest,
concurrent,
});
start(input, dest);
} else {
console.error(helptxt);
Expand Down
16 changes: 7 additions & 9 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import process from "process";
import { fileURLToPath } from "url";
import { argsobj } from "./argsobj.js";
import { start } from "./index.js";
const { input, dest,concurrent } = argsobj;
const { input, dest, concurrent } = argsobj;

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const helppath = path.join(__dirname, "../help.txt");
const helptxt = String(fs.readFileSync(helppath));
if (input && dest) {
console.log({ input, dest /*, reverse */



,concurrent


});
console.log({
input,
dest /*, reverse */,

concurrent,
});
start(input, dest /*, reverse*/);
} else {
console.error(helptxt);
Expand Down
5 changes: 4 additions & 1 deletion lib/execbaidupcs.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// <reference types="node" />
export default function (localfile: string, desdir: string): import("child_process").PromiseWithChild<{
export default function (
localfile: string,
desdir: string
): import("child_process").PromiseWithChild<{
stdout: string;
stderr: string;
}>;
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export declare const cmd: string;
declare const start: (input: string, dest: string) => Promise<void>;
declare function start(input: string, dest: string): Promise<void>;
export { start };
27 changes: 14 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ process.on("unhandledRejection", (e) => {
});
let 总数 = 0;
let 完成数 = 0;
const start = async (input, dest) => {
async function start(input, dest) {
const filedatas = await findfile(path.resolve(input));
console.log("找到文件" + filedatas.length + "个");
console.log(JSON.stringify(filedatas, null, 4));
const 输入目录名 = path.basename(input);
const filesizes = await Promise.all(
filedatas.map(async (file) => {
const stat = await fs.promises.stat(file);
Expand All @@ -26,21 +25,23 @@ const start = async (input, dest) => {
return filesizes[index];
});
总数 = filelist.length;
const destlist = filelist.map((file) => {
const destination = posix.dirname(
posix
.resolve(dest, 输入目录名, path.relative(input, file))
.replace(/\\/g, "/")
);
return destination;
});
await Promise.all(
filelist.map(async (file, index) => {
const destination = destlist[index];
const destination = resolvefiledestination(file, input, dest);
await upload(file, destination);
完成数++;
console.log("完成进度:", `${完成数} / ${总数}`);
const 进度 = "完成进度:" + `${完成数} / ${总数}`;
console.log(进度);
})
);
};
}
export { start };
function resolvefiledestination(file, input, dest) {
const inputbase = path.basename(input);
const destination = posix.dirname(
posix
.resolve(dest, inputbase, path.relative(input, file))
.replace(/\\/g, "/")
);
return destination;
}
40 changes: 19 additions & 21 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ let 完成数 = 0;
* */

async function start (input: string, dest: string
async function start(
input: string,
dest: string


/*, reverse = false*/


) => {
/*, reverse = false*/
) {
const filedatas = await findfile(path.resolve(input));
// 总数 = filedatas.length;
console.log("找到文件" + filedatas.length + "个");
console.log(JSON.stringify(filedatas, null, 4));

/* 要把文件大小为0的文件排除,否则上传失败 */
const filesizes = await Promise.all(
filedatas.map(async (file) => {
Expand All @@ -47,7 +46,7 @@ let 完成数 = 0;
});
// reverse ? filedatas.reverse() : filedatas;
总数 = filelist.length;
/* const destlist = filelist.map((file) => {
/* const destlist = filelist.map((file) => {
const destination = posix.dirname(
posix
.resolve(dest, inputbase, path.relative(input, file))
Expand All @@ -65,8 +64,8 @@ let 完成数 = 0;
// 给上传目标文件夹添加了输入文件夹的名字
/*
*/
const destination = resolvefiledestination(file,input,dest)
//destlist[index];
const destination = resolvefiledestination(file, input, dest);
//destlist[index];

/*posix.dirname(
posix
Expand All @@ -75,20 +74,19 @@ let 完成数 = 0;
);*/
await upload(file, destination);
完成数++;
const 进度="完成进度:"+ `${完成数} / ${总数}`
const 进度 = "完成进度:" + `${完成数} / ${总数}`;
console.log(进度);
}
)
);
};
}
export { start };
function resolvefiledestination(file: string,input: string,dest: string){
const inputbase = path.basename(input);
const destination = posix.dirname(
posix
.resolve(dest, inputbase, path.relative(input, file))
.replace(/\\/g, "/")
);
return destination;

function resolvefiledestination(file: string, input: string, dest: string) {
const inputbase = path.basename(input);
const destination = posix.dirname(
posix
.resolve(dest, inputbase, path.relative(input, file))
.replace(/\\/g, "/")
);
return destination;
}
5 changes: 4 additions & 1 deletion lib/uploadfile.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export declare function upload(file: string, destination: string): Promise<void>;
export declare function upload(
file: string,
destination: string
): Promise<void>;
46 changes: 32 additions & 14 deletions lib/uploadfile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const retrymsg = [
"打开上传未完成数据库错误:",
const fatalerror = [
"遇到错误, 远端服务器返回错误, 代码: 31045, 消息: 操作失败,",
];
const directfailure = [
"以下文件上传失败:",
"全部上传完毕, 总大小: 0B",
"ms, 总大小: 0B",
"打开上传未完成数据库错误:",
];
const retrymsg = [
"网络错误, http 响应错误,",
`遇到错误, 远端服务器返回错误, 代码: 31352, 消息: commit superfile2 failed`,
`网络错误, Post`,
`json 数据解析失败,`,
"遇到错误, 远端服务器返回错误, 代码: 31352, 消息: commit superfile2 failed",
"网络错误, Post",
"json 数据解析失败,",
"获取文件列表错误, 获取目录下的文件列表",
"网络错误, Get",
`上传文件错误: 上传状态过期, 请重新上传`,
`上传文件失败, 分片上传—合并分片文件`,
"全部上传完毕, 总大小: 0B",
"上传文件错误: 上传状态过期, 请重新上传",
"上传文件失败, 分片上传—合并分片文件",
];
const successmsg = [
", 已存在, 跳过...",
Expand Down Expand Up @@ -46,20 +52,32 @@ export async function upload(file, destination) {
stderr,
};
console.log(JSON.stringify(记录日志, null, 4));
if (successmsg.some((m) => stdout.includes(m))) {
if (fatalerror.some((m) => stdout.includes(m))) {
throw new Error(
"exec command failure! baidupcs-go:" + "\n" + stdout + "\n" + stderr
);
} else if (directfailure.some((m) => stdout.includes(m))) {
console.warn(stdout, stderr);
console.warn("上传失败,5秒后重试:" + file);
return new Promise((res) => {
setTimeout(() => {
res(upload(file, destination));
}, 5000);
});
} else if (successmsg.some((m) => stdout.includes(m))) {
console.log("文件上传成功", file);
return;
}
if (retrymsg.some((msg) => stdout.includes(msg))) {
} else if (retrymsg.some((msg) => stdout.includes(msg))) {
console.warn(stdout, stderr);
console.warn("上传失败,5秒后重试:" + file);
return new Promise((res) => {
setTimeout(() => {
res(upload(file, destination));
}, 5000);
});
} else {
throw new Error(
"exec command failure! baidupcs-go:" + "\n" + stdout + "\n" + stderr
);
}
throw new Error(
"command failure! baidupcs-go:" + JSON.stringify({ stdout, stderr })
);
}
81 changes: 24 additions & 57 deletions lib/uploadfile.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
const fatalerror=["遇到错误, 远端服务器返回错误, 代码: 31045, 消息: 操作失败, 可能百度帐号登录状态 过期, 请尝试重新登录, 消息: user not exists"]


const directfailure
=[ "以下文件上传失败:",



"全部上传完毕, 总大小: 0B","ms, 总大小: 0B", "打开上传未完成数据库错误:",]
const fatalerror = [
"遇到错误, 远端服务器返回错误, 代码: 31045, 消息: 操作失败,",
];

const directfailure = [
"以下文件上传失败:",

"全部上传完毕, 总大小: 0B",
"ms, 总大小: 0B",
"打开上传未完成数据库错误:",
];

const retrymsg = [


"网络错误, http 响应错误,",
"遇到错误, 远端服务器返回错误, 代码: 31352, 消息: commit superfile2 failed",
"网络错误, Post",
Expand All @@ -21,7 +19,6 @@ const retrymsg = [
"网络错误, Get",
"上传文件错误: 上传状态过期, 请重新上传",
"上传文件失败, 分片上传—合并分片文件",

];
const successmsg = [
", 已存在, 跳过...",
Expand Down Expand Up @@ -52,7 +49,7 @@ export async function upload(file: string, destination: string): Promise<void> {
console.log("文件上传成功", file);
return;
} else {
//如果。找不到 baidupcs-go的可执行文件,则。会在这里报错
//如果。找不到 baidupcs-go的可执行文件,则。会在这里报错
throw error;
}
}
Expand All @@ -65,66 +62,36 @@ export async function upload(file: string, destination: string): Promise<void> {
stderr,
};
console.log(JSON.stringify(记录日志, null, 4));








//未登录的致命错误要失败

//未登录的致命错误要失败
/* 判断是否上传成功与失败 */

if(

fatalerror.some((m) => stdout.includes(m))

){




throw new Error(
"exec command failure! baidupcs-go:" + "\n"+ stdout+"\n"+ stderr

);
}
else if(directfailure.some((m) => stdout.includes(m))){

console.warn(stdout, stderr);
if (fatalerror.some((m) => stdout.includes(m))) {
throw new Error(
"exec command failure! baidupcs-go:" + "\n" + stdout + "\n" + stderr
);
} else if (directfailure.some((m) => stdout.includes(m))) {
console.warn(stdout, stderr);
console.warn("上传失败,5秒后重试:" + file);
return new Promise((res) => {
setTimeout(() => {
res(upload(file, destination));
}, 5000);
});



}
else
if (successmsg.some((m) => stdout.includes(m))) {
} else if (successmsg.some((m) => stdout.includes(m))) {
console.log("文件上传成功", file);
return;
}else

if (retrymsg.some((msg) => stdout.includes(msg))) {
} else if (retrymsg.some((msg) => stdout.includes(msg))) {
console.warn(stdout, stderr);
console.warn("上传失败,5秒后重试:" + file);
return new Promise((res) => {
setTimeout(() => {
res(upload(file, destination));
}, 5000);
});
} else {
throw new Error(
"exec command failure! baidupcs-go:" + "\n" + stdout + "\n" + stderr
);
}
else{
throw new Error(
"exec command failure! baidupcs-go:" + "\n"+ stdout+"\n"+ stderr

);
}



}
Loading

0 comments on commit 72e02f0

Please sign in to comment.