💩 优化图片请求

This commit is contained in:
yeyang
2023-03-25 18:02:37 +08:00
parent b868b37829
commit d78cb84e23
2 changed files with 43 additions and 23 deletions

View File

@@ -51,31 +51,35 @@ export class NGA extends plugin {
}
}
let imgPaths = []
let imgList = []
if (type == '收益曲线') {
// 收益曲线
if (!this.incomeCurveObj[role.name]) return this.e.reply('暂时无该角色收益曲线~>_<')
if (!this.incomeCurveObj[role.name]) {
return this.e.reply('暂时无该角色收益曲线~>_<')
}
let urls = this.incomeCurveObj[role.name]
if (Array.isArray(urls)) {
urls.forEach((item, index) => imgPaths.push({
urls.forEach((item, index) => imgList.push({
url: item,
imgPath: `${this.incomeCurvePath}/${role.name}_${index + 1}.png`
}))
} else {
imgPaths.push({
imgList.push({
url: urls,
imgPath: `${this.incomeCurvePath}/${role.name}.png`
})
}
} else {
// 参考面板
imgPaths.push({
imgList.push({
url: `http://public.yenai.ren/Referenc/${role.name}.png`,
imgPath: `${this.referencePanelPath}/${role.name}.png`
})
}
for (const item of imgPaths) {
for (const item of imgList) {
// 检测图片并下载图片
if (!fs.existsSync(item.imgPath) || regRet[1]) {
this.initFolder(type)

View File

@@ -59,7 +59,9 @@ export default new class {
return res
} catch (err) {
logger.error(err)
throw Error(`Request Get Error${err.message.match(/reason:(.*)/i) || err.message}`)
throw Error(
`Request Get Error${err.message.match(/reason:(.*)/i) || err.message}`
)
}
}
@@ -82,7 +84,9 @@ export default new class {
if (options.data) {
if (/json/.test(options.headers['Content-Type'])) {
options.body = JSON.stringify(options.data)
} else if (/x-www-form-urlencoded/.test(options.headers['Content-Type'])) {
} else if (
/x-www-form-urlencoded/.test(options.headers['Content-Type'])
) {
options.body = qs(options.data)
} else {
options.body = options.data
@@ -101,7 +105,9 @@ export default new class {
return res
} catch (err) {
logger.error(err)
throw Error(`Request Post Errorreason${err.message.match(/reason:(.*)/)[1]}`)
throw Error(
`Request Post Errorreason${err.message.match(/reason:(.*)/)[1]}`
)
}
}
@@ -138,15 +144,20 @@ export default new class {
}
getAgent (cf) {
let { proxyAddress, switchProxy } = Config.proxy; let { cfTLSVersion } = Config.picSearch
return cf ? this.getTlsVersionAgent(proxyAddress, cfTLSVersion) : (switchProxy ? new HttpsProxyAgent(proxyAddress) : false)
let { proxyAddress, switchProxy } = Config.proxy
let { cfTLSVersion } = Config.picSearch
return cf
? this.getTlsVersionAgent(proxyAddress, cfTLSVersion)
: switchProxy
? new HttpsProxyAgent(proxyAddress)
: false
}
/**
* 从代理字符串获取指定 TLS 版本的代理
* @param {string} str
* @param {import('tls').SecureVersion} tlsVersion
*/
* 从代理字符串获取指定 TLS 版本的代理
* @param {string} str
* @param {import('tls').SecureVersion} tlsVersion
*/
getTlsVersionAgent (str, tlsVersion) {
const tlsOpts = {
maxVersion: tlsVersion,
@@ -155,7 +166,16 @@ export default new class {
if (typeof str === 'string') {
const isHttp = str.startsWith('http')
if (isHttp && Config.proxy.switchProxy) {
const opts = { ..._.pick(new URL(str), ['protocol', 'hostname', 'port', 'username', 'password']), tls: tlsOpts }
const opts = {
..._.pick(new URL(str), [
'protocol',
'hostname',
'port',
'username',
'password'
]),
tls: tlsOpts
}
return new HttpsProxyAgent(opts)
}
}
@@ -172,13 +192,9 @@ export default new class {
*/
async proxyRequestImg (url, { cache, timeout, headers } = {}) {
if (!this.getAgent()) return segment.image(url, cache, timeout, headers)
let buffer = await this.get(url, {
let Request = await this.get(url, {
headers
}).then(res => res.arrayBuffer())
.catch((err) => logger.warn(`图片加载失败 reason: ${err.message}`))
if (!buffer) return segment.image('/plugins/yenai-plugin/resources/img/imgerror.png')
let buff = Buffer.from(buffer)
logger.debug(`Success: imgSize => ${(buff.length / 1024).toFixed(2) + 'kb'}`)
return segment.image(buff, cache, timeout)
}).catch(err => logger.error(err))
return segment.image(Request.body, cache, timeout)
}
}()