321 lines
11 KiB
JavaScript
321 lines
11 KiB
JavaScript
const axios = require('axios');
|
||
const fs = require('fs');
|
||
const crypto = require('crypto');
|
||
const FormData = require('form-data');
|
||
const request = require("request")
|
||
const https = require('https');
|
||
const size = 4 * 1024 * 1024
|
||
let promistlist = []
|
||
let daxiao
|
||
function getsize(url) {
|
||
return new Promise((res, err) => {
|
||
fs.stat("./a.exe", {}, (err, stats) => {
|
||
|
||
if (!err) {
|
||
daxiao = stats.size
|
||
let fenpian = Math.ceil(stats.size / size)
|
||
res({
|
||
size: daxiao,
|
||
num: fenpian
|
||
})
|
||
} else {
|
||
err("文件路径错误或文件不存在")
|
||
}
|
||
|
||
})
|
||
})
|
||
}
|
||
|
||
function getfile({ i, url }) {
|
||
|
||
return new Promise((res) => {
|
||
let data = '';
|
||
let start = i * size
|
||
let end = (i + 1) * size - 1
|
||
const stream = fs.createReadStream(url, {
|
||
start,
|
||
end
|
||
})
|
||
stream.on('data', (chunk) => {
|
||
// console.log(`接收到 ${chunk.length} 个字节的数据`);
|
||
if (!data) {
|
||
data = chunk
|
||
} else {
|
||
data = Buffer.concat([data, chunk])
|
||
}
|
||
});
|
||
stream.on('end', () => {
|
||
res(data)
|
||
});
|
||
})
|
||
|
||
}
|
||
|
||
function upgetfile({ i, url }) {
|
||
i = parseInt(i)
|
||
return new Promise((res) => {
|
||
let data = '';
|
||
const stream = fs.createReadStream(url, {
|
||
start: i * size,
|
||
end: (i + 1) * size - 1
|
||
})
|
||
// res(stream)
|
||
stream.on('data', (chunk) => {
|
||
// console.log(`接收到 ${chunk.length} 个字节的数据`);
|
||
|
||
if (!data) {
|
||
data = chunk
|
||
} else {
|
||
data = Buffer.concat([data, chunk])
|
||
}
|
||
});
|
||
stream.on('end', () => {
|
||
res(data)
|
||
});
|
||
})
|
||
|
||
}
|
||
|
||
async function getmd5list(url, info) {
|
||
promistlist = []
|
||
// fs.createReadStream
|
||
for (let i = 0; i < info.num; i++) {
|
||
let data = await getfile({ url, i })
|
||
const hash = crypto.createHash('md5');
|
||
hash.update(data);
|
||
const md5 = hash.digest('hex');
|
||
console.log(md5);
|
||
promistlist.push(md5)
|
||
}
|
||
return promistlist
|
||
}
|
||
|
||
async function precreate(info, url) {
|
||
let block_list = await getmd5list(url, info)
|
||
block_list = JSON.stringify(block_list)
|
||
let data = {
|
||
path: encodeURI('/app/服务器备份/baidu.exe'),
|
||
size: info.size,
|
||
isdir: 0,
|
||
autoinit: 1,
|
||
block_list,
|
||
rtype: 1,
|
||
|
||
}
|
||
let str = ""
|
||
for (let i in data) {
|
||
str += `${i}=${data[i]}&`
|
||
}
|
||
axios.post('https://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ', str).then((res) => {
|
||
upload({
|
||
url,
|
||
info,
|
||
uploadid: res.data.uploadid,
|
||
pian: res.data.block_list
|
||
})
|
||
})
|
||
}
|
||
|
||
function uploadfile(i,data,uploadid){
|
||
return new Promise((r)=>{
|
||
console.log(data.length)
|
||
let urls = '/rest/2.0/pcs/superfile2?'
|
||
let params = {
|
||
access_token: '121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ',
|
||
method: 'upload',
|
||
type: "tmpfile",
|
||
path: encodeURI('/app/服务器备份/baidu.exe'),
|
||
uploadid,
|
||
partseq: i
|
||
}
|
||
for (let j in params) {
|
||
urls += j + '=' + params[j] + '&'
|
||
}
|
||
const options = {
|
||
hostname: 'd.pcs.baidu.com',
|
||
port: 443,
|
||
path: urls,
|
||
method: 'PUT',
|
||
headers: {
|
||
'content-type':'multipart/form-data',
|
||
'Content-Length': data.length
|
||
}
|
||
};
|
||
|
||
const req = https.request(options, (res) => {
|
||
console.log('状态码:', res.statusCode);
|
||
console.log('请求头:', res.headers);
|
||
|
||
res.on('data', (d) => {
|
||
process.stdout.write(d);
|
||
});
|
||
r(1)
|
||
});
|
||
|
||
req.on('error', (e) => {
|
||
console.error(e);
|
||
});
|
||
req.write(data);
|
||
req.end();
|
||
})
|
||
|
||
}
|
||
|
||
async function upload({ url, info, uploadid, pian }) {
|
||
for (let i of pian) {
|
||
let data = await upgetfile({ url, i })
|
||
await uploadfile(i,data,uploadid)
|
||
// var formData = new FormData();
|
||
// formData.append('file', data);
|
||
// console.log(formData.getBuffer())
|
||
// headers['content-length'] = length;
|
||
// const from = new FormData();
|
||
// // const blob = Buffer.from(data);
|
||
// from.append('file', data);
|
||
// axios.post('https://d.pcs.baidu.com/rest/2.0/pcs/superfile2', {file: data}, {
|
||
// headers: {
|
||
// 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary' + new Date().getTime()
|
||
// },
|
||
// params: {
|
||
// access_token: '121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ',
|
||
// method: 'upload',
|
||
// type: "tmpfile",
|
||
// path: encodeURI('/app/服务器备份/baidu.exe'),
|
||
// uploadid,
|
||
// partseq: i
|
||
// }
|
||
// }).then((res) => {
|
||
// console.log(res)
|
||
// }).catch(err => {
|
||
// console.log(err)
|
||
// })
|
||
// var headers = formData.getHeaders();
|
||
// formData.getLength(async function (err, length) {
|
||
// if (err) {
|
||
// return;
|
||
// }
|
||
// //设置长度,important!!!
|
||
// headers['content-length'] = length;
|
||
// // let $upload = axios.create({ withCredentials: false });
|
||
// // await $upload.post('https://d.pcs.baidu.com/rest/2.0/pcs/superfile2', formData, {
|
||
// // headers,
|
||
// // params: {
|
||
// // access_token: '121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ',
|
||
// // method: 'upload',
|
||
// // type: "tmpfile",
|
||
// // path: encodeURI('/app/服务器备份/baidu.exe'),
|
||
// // uploadid,
|
||
// // partseq: i
|
||
// // }
|
||
// // }).then((res) => {
|
||
// // console.log(res)
|
||
// // }).catch(err => {
|
||
// // console.log(err)
|
||
// // })
|
||
// // await axios.post(data.url, formData, { headers }).then(res => {
|
||
// // console.log("上传成功", res.data);
|
||
// // }).catch(res => {
|
||
// // console.log(res.data);
|
||
// // })
|
||
// let url = 'https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?'
|
||
// let params = {
|
||
// access_token: '121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ',
|
||
// method: 'upload',
|
||
// type: "tmpfile",
|
||
// path: encodeURI('/app/服务器备份/baidu.exe'),
|
||
// uploadid,
|
||
// partseq: i
|
||
// }
|
||
// for (let j in params) {
|
||
// url += j + '=' + params[j] + '&'
|
||
// }
|
||
// request.post({
|
||
// url, formData, function(err, httpResponse, body) {
|
||
// if (err) {
|
||
// return console.error('upload failed:', err);
|
||
// }
|
||
// console.log('Upload successful! Server responded with:', body);
|
||
// }
|
||
// })
|
||
|
||
// })
|
||
// let urls = 'https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?'
|
||
// let params = {
|
||
// access_token: '121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ',
|
||
// method: 'upload',
|
||
// type: "tmpfile",
|
||
// path: encodeURI('/app/服务器备份/baidu.exe'),
|
||
// uploadid,
|
||
// partseq: i
|
||
// }
|
||
// for (let j in params) {
|
||
// urls += j + '=' + params[j] + '&'
|
||
// }
|
||
// // console.log(urls)
|
||
// request.post({
|
||
// url: urls, form:{file: Buffer.from(data)}, function(err, httpResponse, body) {
|
||
// console.log(111)
|
||
// if (err) {
|
||
// return console.error('upload failed:', err);
|
||
// }
|
||
// console.log('Upload successful! Server responded with:', body);
|
||
// }
|
||
// })
|
||
|
||
// formData.submit(`https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&access_token=121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ&type=tmpfile&path=${encodeURI('/app/服务器备份/baidu.exe')}&uploadid=${uploadid}&partseq=${i}`, function(err, res) {
|
||
// console.log(res.statusCode);
|
||
// });
|
||
|
||
}
|
||
// https://pan.baidu.com/rest/2.0/xpan/file?method=create
|
||
let data = {
|
||
path:encodeURI('/app/服务器备份/baidu.exe'),
|
||
size: daxiao,
|
||
isdir:0,
|
||
rtype: 1,
|
||
uploadid,
|
||
block_list:JSON.stringify(promistlist)
|
||
|
||
}
|
||
let s = ''
|
||
for(let i in data){
|
||
s += i + "=" + data[i] + '&'
|
||
}
|
||
console.log(s)
|
||
axios.post('https://pan.baidu.com/rest/2.0/xpan/file?method=create',s,{
|
||
params:{
|
||
access_token: '121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ',
|
||
}
|
||
}).then((res)=>{
|
||
console.log(res)
|
||
})
|
||
}
|
||
|
||
|
||
async function start(url) {
|
||
let info;
|
||
try {
|
||
info = await getsize(url)
|
||
} catch (err) {
|
||
console.log(err)
|
||
}
|
||
console.log(info)
|
||
precreate(info, url)
|
||
}
|
||
module.exports = {
|
||
start
|
||
}
|
||
|
||
|
||
|
||
// 7406691ca4ebf37fdbb8bcccf3cfcfe8 code
|
||
// http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=LAEyKcje44EZbERqw0wC3XxyHm4ExqEf&redirect_uri=http://openapi.baidu.com/oauth/2.0/login_success&scope=basic,netdisk&display=tv&qrcode=1&force_login=1
|
||
// https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=7406691ca4ebf37fdbb8bcccf3cfcfe8&client_id=LAEyKcje44EZbERqw0wC3XxyHm4ExqEf&client_secret=kFAr3xS89UjBG5FTkaCzfOqljuFooYZD&redirect_uri=http://openapi.baidu.com/oauth/2.0/login_success
|
||
// {
|
||
// "expires_in": 2592000,
|
||
// "refresh_token": "122.2c2030994472bfdf99a1a5411524de63.YBabjPVrdcHdiGytZJETi1lAEDkm4gDyLOX0Xcw.2wqJeA",
|
||
// "access_token": "121.f22e108b18705600a0dc665d12cf71ae.Yngau4V98lC_hUvMGnljcSZurJnoohuPYavgk4n.HJDCoQ",
|
||
// "session_secret": "",
|
||
// "session_key": "",
|
||
// "scope": "basic netdisk"
|
||
// } 30天
|