PicInfoServiceImpl.java 3.9 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 51
                    final String picNameOrigin = filePath.getFileName().toString();
                    String picPath = PRE_FIX + picNameOrigin;
                    final String picName = picNameOrigin.split(".")[0];
52
                    this.insertPic(type, picPath, picName);
53 54 55 56 57 58 59 60
                }
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
61 62 63 64 65 66 67

    /**
     * 插入图片
     *
     * @param picPath
     * @param picName
     */
68
    private void insertPic(Integer type, String picPath, String picName) {
69 70 71 72 73 74 75 76 77 78
        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);
79
        picInfo.setType(type);
80 81 82 83
        picInfoMapper.insert(picInfo);
    }

    @Override
84
    public boolean insertByBaiduUrl(String url, Integer type) {
85 86 87 88 89 90 91 92 93
        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();
94 95 96 97 98 99
                final String fromPageTitle = datum.getFromPageTitle();
                if (StringUtils.isNotEmpty(fromPageTitle)) {
                    String[] split = fromPageTitle.split(".");
                    if (Objects.isNull(split) || split.length == 0) {
                        split = fromPageTitle.split(",");
                    }
100
                    this.insertPic(type, hoverURL, Objects.isNull(split) || split.length == 0 ? fromPageTitle : split[0]);
101
                }
102 103 104 105 106 107 108
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
109 110
}