koishi-plugin-echo-cave 1.12.2 → 1.14.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/index.cjs +31 -6
- package/lib/index.d.ts +2 -0
- package/lib/media-helper.d.ts +2 -2
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -40,7 +40,9 @@ var require_zh_CN = __commonJS({
|
|
|
40
40
|
enableSizeLimit: "\u662F\u5426\u542F\u7528\u5A92\u4F53\u6587\u4EF6\u5927\u5C0F\u9650\u5236",
|
|
41
41
|
maxImageSize: "\u6700\u5927\u56FE\u7247\u5927\u5C0F (MB)",
|
|
42
42
|
maxVideoSize: "\u6700\u5927\u89C6\u9891\u5927\u5C0F (MB)",
|
|
43
|
-
maxFileSize: "\u6700\u5927\u6587\u4EF6\u5927\u5C0F (MB)"
|
|
43
|
+
maxFileSize: "\u6700\u5927\u6587\u4EF6\u5927\u5C0F (MB)",
|
|
44
|
+
maxRecordSize: "\u6700\u5927\u5F55\u97F3\u5927\u5C0F (MB)",
|
|
45
|
+
useBase64ForMedia: "\u662F\u5426\u4F7F\u7528Base64\u7F16\u7801\u4FDD\u5B58\u5A92\u4F53\u6587\u4EF6\uFF0C\u5F00\u542F\u540E\u5C06\u76F4\u63A5\u5B58\u50A8\u6587\u4EF6\u5185\u5BB9\u800C\u975E\u6587\u4EF6\u8DEF\u5F84"
|
|
44
46
|
},
|
|
45
47
|
"echo-cave": {
|
|
46
48
|
general: {
|
|
@@ -228,7 +230,7 @@ async function saveMedia(ctx, mediaElement, type, cfg) {
|
|
|
228
230
|
const originalMediaName = mediaElement.file;
|
|
229
231
|
const ext = (() => {
|
|
230
232
|
const i = originalMediaName.lastIndexOf(".");
|
|
231
|
-
return i === -1 ? type === "image" ? "png" : type === "video" ? "mp4" : "bin" : originalMediaName.slice(i + 1).toLowerCase();
|
|
233
|
+
return i === -1 ? type === "image" ? "png" : type === "video" ? "mp4" : type === "record" ? "mp3" : "bin" : originalMediaName.slice(i + 1).toLowerCase();
|
|
232
234
|
})();
|
|
233
235
|
const mediaDir = import_node_path.default.join(ctx.baseDir, "data", "cave", type + "s");
|
|
234
236
|
const mediaName = Date.now().toString();
|
|
@@ -256,6 +258,10 @@ async function saveMedia(ctx, mediaElement, type, cfg) {
|
|
|
256
258
|
ctx.logger.warn(`Invalid video content-type: ${contentType}`);
|
|
257
259
|
return mediaUrl;
|
|
258
260
|
}
|
|
261
|
+
if (type === "record" && !contentType.startsWith("audio/")) {
|
|
262
|
+
ctx.logger.warn(`Invalid record content-type: ${contentType}`);
|
|
263
|
+
return mediaUrl;
|
|
264
|
+
}
|
|
259
265
|
}
|
|
260
266
|
const buffer = Buffer.from(res.data);
|
|
261
267
|
if (!buffer || buffer.length === 0) {
|
|
@@ -274,19 +280,34 @@ async function saveMedia(ctx, mediaElement, type, cfg) {
|
|
|
274
280
|
}
|
|
275
281
|
}
|
|
276
282
|
async function processMediaElement(ctx, element, cfg) {
|
|
277
|
-
if (element.type === "image" || element.type === "video" || element.type === "file") {
|
|
283
|
+
if (element.type === "image" || element.type === "video" || element.type === "file" || element.type === "record") {
|
|
278
284
|
const savedPath = await saveMedia(
|
|
279
285
|
ctx,
|
|
280
286
|
element.data,
|
|
281
287
|
element.type,
|
|
282
288
|
cfg
|
|
283
289
|
);
|
|
284
|
-
|
|
290
|
+
let fileValue;
|
|
291
|
+
if (cfg.useBase64ForMedia) {
|
|
292
|
+
const buffer = await import_node_fs.promises.readFile(savedPath);
|
|
293
|
+
const base64 = buffer.toString("base64");
|
|
294
|
+
const mimeTypes = {
|
|
295
|
+
image: "image/jpeg",
|
|
296
|
+
video: "video/mp4",
|
|
297
|
+
record: "audio/mpeg",
|
|
298
|
+
file: "application/octet-stream"
|
|
299
|
+
};
|
|
300
|
+
const mimeType = mimeTypes[element.type] || "application/octet-stream";
|
|
301
|
+
fileValue = `data:${mimeType};base64,${base64}`;
|
|
302
|
+
} else {
|
|
303
|
+
const fileUri = `file:///${savedPath.replace(/\\/g, "/")}`;
|
|
304
|
+
fileValue = fileUri;
|
|
305
|
+
}
|
|
285
306
|
return {
|
|
286
307
|
...element,
|
|
287
308
|
data: {
|
|
288
309
|
...element.data,
|
|
289
|
-
file:
|
|
310
|
+
file: fileValue,
|
|
290
311
|
// Remove the url field
|
|
291
312
|
url: void 0
|
|
292
313
|
}
|
|
@@ -308,6 +329,8 @@ async function checkAndCleanMediaFiles(ctx, cfg, type) {
|
|
|
308
329
|
return (cfg.maxVideoSize || 500) * 1024 * 1024;
|
|
309
330
|
case "file":
|
|
310
331
|
return (cfg.maxFileSize || 1e3) * 1024 * 1024;
|
|
332
|
+
case "record":
|
|
333
|
+
return (cfg.maxRecordSize || 200) * 1024 * 1024;
|
|
311
334
|
}
|
|
312
335
|
})();
|
|
313
336
|
try {
|
|
@@ -425,7 +448,9 @@ var Config = import_koishi.Schema.object({
|
|
|
425
448
|
enableSizeLimit: import_koishi.Schema.boolean().default(false),
|
|
426
449
|
maxImageSize: import_koishi.Schema.number().default(2048),
|
|
427
450
|
maxVideoSize: import_koishi.Schema.number().default(512),
|
|
428
|
-
maxFileSize: import_koishi.Schema.number().default(512)
|
|
451
|
+
maxFileSize: import_koishi.Schema.number().default(512),
|
|
452
|
+
maxRecordSize: import_koishi.Schema.number().default(512),
|
|
453
|
+
useBase64ForMedia: import_koishi.Schema.boolean().default(false)
|
|
429
454
|
}).i18n({
|
|
430
455
|
"zh-CN": require_zh_CN()._config
|
|
431
456
|
});
|
package/lib/index.d.ts
CHANGED
package/lib/media-helper.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Config } from './index';
|
|
2
2
|
import { Context } from 'koishi';
|
|
3
|
-
export declare function saveMedia(ctx: Context, mediaElement: Record<string, any>, type: 'image' | 'video' | 'file', cfg: Config): Promise<string>;
|
|
3
|
+
export declare function saveMedia(ctx: Context, mediaElement: Record<string, any>, type: 'image' | 'video' | 'file' | 'record', cfg: Config): Promise<string>;
|
|
4
4
|
export declare function processMediaElement(ctx: Context, element: any, cfg: Config): Promise<any>;
|
|
5
|
-
export declare function checkAndCleanMediaFiles(ctx: Context, cfg: Config, type: 'image' | 'video' | 'file'): Promise<void>;
|
|
5
|
+
export declare function checkAndCleanMediaFiles(ctx: Context, cfg: Config, type: 'image' | 'video' | 'file' | 'record'): Promise<void>;
|