feat(image): 添加图片删除功能并优化 Markdown 编辑器

- 在 ImageController 中添加删除图片的接口
- 在 ImageService 中实现删除图片和批量更新图片 ID 的方法
- 在前端集成复制代码插件并优化 Markdown 编辑器配置
- 修复后端分组相关接口的参数类型问题
This commit is contained in:
ikmkj
2025-06-20 09:35:19 +08:00
parent df848fed23
commit 12ba82eaa1
8 changed files with 145 additions and 27 deletions

View File

@@ -31,4 +31,19 @@ public interface ImageService extends IService<Image> {
* @return 图片列表
*/
List<Image> getMarkdownImages(Long markdownId);
/**
* 根据URL删除图片
* @param url
* @return
*/
boolean deleteImageByUrl(String url);
/**
* 根据URL批量更新图片ID
* @param list
* @param markdownId
* @return
*/
boolean updateImageId(List<String> list, Long markdownId);
}

View File

@@ -1,5 +1,7 @@
package com.test.bijihoudaun.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.stream.CollectorUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -87,4 +89,38 @@ public class ImageServiceImpl
.orderByDesc("created_at");
return this.list(queryWrapper);
}
@Override
public boolean deleteImageByUrl(String url) {
Image image = imageMapper.selectOne(new QueryWrapper<Image>().eq("url", url));
if (image == null) {
return false;
}
try {
// 删除文件系统中的图片
Path filePath = Paths.get(uploadDir, image.getStoredName());
Files.deleteIfExists(filePath);
// 删除数据库记录
this.removeById(image.getId());
return true;
} catch (IOException e) {
throw new RuntimeException("删除图片失败", e);
}
}
@Override
public boolean updateImageId(List<String> list, Long markdownId) {
if (CollUtil.isEmpty( list)) {
return false;
}
for (String url : list) {
Image image = imageMapper.selectOne(new QueryWrapper<Image>().eq("url", url));
if (image != null) {
image.setMarkdownId(markdownId);
this.updateById(image);
}
}
return true;
}
}