1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| require("events").EventEmitter.defaultMaxListeners = 0; const axios = require("axios"); const FormData = require("form-data"); const fs = require("fs"); const batch = 500; const startIndex = 0; const OLD_SERVER = { host: "172.0.0.1", port: 80, }; const NEW_SERVER = { host: "172.0.0.1", port: 80, };
console.log("loading filemeta..."); const rawFileMeta = fs.readFileSync("filemeta.json"); const fileMeta = JSON.parse(rawFileMeta)["RECORDS"]; console.log("load finish");
axios.interceptors.response.use(undefined, (err) => { var config = err.config; if (!config || !config.retry) return Promise.reject(err);
config.__retryCount = config.__retryCount || 0;
if (config.__retryCount >= config.retry) { return Promise.reject(err); }
config.__retryCount += 1;
var backoff = new Promise((resolve) => { setTimeout(() => resolve(), config.retryDelay || 1); });
return backoff.then(() => { return axios(config); }); });
function isFile(res) { if (res.data.headers["transfer-encoding"] === "chunked") return false; else return true; }
async function move(oldServer, newServer, path) { const res = await axios .get(`http://${oldServer.host}:${oldServer.port}${encodeURI(path)}`, { retry: 10, retryDelay: 1000, timeout: 10000, responseType: "stream", }) .catch(() => { console.log("download fail"); fs.appendFileSync("errorfile.list", `${path}\n`); }); if (res && !isFile(res)) { return; } const formData = new FormData(); formData.append("file", res.data); await axios .post( `http://${newServer.host}:${newServer.port}${encodeURI(path)}`, formData, { headers: formData.getHeaders(), retry: 0, retryDelay: 500, timeout: 10000, } ) .then() .catch((err) => { console.log("upload fail"); fs.appendFileSync("errorfile.list", `${path}\n`); }); }
async function start(startIndex) { let i = startIndex; for (; i < fileMeta.length; ) { const qlist = []; let j = 0; for (; j < batch; j++) { const q = move( OLD_SERVER, NEW_SERVER, `${fileMeta[i].directory}/${fileMeta[i].name}` ); qlist.push(q); if (i < fileMeta.length) i++; } await Promise.all(qlist).catch((err) => { console.log(err); console.log(`error range: ${i - batch}-${i}`); }); console.log(`${i}/${fileMeta.length}`); } }
start(startIndex);
|