feat(image): 添加图片删除功能并优化 Markdown 编辑器
- 在 ImageController 中添加删除图片的接口 - 在 ImageService 中实现删除图片和批量更新图片 ID 的方法 - 在前端集成复制代码插件并优化 Markdown 编辑器配置 - 修复后端分组相关接口的参数类型问题
This commit is contained in:
@@ -35,17 +35,20 @@ public class GroupingController {
|
||||
@Operation(summary = "更新分组名称")
|
||||
@PutMapping("/{id}")
|
||||
public R<Grouping> updateGrouping(
|
||||
@PathVariable Long id,
|
||||
@PathVariable String id,
|
||||
@RequestBody Grouping grouping) {
|
||||
grouping.setId(id);
|
||||
|
||||
long l = Long.parseLong(id);
|
||||
grouping.setId(l);
|
||||
Grouping updated = groupingService.updateGrouping(grouping);
|
||||
return R.success(updated);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除分组")
|
||||
@DeleteMapping("/{id}")
|
||||
public R<Void> deleteGrouping(@PathVariable Long id) {
|
||||
groupingService.deleteGrouping(id);
|
||||
public R<Void> deleteGrouping(@PathVariable String id) {
|
||||
Long idLong = Long.parseLong(id);
|
||||
groupingService.deleteGrouping(idLong);
|
||||
return R.success();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "markdown接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/images")
|
||||
@@ -28,14 +30,39 @@ public class ImageController {
|
||||
})
|
||||
@PostMapping
|
||||
public R<Image> uploadImage(
|
||||
@RequestParam(required = false) Long markdownId,
|
||||
@RequestParam(required = false) String markdownId,
|
||||
@RequestParam("file") MultipartFile file) {
|
||||
|
||||
try {
|
||||
Image image = imageService.uploadImage(markdownId, file);
|
||||
Long markdownIdLong = Long.parseLong(markdownId);
|
||||
Image image = imageService.uploadImage(markdownIdLong, file);
|
||||
return R.success(image);
|
||||
} catch (IOException e) {
|
||||
return R.fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "根据id删除图片")
|
||||
@DeleteMapping("/{id}")
|
||||
public R<Void> deleteImage(@PathVariable Long id) {
|
||||
boolean result = imageService.deleteImage(id);
|
||||
if (result) {
|
||||
return R.success();
|
||||
} else {
|
||||
return R.fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "根据url删除图片")
|
||||
@DeleteMapping
|
||||
public R<Void> deleteImageByUrl(@RequestParam String url) {
|
||||
boolean result = imageService.deleteImageByUrl(url);
|
||||
if (result) {
|
||||
return R.success();
|
||||
} else {
|
||||
return R.fail();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user