koishi-plugin-custom-image 0.2.2 → 0.2.3
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/index.d.ts +1 -0
- package/lib/index.js +34 -19
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export interface Config {
|
|
|
17
17
|
downloadImageBeforeSend: boolean;
|
|
18
18
|
messageBufferDelay: number;
|
|
19
19
|
tempDir: string;
|
|
20
|
+
useImageUrlInsteadOfFile: boolean;
|
|
20
21
|
}
|
|
21
22
|
export declare const Config: Schema<Config>;
|
|
22
23
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -81,15 +81,18 @@ exports.Config = koishi_1.Schema.object({
|
|
|
81
81
|
.min(0)
|
|
82
82
|
.description('【缓存设置】自动清理缓存间隔(分钟)'),
|
|
83
83
|
downloadImageBeforeSend: koishi_1.Schema.boolean()
|
|
84
|
-
.default(
|
|
85
|
-
.description('
|
|
84
|
+
.default(false)
|
|
85
|
+
.description('【展示设置】发送前下载图片(关闭可解决OneBot文件上传问题)'),
|
|
86
86
|
messageBufferDelay: koishi_1.Schema.number()
|
|
87
87
|
.default(0)
|
|
88
88
|
.min(0)
|
|
89
89
|
.description('【性能设置】消息缓冲延迟:合并短时间内的多个图片请求(秒)'),
|
|
90
90
|
tempDir: koishi_1.Schema.string()
|
|
91
91
|
.default(path_1.default.join(process.cwd(), 'temp_images'))
|
|
92
|
-
.description('【文件设置】临时图片保存目录')
|
|
92
|
+
.description('【文件设置】临时图片保存目录'),
|
|
93
|
+
useImageUrlInsteadOfFile: koishi_1.Schema.boolean()
|
|
94
|
+
.default(true)
|
|
95
|
+
.description('【兼容设置】强制使用图片URL发送(避免OneBot文件上传错误)')
|
|
93
96
|
});
|
|
94
97
|
if (!worker_threads_1.isMainThread) {
|
|
95
98
|
const { url, filePath } = worker_threads_1.workerData;
|
|
@@ -137,7 +140,7 @@ async function sendTimeout(session, content, config) {
|
|
|
137
140
|
async function downloadImageWithThreads(url, filename, config) {
|
|
138
141
|
return new Promise((resolve, reject) => {
|
|
139
142
|
if (!fs_1.default.existsSync(config.tempDir)) {
|
|
140
|
-
fs_1.default.mkdirSync(config.tempDir, { recursive: true });
|
|
143
|
+
fs_1.default.mkdirSync(config.tempDir, { recursive: true, mode: 0o777 });
|
|
141
144
|
}
|
|
142
145
|
const filePath = path_1.default.join(config.tempDir, `${filename}.jpg`);
|
|
143
146
|
const worker = new worker_threads_1.Worker(currentFilePath, { workerData: { url, filePath } });
|
|
@@ -229,12 +232,16 @@ async function processCustomImage(session, apiUrl, config) {
|
|
|
229
232
|
return { success: false, msg: getI18nText(session, 'messages.fetchFailed') };
|
|
230
233
|
}
|
|
231
234
|
let imageElem;
|
|
232
|
-
if (
|
|
235
|
+
if (config.useImageUrlInsteadOfFile) {
|
|
236
|
+
imageElem = koishi_1.h.image(result.type === 'url' ? result.data : `data:image/jpeg;base64,${Buffer.from(result.data).toString('base64')}`);
|
|
237
|
+
}
|
|
238
|
+
else if (result.type === 'url' && config.downloadImageBeforeSend) {
|
|
233
239
|
try {
|
|
234
240
|
const filename = crypto_1.default.createHash('md5').update(result.data).digest('hex');
|
|
235
241
|
const filePath = await downloadImageWithThreads(result.data, filename, config);
|
|
236
242
|
const absPath = path_1.default.resolve(filePath);
|
|
237
|
-
|
|
243
|
+
fs_1.default.chmodSync(absPath, 0o777);
|
|
244
|
+
imageElem = koishi_1.h.file(`file://${absPath.replace(/\\/g, '/')}`);
|
|
238
245
|
}
|
|
239
246
|
catch (error) {
|
|
240
247
|
imageElem = koishi_1.h.image(result.data);
|
|
@@ -242,13 +249,13 @@ async function processCustomImage(session, apiUrl, config) {
|
|
|
242
249
|
}
|
|
243
250
|
else if (result.type === 'buffer') {
|
|
244
251
|
if (!fs_1.default.existsSync(config.tempDir)) {
|
|
245
|
-
fs_1.default.mkdirSync(config.tempDir, { recursive: true });
|
|
252
|
+
fs_1.default.mkdirSync(config.tempDir, { recursive: true, mode: 0o777 });
|
|
246
253
|
}
|
|
247
254
|
const filename = crypto_1.default.randomUUID();
|
|
248
255
|
const filePath = path_1.default.join(config.tempDir, `${filename}.jpg`);
|
|
249
256
|
const absPath = path_1.default.resolve(filePath);
|
|
250
|
-
fs_1.default.writeFileSync(absPath, result.data);
|
|
251
|
-
imageElem = koishi_1.h.file(`file
|
|
257
|
+
fs_1.default.writeFileSync(absPath, result.data, { mode: 0o777 });
|
|
258
|
+
imageElem = koishi_1.h.file(`file://${absPath.replace(/\\/g, '/')}`);
|
|
252
259
|
}
|
|
253
260
|
else {
|
|
254
261
|
imageElem = koishi_1.h.image(result.data);
|
|
@@ -347,12 +354,16 @@ function apply(ctx, config) {
|
|
|
347
354
|
const result = await fetchHsjpImage(msg, msg1, msg2, config);
|
|
348
355
|
if (result.success) {
|
|
349
356
|
let imageElem;
|
|
350
|
-
if (
|
|
357
|
+
if (config.useImageUrlInsteadOfFile) {
|
|
358
|
+
imageElem = koishi_1.h.image(result.type === 'url' ? result.data : `data:image/jpeg;base64,${Buffer.from(result.data).toString('base64')}`);
|
|
359
|
+
}
|
|
360
|
+
else if (result.type === 'url' && config.downloadImageBeforeSend) {
|
|
351
361
|
try {
|
|
352
362
|
const filename = crypto_1.default.createHash('md5').update(result.data).digest('hex');
|
|
353
363
|
const filePath = await downloadImageWithThreads(result.data, filename, config);
|
|
354
364
|
const absPath = path_1.default.resolve(filePath);
|
|
355
|
-
|
|
365
|
+
fs_1.default.chmodSync(absPath, 0o777);
|
|
366
|
+
imageElem = koishi_1.h.file(`file://${absPath.replace(/\\/g, '/')}`);
|
|
356
367
|
}
|
|
357
368
|
catch (error) {
|
|
358
369
|
imageElem = koishi_1.h.image(result.data);
|
|
@@ -360,13 +371,13 @@ function apply(ctx, config) {
|
|
|
360
371
|
}
|
|
361
372
|
else if (result.type === 'buffer') {
|
|
362
373
|
if (!fs_1.default.existsSync(config.tempDir)) {
|
|
363
|
-
fs_1.default.mkdirSync(config.tempDir, { recursive: true });
|
|
374
|
+
fs_1.default.mkdirSync(config.tempDir, { recursive: true, mode: 0o777 });
|
|
364
375
|
}
|
|
365
376
|
const filename = crypto_1.default.randomUUID();
|
|
366
377
|
const filePath = path_1.default.join(config.tempDir, `${filename}.jpg`);
|
|
367
378
|
const absPath = path_1.default.resolve(filePath);
|
|
368
|
-
fs_1.default.writeFileSync(absPath, result.data);
|
|
369
|
-
imageElem = koishi_1.h.file(`file
|
|
379
|
+
fs_1.default.writeFileSync(absPath, result.data, { mode: 0o777 });
|
|
380
|
+
imageElem = koishi_1.h.file(`file://${absPath.replace(/\\/g, '/')}`);
|
|
370
381
|
}
|
|
371
382
|
else {
|
|
372
383
|
imageElem = koishi_1.h.image(result.data);
|
|
@@ -389,12 +400,16 @@ function apply(ctx, config) {
|
|
|
389
400
|
const result = await fetchDmjpImage(text, config);
|
|
390
401
|
if (result.success) {
|
|
391
402
|
let imageElem;
|
|
392
|
-
if (
|
|
403
|
+
if (config.useImageUrlInsteadOfFile) {
|
|
404
|
+
imageElem = koishi_1.h.image(result.type === 'url' ? result.data : `data:image/jpeg;base64,${Buffer.from(result.data).toString('base64')}`);
|
|
405
|
+
}
|
|
406
|
+
else if (result.type === 'url' && config.downloadImageBeforeSend) {
|
|
393
407
|
try {
|
|
394
408
|
const filename = crypto_1.default.createHash('md5').update(result.data).digest('hex');
|
|
395
409
|
const filePath = await downloadImageWithThreads(result.data, filename, config);
|
|
396
410
|
const absPath = path_1.default.resolve(filePath);
|
|
397
|
-
|
|
411
|
+
fs_1.default.chmodSync(absPath, 0o777);
|
|
412
|
+
imageElem = koishi_1.h.file(`file://${absPath.replace(/\\/g, '/')}`);
|
|
398
413
|
}
|
|
399
414
|
catch (error) {
|
|
400
415
|
imageElem = koishi_1.h.image(result.data);
|
|
@@ -402,13 +417,13 @@ function apply(ctx, config) {
|
|
|
402
417
|
}
|
|
403
418
|
else if (result.type === 'buffer') {
|
|
404
419
|
if (!fs_1.default.existsSync(config.tempDir)) {
|
|
405
|
-
fs_1.default.mkdirSync(config.tempDir, { recursive: true });
|
|
420
|
+
fs_1.default.mkdirSync(config.tempDir, { recursive: true, mode: 0o777 });
|
|
406
421
|
}
|
|
407
422
|
const filename = crypto_1.default.randomUUID();
|
|
408
423
|
const filePath = path_1.default.join(config.tempDir, `${filename}.jpg`);
|
|
409
424
|
const absPath = path_1.default.resolve(filePath);
|
|
410
|
-
fs_1.default.writeFileSync(absPath, result.data);
|
|
411
|
-
imageElem = koishi_1.h.file(`file
|
|
425
|
+
fs_1.default.writeFileSync(absPath, result.data, { mode: 0o777 });
|
|
426
|
+
imageElem = koishi_1.h.file(`file://${absPath.replace(/\\/g, '/')}`);
|
|
412
427
|
}
|
|
413
428
|
else {
|
|
414
429
|
imageElem = koishi_1.h.image(result.data);
|