fix:插入图片

上级 0e4d0ff7
此差异已折叠。
......@@ -42,7 +42,7 @@ public class PicInfoController {
/**
* 分页查询所有数据
* 分页查询图片
*
* @return 所有数据
*/
......@@ -91,6 +91,19 @@ public class PicInfoController {
return Result.ok(this.picInfoService.save(picInfo));
}
/**
* 新增图片
*
* @param path
* @return
*/
@PostMapping(value = "/insertByPath")
public Result insertByPath(@RequestParam String path) {
return Result.ok(this.picInfoService.insertByPath(path));
}
/**
* 修改数据
*
......
......@@ -11,5 +11,6 @@ import com.kwan.springbootkwan.entity.PicInfo;
*/
public interface PicInfoService extends IService<PicInfo> {
boolean insertByPath(String path);
}
package com.kwan.springbootkwan.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kwan.springbootkwan.entity.PicInfo;
import com.kwan.springbootkwan.mapper.PicInfoMapper;
import com.kwan.springbootkwan.service.PicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
/**
* 图片信息表(PicInfo)表服务实现类
*
......@@ -15,5 +24,39 @@ import org.springframework.stereotype.Service;
@Service("picInfoService")
public class PicInfoServiceImpl extends ServiceImpl<PicInfoMapper, PicInfo> implements PicInfoService {
@Autowired
private PicInfoMapper picInfoMapper;
/**
* 图片路径前缀
*/
private static final String PRE_FIX = "https://gitcode.net/qyj19920704/baby-images/-/raw/main/";
@Override
public boolean insertByPath(String path) {
Path directoryPath = Paths.get(path);
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directoryPath)) {
for (Path filePath : directoryStream) {
if (Files.isRegularFile(filePath)) {
final String picName = filePath.getFileName().toString();
String picPath = PRE_FIX + picName;
QueryWrapper<PicInfo> wrapper = new QueryWrapper<>();
wrapper.eq("pic_url", picPath);
wrapper.eq("is_delete", 0);
final PicInfo pic = picInfoMapper.selectOne(wrapper);
if(Objects.nonNull(pic)){
continue;
}
PicInfo picInfo = new PicInfo();
picInfo.setPicName(picName);
picInfo.setPicUrl(picPath);
picInfoMapper.insert(picInfo);
}
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册