Commit 27ac687c authored by nanahira's avatar nanahira

bump

parent 405bfde2
...@@ -36,7 +36,10 @@ app ...@@ -36,7 +36,10 @@ app
app app
.command('image') .command('image')
.action(async () => .action(async () =>
segment.image(await fs.promises.readFile(__dirname + '/10000.jpg')), segment.image(
await fs.promises.readFile(__dirname + '/10000.jpg'),
'image/jpeg',
),
); );
app app
......
This diff is collapsed.
...@@ -47,12 +47,13 @@ ...@@ -47,12 +47,13 @@
}, },
"dependencies": { "dependencies": {
"file-type": "16.5.3", "file-type": "16.5.3",
"koishi-thirdeye": "^11.1.14", "koishi-thirdeye": "^11.1.20",
"mime2ext": "^1.0.1",
"wechaty": "^1.20.2", "wechaty": "^1.20.2",
"wechaty-puppet-wechat": "^1.18.4" "wechaty-puppet-wechat": "^1.18.4"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.10.6" "koishi": "^4.11.0"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-help": "^2.0.0", "@koishijs/plugin-help": "^2.0.0",
......
...@@ -4,6 +4,7 @@ import type { RoomInterface } from 'wechaty/src/user-modules/room'; ...@@ -4,6 +4,7 @@ import type { RoomInterface } from 'wechaty/src/user-modules/room';
import type { MessageInterface } from 'wechaty/src/user-modules/message'; import type { MessageInterface } from 'wechaty/src/user-modules/message';
import WechatyBot from './index'; import WechatyBot from './index';
import FileType from 'file-type'; import FileType from 'file-type';
import mime2ext from 'mime2ext';
export type ContactLike = Pick< export type ContactLike = Pick<
ContactInterface, ContactInterface,
...@@ -28,7 +29,9 @@ export const fileBoxToUrl = async (file: FileBoxLike): Promise<string> => { ...@@ -28,7 +29,9 @@ export const fileBoxToUrl = async (file: FileBoxLike): Promise<string> => {
} catch (e) { } catch (e) {
buf = file['stream']; 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 ( export const adaptContact = async (
...@@ -173,6 +176,16 @@ export async function autoFilename(url: string) { ...@@ -173,6 +176,16 @@ export async function autoFilename(url: string) {
const type = await FileType.fromBuffer(buf); const type = await FileType.fromBuffer(buf);
return `file.${type.ext}`; 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); return path.basename(new URL(url).pathname);
} }
...@@ -187,6 +200,11 @@ export const elementToFileBox = async (element: Element) => { ...@@ -187,6 +200,11 @@ export const elementToFileBox = async (element: Element) => {
if (url.startsWith('base64://')) { if (url.startsWith('base64://')) {
return FileBox.fromBase64(url.slice(9), file || (await autoFilename(url))); 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, { return FileBox.fromUrl(url, {
name: file || (await autoFilename(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