Skip to content

Commit

Permalink
feat: image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
LetTTGACO committed Jun 15, 2024
1 parent 48c1c20 commit 5a6e9c9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/elog/src/types/image.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
28 changes: 23 additions & 5 deletions packages/elog/src/utils/doc/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ export const transformImagesFunc = async (options: TransformImageOptions) => {
const promise = async (image: ImageUrl): Promise<ImageSource | undefined> => {
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;
}
// 完整文件名
Expand All @@ -76,15 +80,29 @@ 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;
}
// 上传图片
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);
Expand Down
26 changes: 22 additions & 4 deletions packages/elog/src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 中获取
Expand Down

0 comments on commit 5a6e9c9

Please sign in to comment.