尝试解决图片第一次下载失败后无法自动解决图片裂开问题&驱动盘序号错位问题

This commit is contained in:
bietiaop 2024-08-14 20:46:13 +08:00
parent 8304b8bffa
commit 839ca76855
6 changed files with 153 additions and 56 deletions

View file

@ -1,10 +1,10 @@
import path from 'path';
import fs from 'fs';
import fetch from 'node-fetch';
import { ZZZ_SQUARE_AVATAR, ZZZ_SQUARE_BANGBOO } from './mysapi/api.js';
import { imageResourcesPath } from './path.js';
import { char, equip, weapon } from './convert.js';
import { getResourceRemotePath } from './assets.js';
import request from '../utils/request.js';
const ZZZ_SQUARE_AVATAR_PATH = path.join(imageResourcesPath, 'square_avatar');
const ZZZ_SMALL_SQUARE_AVATAR_PATH = path.join(
@ -26,32 +26,24 @@ const ZZZ_SUIT_PATH = path.join(imageResourcesPath, 'suit');
* 下载文件
* @param {string} url 下载地址
* @param {string} savePath 保存路径
* @returns
* @returns {Promise<string | null>} 保存路径
*/
export const downloadFile = async (url, savePath) => {
const _download = async (url, savePath, retry = 0) => {
// 重试次数超过 5 次则返回 null
if (retry > 5) {
return null;
// 下载文件
try {
const download = await request(url, {}, 5);
const arrayBuffer = await download.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// 保存文件
if (!fs.existsSync(path.dirname(savePath))) {
fs.mkdirSync(path.dirname(savePath), { recursive: true });
}
// 下载文件
try {
const download = await fetch(url);
const arrayBuffer = await download.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// 保存文件
if (!fs.existsSync(path.dirname(savePath))) {
fs.mkdirSync(path.dirname(savePath), { recursive: true });
}
fs.writeFileSync(savePath, buffer);
// 返回保存路径
return savePath;
} catch (error) {
// 下载失败,重试
return await _download(url, savePath, retry + 1);
}
};
return await _download(url, savePath);
fs.writeFileSync(savePath, buffer);
// 返回保存路径
return savePath;
} catch (error) {
return null;
}
};
/**
@ -62,7 +54,12 @@ export const downloadFile = async (url, savePath) => {
export const getSquareAvatar = async charID => {
const filename = `role_square_avatar_${charID}.png`;
const avatarPath = path.join(ZZZ_SQUARE_AVATAR_PATH, filename);
if (fs.existsSync(avatarPath)) return avatarPath;
if (fs.existsSync(avatarPath)) {
const stats = fs.statSync(avatarPath);
if (stats.size > 0) {
return avatarPath;
}
}
const url = `${ZZZ_SQUARE_AVATAR}/${filename}`;
const savePath = avatarPath;
const download = await downloadFile(url, savePath);
@ -80,7 +77,12 @@ export const getSmallSquareAvatar = async charID => {
if (!sprite) return null;
const filename = `IconRoleGeneral${sprite}.png`;
const avatarPath = path.join(ZZZ_SMALL_SQUARE_AVATAR_PATH, filename);
if (fs.existsSync(avatarPath)) return avatarPath;
if (fs.existsSync(avatarPath)) {
const stats = fs.statSync(avatarPath);
if (stats.size > 0) {
return avatarPath;
}
}
const url = await getResourceRemotePath('role_general', filename);
const savePath = avatarPath;
const download = await downloadFile(url, savePath);
@ -95,7 +97,12 @@ export const getSmallSquareAvatar = async charID => {
export const getSquareBangboo = async bangbooId => {
const filename = `bangboo_rectangle_avatar_${bangbooId}.png`;
const bangbooPath = path.join(ZZZ_SQUARE_BANGBOO_PATH, filename);
if (fs.existsSync(bangbooPath)) return bangbooPath;
if (fs.existsSync(bangbooPath)) {
const stats = fs.statSync(bangbooPath);
if (stats.size > 0) {
return bangbooPath;
}
}
const url = `${ZZZ_SQUARE_BANGBOO}/${filename}`;
const savePath = bangbooPath;
const download = await downloadFile(url, savePath);
@ -111,7 +118,12 @@ export const getWeaponImage = async id => {
const name = weapon.IDToWeaponFileName(id);
const filename = `${name}.png`;
const weaponPath = path.join(ZZZ_WEAPON_PATH, filename);
if (fs.existsSync(weaponPath)) return weaponPath;
if (fs.existsSync(weaponPath)) {
const stats = fs.statSync(weaponPath);
if (stats.size > 0) {
return weaponPath;
}
}
const url = await getResourceRemotePath('weapon', filename);
const savePath = weaponPath;
const download = await downloadFile(url, savePath);
@ -128,7 +140,12 @@ export const getRoleImage = async id => {
if (!sprite) return null;
const filename = `IconRole${sprite}.png`;
const rolePath = path.join(ZZZ_ROLE_PATH, filename);
if (fs.existsSync(rolePath)) return rolePath;
if (fs.existsSync(rolePath)) {
const stats = fs.statSync(rolePath);
if (stats.size > 0) {
return rolePath;
}
}
const url = await getResourceRemotePath('role', filename);
const savePath = rolePath;
const download = await downloadFile(url, savePath);
@ -145,7 +162,12 @@ export const getRoleCircleImage = async id => {
if (!sprite) return null;
const filename = `IconRoleCircle${sprite}.png`;
const roleCirclePath = path.join(ZZZ_ROLE_CIRCLE_PATH, filename);
if (fs.existsSync(roleCirclePath)) return roleCirclePath;
if (fs.existsSync(roleCirclePath)) {
const stats = fs.statSync(roleCirclePath);
if (stats.size > 0) {
return roleCirclePath;
}
}
const url = await getResourceRemotePath('role_circle', filename);
const savePath = roleCirclePath;
const download = await downloadFile(url, savePath);
@ -161,7 +183,12 @@ export const getSuitImage = async suitId => {
const suitName = equip.equipIdToSprite(suitId);
const filename = `${suitName}.png`;
const suitPath = path.join(ZZZ_SUIT_PATH, filename);
if (fs.existsSync(suitPath)) return suitPath;
if (fs.existsSync(suitPath)) {
const stats = fs.statSync(suitPath);
if (stats.size > 0) {
return suitPath;
}
}
const url = await getResourceRemotePath('suit', filename);
const savePath = suitPath;
const download = await downloadFile(url, savePath);
@ -177,7 +204,12 @@ export const getSuit3DImage = async suitId => {
const suitName = equip.equipIdToSprite(suitId);
const filename = `${suitName}_3d.png`;
const suitPath = path.join(ZZZ_SUIT_3D_PATH, filename);
if (fs.existsSync(suitPath)) return suitPath;
if (fs.existsSync(suitPath)) {
const stats = fs.statSync(suitPath);
if (stats.size > 0) {
return suitPath;
}
}
const url = await getResourceRemotePath('suit_3d', filename);
const savePath = suitPath;
const download = await downloadFile(url, savePath);

View file

@ -3,7 +3,7 @@ import { sleep } from '../utils/time.js';
import { rank } from './convert.js';
import { getGachaLog, saveGachaLog } from './db.js';
import { ZZZ_GET_GACHA_LOG_API } from './mysapi/api.js';
import { fetchWithRetry } from '../utils/network.js';
import request from '../utils/request.js';
// 池子代码
export const gacha_type_meta_data = {
@ -121,11 +121,12 @@ export const getZZZGachaLogByAuthkey = async (
endId
);
// 发送请求
const response = await fetchWithRetry(link, {
const response = await request(link, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
retry: 3,
});
// 获取数据
const data = await response.json();

View file

@ -474,6 +474,17 @@ export class ZZZAvatarInfo {
return score;
}
/** @type {Equip[]} equip_final */
get equip_final() {
const result = [];
this.equip.forEach(equip => {
if (equip.equipment_type) {
result[equip.equipment_type - 1] = equip;
}
});
return result;
}
/**
* 获取基础资源
* @returns {Promise<void>}

View file

@ -150,7 +150,8 @@
</div>
{{/if}}
<div class="equip-list">
{{each charData.equip equip}}
{{each charData.equip_final equip}}
{{if equip}}
<div class="box">
<div class="icon">
<img src="{{equip.suit_icon}}" alt="">
@ -199,13 +200,14 @@
{{/each}}
</div>
</div>
{{/each}}
<% for(let i=charData.equip.length; i < 6 ; i++) { %>
{{else}}
<div class="box empty">
<div class="icon">
</div>
</div>
<% } %>
{{/if}}
{{/each}}
</div>
{{if charData?.damages && charData?.damages?.length}}

View file

@ -1,6 +1,5 @@
import http from 'http';
import https from 'https';
import fetch from 'node-fetch';
export async function checkLatency(url) {
let request = http;
if (url.startsWith('https')) {
@ -36,20 +35,3 @@ export function getQueryVariable(url, variable) {
const key = searchParams.get(variable);
return key;
}
export async function fetchWithRetry(url, options, retry = 3) {
let err;
const _fetch = async (url, options, retryCount = 0) => {
if (retryCount > retry) {
throw new Error('Retry limit reached', err);
}
try {
return await fetch(url, options);
} catch (error) {
logger.debug(`Fetch error: ${error.message}`);
err = error;
return _fetch(url, options, retryCount + 1);
}
};
return _fetch(url, options);
}

69
utils/request.js Normal file
View file

@ -0,0 +1,69 @@
import fetch from 'node-fetch';
/**
* 请求
* @param {string} url 请求地址
* @param {object} options 请求配置
* @returns {Promise<Response>}
*/
const _request = async (url, options) => {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
} else if (response.status < 200 || response.status >= 300) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response;
};
/**
* 请求
* @param {string} url 请求地址
* @param {object} options 请求配置
* @param {number} retry 重试次数
* @returns {Promise<Response>}
*/
const request = (url, options, retry = 0) => {
let err;
const _fetch = async (url, options, retryCount = 0) => {
if (retryCount > retry) {
throw new Error('Retry limit reached', err);
}
try {
return await _request(url, options);
} catch (error) {
logger.debug(`Fetch error: ${error.message}`);
err = error;
return await _fetch(url, options, retryCount + 1);
}
};
return _fetch(url, options);
};
/**
* GET 请求
* @param {string} url 请求地址
* @param {object} data 请求数据
* @param {object} options 请求配置
* @returns {Promise<Response>}
*/
request.get = async (url, data, options) => {
const params = new URLSearchParams(data);
const { retry, ...restOptions } = options;
return request(`${url}?${params}`, restOptions, retry);
};
/**
* POST 请求
* @param {string} url 请求地址
* @param {object} data 请求数据
* @param {object} options 请求配置
* @returns {Promise<Response>}
*/
request.post = async (url, data, options) => {
const body = JSON.stringify(data);
const { retry, ...restOptions } = options;
return request(url, { ...restOptions, method: 'POST', body }, retry);
};
export default request;