koishi-plugin-echo-cave 1.10.4 → 1.11.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/image-helper.d.ts +2 -1
- package/lib/index.cjs +58 -53
- package/package.json +1 -1
package/lib/image-helper.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { Context } from 'koishi';
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function saveMedia(ctx: Context, mediaElement: Record<string, any>, type: 'image' | 'video' | 'file'): Promise<string>;
|
|
3
|
+
export declare function processMediaElement(ctx: Context, element: any): Promise<any>;
|
package/lib/index.cjs
CHANGED
|
@@ -219,44 +219,66 @@ function formatDate(date) {
|
|
|
219
219
|
var import_axios = __toESM(require("axios"), 1);
|
|
220
220
|
var import_node_fs = require("node:fs");
|
|
221
221
|
var import_node_path = __toESM(require("node:path"), 1);
|
|
222
|
-
async function
|
|
223
|
-
const
|
|
224
|
-
const
|
|
222
|
+
async function saveMedia(ctx, mediaElement, type) {
|
|
223
|
+
const mediaUrl = mediaElement.url;
|
|
224
|
+
const originalMediaName = mediaElement.file;
|
|
225
225
|
const ext = (() => {
|
|
226
|
-
const i =
|
|
227
|
-
return i === -1 ? "png" :
|
|
226
|
+
const i = originalMediaName.lastIndexOf(".");
|
|
227
|
+
return i === -1 ? type === "image" ? "png" : type === "video" ? "mp4" : "bin" : originalMediaName.slice(i + 1).toLowerCase();
|
|
228
228
|
})();
|
|
229
|
-
const
|
|
230
|
-
const
|
|
231
|
-
const
|
|
232
|
-
ctx.logger.info(`Saving
|
|
229
|
+
const mediaDir = import_node_path.default.join(ctx.baseDir, "data", "cave", type + "s");
|
|
230
|
+
const mediaName = Date.now().toString();
|
|
231
|
+
const fullMediaPath = import_node_path.default.join(mediaDir, `${mediaName}.${ext}`);
|
|
232
|
+
ctx.logger.info(`Saving ${type} from ${mediaUrl} -> ${fullMediaPath}`);
|
|
233
233
|
try {
|
|
234
|
-
await import_node_fs.promises.mkdir(
|
|
235
|
-
const res = await import_axios.default.get(
|
|
234
|
+
await import_node_fs.promises.mkdir(mediaDir, { recursive: true });
|
|
235
|
+
const res = await import_axios.default.get(mediaUrl, {
|
|
236
236
|
responseType: "arraybuffer",
|
|
237
237
|
validateStatus: () => true
|
|
238
238
|
});
|
|
239
239
|
if (res.status < 200 || res.status >= 300) {
|
|
240
|
-
ctx.logger.warn(
|
|
241
|
-
|
|
240
|
+
ctx.logger.warn(
|
|
241
|
+
`${type.charAt(0).toUpperCase() + type.slice(1)} download failed: HTTP ${res.status}`
|
|
242
|
+
);
|
|
243
|
+
return mediaUrl;
|
|
242
244
|
}
|
|
243
|
-
const
|
|
244
|
-
if (
|
|
245
|
-
|
|
246
|
-
|
|
245
|
+
const contentType = res.headers["content-type"];
|
|
246
|
+
if (contentType) {
|
|
247
|
+
if (type === "image" && !contentType.startsWith("image/")) {
|
|
248
|
+
ctx.logger.warn(`Invalid image content-type: ${contentType}`);
|
|
249
|
+
return mediaUrl;
|
|
250
|
+
}
|
|
251
|
+
if (type === "video" && !contentType.startsWith("video/")) {
|
|
252
|
+
ctx.logger.warn(`Invalid video content-type: ${contentType}`);
|
|
253
|
+
return mediaUrl;
|
|
254
|
+
}
|
|
247
255
|
}
|
|
248
256
|
const buffer = Buffer.from(res.data);
|
|
249
257
|
if (!buffer || buffer.length === 0) {
|
|
250
|
-
ctx.logger.warn(
|
|
251
|
-
return
|
|
258
|
+
ctx.logger.warn(`Downloaded ${type} buffer is empty`);
|
|
259
|
+
return mediaUrl;
|
|
252
260
|
}
|
|
253
|
-
await import_node_fs.promises.writeFile(
|
|
254
|
-
ctx.logger.info(
|
|
255
|
-
|
|
261
|
+
await import_node_fs.promises.writeFile(fullMediaPath, buffer);
|
|
262
|
+
ctx.logger.info(
|
|
263
|
+
`${type.charAt(0).toUpperCase() + type.slice(1)} saved successfully: ${fullMediaPath}`
|
|
264
|
+
);
|
|
265
|
+
return fullMediaPath;
|
|
256
266
|
} catch (err) {
|
|
257
|
-
ctx.logger.error(`Failed to save
|
|
258
|
-
return
|
|
267
|
+
ctx.logger.error(`Failed to save ${type}: ${err}`);
|
|
268
|
+
return mediaUrl;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async function processMediaElement(ctx, element) {
|
|
272
|
+
if (element.type === "image" || element.type === "video" || element.type === "file") {
|
|
273
|
+
return {
|
|
274
|
+
...element,
|
|
275
|
+
data: {
|
|
276
|
+
...element.data,
|
|
277
|
+
url: await saveMedia(ctx, element.data, element.type)
|
|
278
|
+
}
|
|
279
|
+
};
|
|
259
280
|
}
|
|
281
|
+
return element;
|
|
260
282
|
}
|
|
261
283
|
|
|
262
284
|
// src/forward-helper.ts
|
|
@@ -285,41 +307,21 @@ async function processForwardMessageContent(ctx, msg) {
|
|
|
285
307
|
}
|
|
286
308
|
return Promise.all(
|
|
287
309
|
msg.message.map(async (element) => {
|
|
288
|
-
|
|
289
|
-
return {
|
|
290
|
-
...element,
|
|
291
|
-
data: {
|
|
292
|
-
...element.data,
|
|
293
|
-
url: await saveImages(ctx, element.data)
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
return element;
|
|
310
|
+
return processMediaElement(ctx, element);
|
|
298
311
|
})
|
|
299
312
|
);
|
|
300
313
|
}
|
|
301
314
|
|
|
302
315
|
// src/msg-helper.ts
|
|
303
316
|
async function processMessageContent(ctx, msg) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
...element,
|
|
313
|
-
data: {
|
|
314
|
-
...element.data,
|
|
315
|
-
url: newUrl
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
} else {
|
|
319
|
-
result.push(element);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
return result;
|
|
317
|
+
return Promise.all(
|
|
318
|
+
msg.map(async (element) => {
|
|
319
|
+
if (element.type === "reply") {
|
|
320
|
+
return element;
|
|
321
|
+
}
|
|
322
|
+
return processMediaElement(ctx, element);
|
|
323
|
+
})
|
|
324
|
+
);
|
|
323
325
|
}
|
|
324
326
|
|
|
325
327
|
// src/index.ts
|
|
@@ -496,6 +498,9 @@ async function addCave(ctx, session) {
|
|
|
496
498
|
if (typeof message === "string") {
|
|
497
499
|
msgJson = import_koishi_plugin_adapter_onebot2.CQCode.parse(message);
|
|
498
500
|
} else {
|
|
501
|
+
if (message[0].type === "video" || message[0].type === "file") {
|
|
502
|
+
type = "forward";
|
|
503
|
+
}
|
|
499
504
|
msgJson = message;
|
|
500
505
|
}
|
|
501
506
|
content = JSON.stringify(await processMessageContent(ctx, msgJson));
|