PicInfoServiceImpl.java 3.4 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.springframework.beans.factory.annotation.Autowired;
15 16
import org.springframework.stereotype.Service;

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

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

35 36 37 38 39 40 41 42 43 44 45 46 47
    @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)) {
48 49 50 51
                    final String picNameOrigin = filePath.getFileName().toString();
                    String picPath = PRE_FIX + picNameOrigin;
                    final String picName = picNameOrigin.split(".")[0];
                    this.insertPic(picPath, picName);
52 53 54 55 56 57 58 59
                }
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    /**
     * 插入图片
     *
     * @param picPath
     * @param picName
     */
    private void insertPic(String picPath, String 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)) {
            return;
        }
        PicInfo picInfo = new PicInfo();
        picInfo.setPicName(picName);
        picInfo.setPicUrl(picPath);
        picInfoMapper.insert(picInfo);
    }

    @Override
    public boolean insertByBaiduUrl(String url) {
        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();
                final String picName = datum.getFromPageTitle().split(".")[0];
                this.insertPic(hoverURL, picName);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
101 102
}