PicInfoServiceImpl.java 2.2 KB
Newer Older
1 2
package com.kwan.springbootkwan.service.impl;

3
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 5 6 7
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;
8
import org.springframework.beans.factory.annotation.Autowired;
9 10
import org.springframework.stereotype.Service;

11 12 13 14 15 16 17
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;

18 19 20 21 22 23 24 25 26
/**
 * 图片信息表(PicInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-08-09 12:44:03
 */
@Service("picInfoService")
public class PicInfoServiceImpl extends ServiceImpl<PicInfoMapper, PicInfo> implements PicInfoService {

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    @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);
46
                    if (Objects.nonNull(pic)) {
47 48 49
                        continue;
                    }
                    PicInfo picInfo = new PicInfo();
50
                    picInfo.setPicName(picName.split(".")[0]);
51 52 53 54 55 56 57 58 59 60
                    picInfo.setPicUrl(picPath);
                    picInfoMapper.insert(picInfo);
                }
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
61 62
}