Skip to content

Commit

Permalink
perf: 缓存图片到本地
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Dec 1, 2024
1 parent 0114e85 commit 81045e3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ test
node_modules
config/config
data
data.db
data.db
temp
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pnpm install --filter=steam-plugin
- [x] steam绑定
- [x] steam更换绑定
- [x] steam解除绑定
- [x] steam状态
- [x] steam库存
- [x] steam最近游玩
- [x] steam愿望单
Expand All @@ -84,7 +85,6 @@ pnpm install --filter=steam-plugin
- [x] 开启/关闭推送
- [x] steam特惠
- [x] 推送黑/白群名单
- [ ] steam状态渲染成图片
- [ ] steam绑定渲染成图片
- [x] steam评论
- [ ] steam游戏详情
Expand Down
11 changes: 7 additions & 4 deletions apps/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export const rule = {
const color = info.gameid ? 1 : info.personastate === 0 ? 3 : 2
const bg = await api.IPlayerService.GetProfileItemsEquipped(steamId)
const img = await Render.render('info/index', {
background: utils.getStaticUrl(bg.mini_profile_background.image_large),
frame: utils.getStaticUrl(bg.avatar_frame.image_small),
avatar: Config.other.steamAvatar ? info.avatarfull : await utils.getUserAvatar(e.self_id, uid, e.group_id),
background: await utils.saveImg(utils.getStaticUrl(bg.mini_profile_background.image_large)),
frame: await utils.saveImg(utils.getStaticUrl(bg.avatar_frame.image_small)),
avatar: await utils.saveImg(Config.other.steamAvatar ? info.avatarfull : await utils.getUserAvatar(e.self_id, uid, e.group_id)),
name: info.personaname,
status: utils.getPersonaState(info.personastate),
gameId: info.gameid,
Expand All @@ -41,7 +41,10 @@ export const rule = {
lastTime: (info.lastlogoff && info.personastate === 0) ? moment.unix(info.lastlogoff).format('YYYY-MM-DD HH:mm:ss') : '',
country: info.loccountrycode ? getLoccountryCode(info.loccountrycode) : '',
color,
scale: 1.4
scale: 1.4,
pageGotoParams: {
waitUntil: 'load'
}
})
if (img) {
await e.reply(img)
Expand Down
49 changes: 45 additions & 4 deletions models/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Version } from '#components'
import * as request from './request.js'
import fs from 'fs'
import moment from 'moment'
import { Bot, logger } from '#lib'
import { Version } from '#components'
import * as request from './request.js'
import { join } from 'path'

export { request }

const tempDir = join(Version.pluginPath, 'temp')

if (fs.existsSync(tempDir)) {
fs.rmdirSync(tempDir, { recursive: true })
}
fs.mkdirSync(tempDir)

const steamIdOffset = 76561197960265728n

/**
Expand Down Expand Up @@ -206,11 +215,13 @@ export function getStaticUrl (path) {
/**
* 获取图片buffer
* @param {string} url
* @param {number} retry 重试次数 默认3
* @returns {Promise<Buffer|null|string>}
*/
export async function getImgUrlBuffer (url) {
export async function getImgUrlBuffer (url, retry = 3) {
if (!url) return null
for (let i = 0; i < 3; i++) {
retry = Number(retry) || 3
for (let i = 0; i < retry; i++) {
try {
const buffer = await request.get(url, { responseType: 'arraybuffer', baseUrl: '' }).then(res => res.data)
if (Version.BotName === 'Karin') {
Expand All @@ -225,6 +236,36 @@ export async function getImgUrlBuffer (url) {
return null
}

/**
* 将图片保存到临时文件夹
* @param {*} url
* @param {number} retry 重试次数 默认3
* @returns {Promise<string>} 图片绝对路径
*/
export async function saveImg (url, retry = 3) {
if (!url) return ''
retry = Number(retry) || 3
for (let i = 0; i < retry; i++) {
try {
let ext = ''
const buffer = await request.get(url, { responseType: 'arraybuffer', baseUrl: '' }).then(res => {
ext = res.headers['content-type']?.split('/')?.pop() || 'png'
return res.data
})
const filename = `${Date.now()}.${ext}`
const filepath = join(tempDir, filename)
fs.writeFileSync(filepath, buffer)
setTimeout(() => {
fs.unlinkSync(filepath)
}, 1000 * 60 * 10) // 10分钟后删除
return filepath.replace(/\\/g, '/')
} catch (error) {
logger.error(`保存图片${url}失败: ${error.message}, 第${i + 1}次重试`)
}
}
return ''
}

/**
* 将用户状态码转换为中文
* @param {number} state
Expand Down
2 changes: 1 addition & 1 deletion resources/info/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<link rel="stylesheet" type="text/css" href="{{pluResPath}}info/index.css" />
<style>
#container {
background: url({{background}}) no-repeat #363a42;
background: url('{{background}}') no-repeat #363a42;
}
</style>
{{/block}}
Expand Down

0 comments on commit 81045e3

Please sign in to comment.