diff --git a/packages/elog/src/types/image.ts b/packages/elog/src/types/image.ts index e9922324..b46e1f3b 100644 --- a/packages/elog/src/types/image.ts +++ b/packages/elog/src/types/image.ts @@ -1,10 +1,10 @@ import { DocDetail } from './doc'; export interface ImageUrl { - /** 更新后的 url */ - url: string; + data: string; /** 原始 url*/ originalUrl: string; + type: 'url' | 'base64'; } export interface ImageSource { diff --git a/packages/elog/src/utils/doc/image.ts b/packages/elog/src/utils/doc/image.ts index 9289f472..145e3b91 100644 --- a/packages/elog/src/utils/doc/image.ts +++ b/packages/elog/src/utils/doc/image.ts @@ -56,11 +56,15 @@ export const transformImagesFunc = async (options: TransformImageOptions) => { const promise = async (image: ImageUrl): Promise => { try { // 生成文件名 - const fileName = genUniqueIdFromUrl(image.url); + const fileName = genUniqueIdFromUrl(image.data); // 生成文件名后缀 - const fileType = await getFileType(image.url); + const fileType = await getFileType(image.data); if (!fileType) { - out.warn(`${doc?.properties?.title} 存在获取图片类型失败,跳过:${image.url}`); + if (image.type === 'url') { + out.warn(`${doc?.properties?.title} 存在获取图片类型失败,跳过:${image.data}`); + } else { + out.warn(`${doc?.properties?.title} 存在获取图片类型失败`); + } return undefined; } // 完整文件名 @@ -76,7 +80,17 @@ export const transformImagesFunc = async (options: TransformImageOptions) => { url: exist, }; } else { - const buffer = await getBufferFromUrl(image.originalUrl); + let buffer = null; + if (image.type === 'base64') { + // base64 转 buffer + const buffer = Buffer.from(image.data, 'base64'); + if (!buffer) { + failBack?.(image); + return undefined; + } + } else { + buffer = await getBufferFromUrl(image.originalUrl); + } if (!buffer) { failBack?.(image); return undefined; @@ -84,7 +98,11 @@ export const transformImagesFunc = async (options: TransformImageOptions) => { // 上传图片 let url = await imageClient.uploadImage(fileName, buffer); if (!url) { - out.warn(`${doc?.properties?.title} 存在上传图片失败:${image.url}`); + if (image.type === 'url') { + out.warn(`${doc?.properties?.title} 存在上传图片失败:${image.data}`); + } else { + out.warn(`${doc?.properties?.title} 存在上传图片失败`); + } url = image.originalUrl; } else { out.info('上传成功', url); diff --git a/packages/elog/src/utils/image.ts b/packages/elog/src/utils/image.ts index 1c488524..5d7ea584 100644 --- a/packages/elog/src/utils/image.ts +++ b/packages/elog/src/utils/image.ts @@ -72,18 +72,26 @@ export const cleanUrlParam = (originalUrl: string) => { * 从 md 文档获取图片链接列表 * @param content */ -export const getUrlListFromContent = (content: string) => { +export const getUrlListFromContent = (content: string): ImageUrl[] => { return (content.match(/!\[[^\]]*\]\(([^)]+)\)/g) || []) .map((item: string) => { const res = item.match(/\!\[.*\]\((.*?)( ".*")?\)/); if (res) { - const url = res[1]; + let url = res[1]; // 过滤 Base64 图片 - if (url.startsWith('data:')) return undefined; + if (url.startsWith('data:')) { + return { + originalUrl: url, + data: url, + type: 'buffer', + }; + } // 去除#?号 + url = cleanUrlParam(url); return { originalUrl: url, - url: cleanUrlParam(url), + data: url, + type: 'url', }; } return undefined; @@ -120,6 +128,16 @@ export const genUniqueIdFromUrl = (url: string, length?: number) => { * @param url */ export const getFileType = async (url: string) => { + // 从 base64 中获取文件类型 + if (url.startsWith('data:')) { + const base64Reg = /^data:image\/(\w+);base64,/; + const res = url.match(base64Reg); + if (res) { + return { + type: res[1], + }; + } + } let fileType: FileType | undefined = getFileTypeFromUrl(url, false); if (!fileType) { // 尝试从 buffer 中获取