koishi-plugin-checkgal 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/api.d.ts CHANGED
@@ -4,8 +4,9 @@ import { Config } from './config';
4
4
  export declare class TouchGalAPI {
5
5
  private http;
6
6
  private logger;
7
+ private tempDir;
7
8
  constructor(ctx: Context);
8
9
  searchGame(keyword: string, config: Config): Promise<GameInfo[]>;
9
10
  getDownloads(patchId: number): Promise<DownloadResource[]>;
10
- downloadAndConvertImage(url: string): Promise<Buffer | null>;
11
+ downloadAndConvertImage(url: string): Promise<string | null>;
11
12
  }
package/lib/api.js CHANGED
@@ -1,14 +1,55 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
37
  };
5
38
  Object.defineProperty(exports, "__esModule", { value: true });
6
39
  exports.TouchGalAPI = void 0;
7
40
  const sharp_1 = __importDefault(require("sharp"));
41
+ const path = __importStar(require("path"));
42
+ const crypto = __importStar(require("crypto"));
43
+ const fs_1 = require("fs");
8
44
  class TouchGalAPI {
9
45
  constructor(ctx) {
10
46
  this.http = ctx.http;
11
47
  this.logger = ctx.logger('checkgal-api');
48
+ this.tempDir = path.join(ctx.app.baseDir, 'data', 'temp', 'checkgal');
49
+ // 确保临时目录存在
50
+ ctx.on('ready', async () => {
51
+ await fs_1.promises.mkdir(this.tempDir, { recursive: true });
52
+ });
12
53
  }
13
54
  async searchGame(keyword, config) {
14
55
  const url = 'https://www.touchgal.us/api/search';
@@ -72,7 +113,12 @@ class TouchGalAPI {
72
113
  // 类型守卫,确保 response 是 ArrayBuffer
73
114
  if (!(response instanceof ArrayBuffer))
74
115
  return null;
75
- return (0, sharp_1.default)(Buffer.from(response)).jpeg().toBuffer();
116
+ const imageBuffer = await (0, sharp_1.default)(Buffer.from(response)).jpeg().toBuffer();
117
+ // 生成唯一文件名并写入临时目录
118
+ const hash = crypto.createHash('md5').update(url).digest('hex');
119
+ const filePath = path.join(this.tempDir, `${hash}.jpg`);
120
+ await fs_1.promises.writeFile(filePath, imageBuffer);
121
+ return filePath;
76
122
  }
77
123
  catch (error) {
78
124
  this.logger.error(`Failed to download or convert image from ${url}:`, error);
package/lib/commands.js CHANGED
@@ -19,11 +19,11 @@ function apply(ctx, config) {
19
19
  // 缓存结果
20
20
  results.forEach(game => ctx.gameCache.set(game.id, game));
21
21
  // 并发下载并转换所有图片
22
- const imageBuffers = await Promise.all(results.map(game => ctx.touchgal.downloadAndConvertImage(game.banner)));
22
+ const imagePaths = await Promise.all(results.map(game => ctx.touchgal.downloadAndConvertImage(game.banner)));
23
23
  const forwardMessages = results.map((game, index) => {
24
- const imageBuffer = imageBuffers[index];
25
- const imageElement = imageBuffer
26
- ? (0, koishi_1.h)('image', { url: `base64://${imageBuffer.toString('base64')}` })
24
+ const imagePath = imagePaths[index];
25
+ const imageElement = imagePath
26
+ ? (0, koishi_1.h)('image', { url: `file://${imagePath}` })
27
27
  : (0, koishi_1.h)('text', { content: '封面图加载失败' });
28
28
  const content = [
29
29
  imageElement,
@@ -63,9 +63,9 @@ function apply(ctx, config) {
63
63
  return `未找到ID为 ${id} 的下载资源。`;
64
64
  }
65
65
  const gameTitle = gameInfo ? `游戏: ${gameInfo.name} (ID: ${id})` : `游戏ID: ${id}`;
66
- const imageBuffer = gameInfo ? await ctx.touchgal.downloadAndConvertImage(gameInfo.banner) : null;
67
- const imageElement = imageBuffer
68
- ? (0, koishi_1.h)('image', { url: `base64://${imageBuffer.toString('base64')}` })
66
+ const imagePath = gameInfo ? await ctx.touchgal.downloadAndConvertImage(gameInfo.banner) : null;
67
+ const imageElement = imagePath
68
+ ? (0, koishi_1.h)('image', { url: `file://${imagePath}` })
69
69
  : (0, koishi_1.h)('text', { content: gameInfo ? '封面图加载失败' : '' });
70
70
  const header = [
71
71
  imageElement,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-checkgal",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "个人测试请勿使用",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",