将url下载到本地
2021-05-28 00:00
标签:als function file url geturl manage rsync rar exports 将url下载到本地 标签:als function file url geturl manage rsync rar exports 原文地址:https://www.cnblogs.com/caoke/p/14791021.htmlconst axios=require(‘axios‘)
const fs = require(‘fs-extra‘);
const https=require(‘https‘)
const path=require(‘path‘)
function resolve (dir) {
return path.join(__dirname, ‘..‘, dir)
}
/**将url下载到本地
* */
class LoadUrlManage{
constructor (rArr){
this.rArr=[
[/\?.*$/,‘‘]
]
if(Array.isArray(rArr)){
this.rArr=this.rArr.concat(rArr)
}
this.ignoreSSL = axios.create({
responseType: "stream",
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
}
async getUrl(url){
const res=await this.ignoreSSL({
url:url,
method: "GET",
})
if(res.status===200){
return res;
}
throw res;
}
async loadUrl(url){
let filepath=url;
this.rArr.forEach(function (arr) {
filepath=filepath.replace(arr[0],arr[1])
})
fs.ensureDirSync(path.dirname(filepath))
const res=await this.getUrl(url)
res.data.pipe(fs.createWriteStream(filepath))
return filepath;
}
}
module.exports=LoadUrlManage;