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

3 4
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
5
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
6
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7
import com.fasterxml.jackson.databind.ObjectMapper;
8
import com.kwan.springbootkwan.entity.PicInfo;
9 10
import com.kwan.springbootkwan.entity.pic.BaiduPic;
import com.kwan.springbootkwan.entity.pic.PicDataInfo;
11 12
import com.kwan.springbootkwan.mapper.PicInfoMapper;
import com.kwan.springbootkwan.service.PicInfoService;
13
import lombok.extern.slf4j.Slf4j;
14
import org.apache.commons.lang3.StringUtils;
15
import org.springframework.beans.factory.annotation.Autowired;
16 17
import org.springframework.stereotype.Service;

18 19 20 21 22
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
23
import java.util.List;
24 25
import java.util.Objects;

26 27 28 29 30 31
/**
 * 图片信息表(PicInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-08-09 12:44:03
 */
32
@Slf4j
33 34 35
@Service("picInfoService")
public class PicInfoServiceImpl extends ServiceImpl<PicInfoMapper, PicInfo> implements PicInfoService {

36 37 38 39 40 41 42 43
    @Autowired
    private PicInfoMapper picInfoMapper;
    /**
     * 图片路径前缀
     */
    private static final String PRE_FIX = "https://gitcode.net/qyj19920704/baby-images/-/raw/main/";

    @Override
44
    public boolean insertByPath(String path, Integer type) {
45 46 47 48
        Path directoryPath = Paths.get(path);
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directoryPath)) {
            for (Path filePath : directoryStream) {
                if (Files.isRegularFile(filePath)) {
49 50
                    final String picNameOrigin = filePath.getFileName().toString();
                    String picPath = PRE_FIX + picNameOrigin;
51 52 53 54 55
                    final String[] split = picNameOrigin.split(".");
                    String picName = picNameOrigin;
                    if (Objects.nonNull(split) && split.length > 0) {
                        picName = split[0];
                    }
56
                    this.insertPic(type, picPath, picName);
57 58 59 60 61 62 63 64
                }
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
65 66 67 68 69 70 71

    /**
     * 插入图片
     *
     * @param picPath
     * @param picName
     */
72
    private void insertPic(Integer type, String picPath, String picName) {
73 74 75 76 77 78 79 80 81 82
        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)) {
            return;
        }
        PicInfo picInfo = new PicInfo();
        picInfo.setPicName(picName);
        picInfo.setPicUrl(picPath);
83
        picInfo.setType(type);
84 85 86 87
        picInfoMapper.insert(picInfo);
    }

    @Override
88
    public boolean insertByBaiduUrl(String url, Integer type) {
89 90 91 92 93 94 95 96 97
        HttpResponse response;
        try {
            response = HttpUtil.createGet(url).execute();
            String responseBody = response.body();
            ObjectMapper objectMapper = new ObjectMapper();
            BaiduPic baiduPic = objectMapper.readValue(responseBody, BaiduPic.class);
            final List<PicDataInfo> data = baiduPic.getData();
            for (PicDataInfo datum : data) {
                final String hoverURL = datum.getHoverURL();
98 99 100 101 102 103
                final String fromPageTitle = datum.getFromPageTitle();
                if (StringUtils.isNotEmpty(fromPageTitle)) {
                    String[] split = fromPageTitle.split(".");
                    if (Objects.isNull(split) || split.length == 0) {
                        split = fromPageTitle.split(",");
                    }
104
                    this.insertPic(type, hoverURL, Objects.isNull(split) || split.length == 0 ? fromPageTitle : split[0]);
105
                }
106 107 108 109 110 111 112
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
113 114
}