feat(image): 实现 Markdown 图片文件名同步

- 新增 ImageName 实体类和对应的 Mapper- 在 MarkdownFileService 中添加图片文件名同步方法
- 优化 HomePage 组件,支持实时预览 Markdown 内容
- 新增 MarkdownImageExtractor 工具类,用于提取 Markdown 中的图片文件名
This commit is contained in:
ikmkj
2025-08-01 22:25:36 +08:00
parent 165bd5ea92
commit 15091c315e
10 changed files with 271 additions and 27 deletions

View File

@@ -1,15 +1,20 @@
package com.test.bijihoudaun.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.bijihoudaun.entity.ImageName;
import com.test.bijihoudaun.entity.MarkdownFile;
import com.test.bijihoudaun.entity.MarkdownFileVO;
import com.test.bijihoudaun.mapper.ImageNameMapper;
import com.test.bijihoudaun.mapper.MarkdownFileMapper;
import com.test.bijihoudaun.service.MarkdownFileService;
import com.test.bijihoudaun.util.MarkdownImageExtractor;
import com.test.bijihoudaun.util.SnowflakeIdGenerator;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.Date;
@@ -23,20 +28,30 @@ public class MarkdownFileServiceImpl
@Autowired
MarkdownFileMapper markdownFileMapper;
@Resource
ImageNameMapper imageNameMapper;
@Resource
SnowflakeIdGenerator snowflakeIdGenerator;
@Override
public MarkdownFile updateMarkdownContent(MarkdownFile markdownFile) {
long id;
markdownFile.setUpdatedAt(new Date());
// 如果ID为空或0则视为新文件
if (markdownFile.getId() == null || markdownFile.getId() == 0L) {
markdownFile.setId(snowflakeIdGenerator.nextId());
long l = snowflakeIdGenerator.nextId();
markdownFile.setId(l);
markdownFile.setCreatedAt(new Date());
this.save(markdownFile); // 使用MyBatis-Plus的save方法
id=l;
} else {
this.updateById(markdownFile); // 使用MyBatis-Plus的updateById方法
id=markdownFile.getId();
}
List<String> strings = MarkdownImageExtractor.extractImageFilenames(markdownFile.getContent());
// 异步处理图片文件名同步
syncImageNames(id, strings);
return markdownFile;
}
@@ -62,8 +77,7 @@ public class MarkdownFileServiceImpl
@Override
public List<MarkdownFile> test() {
List<MarkdownFile> markdownFiles = markdownFileMapper.selectList(null);
return markdownFiles;
return markdownFileMapper.selectList(null);
}
@Override
@@ -93,4 +107,63 @@ public class MarkdownFileServiceImpl
public List<MarkdownFileVO> getRecentFiles(int limit) {
return markdownFileMapper.selectRecentWithGrouping(limit);
}
@Async("imageNameSyncExecutor")
public void syncImageNames(Long markdownId, List<String> strings) {
// 查询数据库中已存在的文件名
List<ImageName> imageNames = imageNameMapper.selectList(new LambdaUpdateWrapper<ImageName>()
.eq(ImageName::getMarkdownId, markdownId));
// 若是数据库中的数据为null则插入
if (CollUtil.isEmpty(imageNames)) {
if (CollUtil.isNotEmpty(strings)) {
List<ImageName> list = strings.stream().map(fileName -> {
ImageName imageName = new ImageName();
imageName.setFileName(fileName);
imageName.setMarkdownId(markdownId);
return imageName;
}).toList();
// 批量插入新的文件名
list.forEach(imageName -> imageNameMapper.insert(imageName));
}
} else {
// 数据库中已有记录,需要对比处理
// 获取数据库中的文件名列表
List<String> dbFileNames = imageNames.stream()
.map(ImageName::getFileName)
.toList();
// 找出需要新增的文件名在strings中但不在数据库中
List<String> toInsert = strings.stream()
.filter(fileName -> !dbFileNames.contains(fileName))
.toList();
// 找出需要删除的记录在数据库中但不在strings中
List<ImageName> toDelete = imageNames.stream()
.filter(imageName -> !strings.contains(imageName.getFileName()))
.toList();
// 插入新增的文件名
if (CollUtil.isNotEmpty(toInsert)) {
List<ImageName> insertList = toInsert.stream().map(fileName -> {
ImageName imageName = new ImageName();
imageName.setFileName(fileName);
imageName.setMarkdownId(markdownId);
return imageName;
}).toList();
imageNameMapper.insert(insertList);
}
// 删除不再需要的记录
if (CollUtil.isNotEmpty(toDelete)) {
List<Long> deleteIds = toDelete.stream()
.map(ImageName::getId)
.toList();
imageNameMapper.deleteByIds(deleteIds);
}
}
}
}