Commit 27ac687c authored by nanahira's avatar nanahira

bump

parent 405bfde2
......@@ -36,7 +36,10 @@ app
app
.command('image')
.action(async () =>
segment.image(await fs.promises.readFile(__dirname + '/10000.jpg')),
segment.image(
await fs.promises.readFile(__dirname + '/10000.jpg'),
'image/jpeg',
),
);
app
......
This diff is collapsed.
......@@ -47,12 +47,13 @@
},
"dependencies": {
"file-type": "16.5.3",
"koishi-thirdeye": "^11.1.14",
"koishi-thirdeye": "^11.1.20",
"mime2ext": "^1.0.1",
"wechaty": "^1.20.2",
"wechaty-puppet-wechat": "^1.18.4"
},
"peerDependencies": {
"koishi": "^4.10.6"
"koishi": "^4.11.0"
},
"devDependencies": {
"@koishijs/plugin-help": "^2.0.0",
......
......@@ -4,6 +4,7 @@ import type { RoomInterface } from 'wechaty/src/user-modules/room';
import type { MessageInterface } from 'wechaty/src/user-modules/message';
import WechatyBot from './index';
import FileType from 'file-type';
import mime2ext from 'mime2ext';
export type ContactLike = Pick<
ContactInterface,
......@@ -28,7 +29,9 @@ export const fileBoxToUrl = async (file: FileBoxLike): Promise<string> => {
} catch (e) {
buf = file['stream'];
}
return `base64://${buf.toString('base64')}`;
const fileType = await FileType.fromBuffer(buf);
const mime = fileType ? fileType.mime : 'application/octet-stream';
return `data:${mime};base64,${buf.toString('base64')}`;
};
export const adaptContact = async (
......@@ -173,6 +176,16 @@ export async function autoFilename(url: string) {
const type = await FileType.fromBuffer(buf);
return `file.${type.ext}`;
}
if (url.startsWith('data:')) {
const [, mime, base64] = url.match(/^data:([^;]+);base64,(.+)$/);
const ext = mime2ext(mime);
if (ext) {
return `file.${ext}`;
}
const buf = Buffer.from(base64, 'base64');
const type = await FileType.fromBuffer(buf);
return `file.${type?.ext || 'bin'}`;
}
return path.basename(new URL(url).pathname);
}
......@@ -187,6 +200,11 @@ export const elementToFileBox = async (element: Element) => {
if (url.startsWith('base64://')) {
return FileBox.fromBase64(url.slice(9), file || (await autoFilename(url)));
}
if (url.startsWith('data:')) {
const [, mime, base64] = url.match(/^data:([^;]+);base64,(.+)$/);
const ext = mime2ext(mime) || 'bin';
return FileBox.fromBase64(base64, file || `file.${ext}`);
}
return FileBox.fromUrl(url, {
name: file || (await autoFilename(url)),
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment