FileContainsUtil.java 6.6 KB
Newer Older
1 2
package com.kwan.springbootkwan.utils;

3
import cn.hutool.core.collection.CollectionUtil;
4 5 6 7
import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson2.JSON;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
8
import lombok.extern.slf4j.Slf4j;
9 10 11 12

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
13
import java.util.Arrays;
14 15 16
import java.util.List;

/**
17
 * 删除博客中不存在的照片
18 19 20 21 22
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2023/2/8 10:45
 */
23
@Slf4j
24 25
public class FileContainsUtil {
    /**
26
     * 默认图片不存在
27 28 29 30 31 32
     */
    private static boolean IS_EXIST = false;
    /**
     * 图片路径
     */
    private static final String PIC_PATH = "/Users/qinyingjie/Documents/idea-workspace/blogimg/";
33 34 35 36
    /**
     * 宝宝图片路径
     */
    private static final String BABY_PIC_PATH = "/Users/qinyingjie/Documents/idea-workspace/study/baby-images";
37
    /**
38
     * 博客路径1
39
     */
40
    private static final String BLOG_FOLDER1 = "/Users/qinyingjie/Documents/vscode-workspace/blog/";
41 42 43 44
    /**
     * 博客路径2
     */
    private static final String BLOG_FOLDER2 = "/Users/qinyingjie/Documents/idea-workspace/study/blog/";
45 46 47 48
    /**
     * 博客路径3
     */
    private static final String BLOG_FOLDER3 = "/Users/qinyingjie/Documents/idea-workspace/study/belle_blog/";
49
    /**
50
     * 图片白名单
51
     */
52
    private static final List<String> PIC_PATH_WHITELISTS = Arrays.asList(
53 54 55
            "http://qinyingjie.top/blogImg/image-20230324112725149.png"
            , "http://qinyingjie.top/blogImg/logo.png"
            , "http://qinyingjie.top/blogImg/image-20230601124308164.png"
56
            , "http://qinyingjie.top/blogImg/image-20230822102858692.png"
57
    );
58

59
    public static void main(String[] args) throws Exception {
60
        StopWatch stopWatch = new StopWatch();
61 62
        stopWatch.start("删除未用到的图片");
        //不存在的图片集合
63
        final List<String> isNotExist = new ArrayList<>();
64
        //获取所有图片名称
65
        final List<String> picNames = getPicName(PIC_PATH);
66 67 68 69
        //宝宝图片
        final List<String> babyPicNames = getPicName(BABY_PIC_PATH);
        final int size = picNames.size();
        log.info("图片总数为{}", size);
70
        if (CollectionUtil.isNotEmpty(picNames)) {
71
            outerloop:
72
            for (int i = 0; i < size; i++) {
73
                String picName = picNames.get(i);
74
                //是白名单里面的图片,直接忽略
75 76 77 78
                for (String picPathWhitelist : PIC_PATH_WHITELISTS) {
                    if (picPathWhitelist.contains(picName)) {
                        continue outerloop;
                    }
79
                }
80 81 82
                if (babyPicNames.contains(picName)) {
                    continue outerloop;
                }
83 84 85 86 87 88
                //默认不存在
                IS_EXIST = false;
                //指定类型的文件
                List<String> suffix = new ArrayList<>();
                suffix.add(".md");
                //包含某个字符串
89 90
                traverseFolder(BLOG_FOLDER1, suffix, picName);
                traverseFolder(BLOG_FOLDER2, suffix, picName);
91
                traverseFolder(BLOG_FOLDER3, suffix, picName);
92 93 94 95 96
                //文件不存在
                if (!IS_EXIST) {
                    isNotExist.add(picName);
                    deletePic(PIC_PATH + picName);
                }
97 98
            }
        }
99
        log.info("不存在图片总数为{}", isNotExist.size());
100 101
        stopWatch.stop();
        //毫秒输出
102 103 104
        log.info("耗时统计信息:{}", JSON.toJSONString(stopWatch.getTaskInfo()));
        log.info("耗时秒数:{}", JSON.toJSONString(stopWatch.getTotalTimeSeconds()));
        log.info("耗时分钟数:{}", JSON.toJSONString(stopWatch.getTotalTimeSeconds() / 60));
105 106 107 108 109 110
    }

    /**
     * 获取文件
     *
     * @param path
111
     * @param suffixs
112 113 114
     * @param word
     * @throws IOException
     */
115
    public static void traverseFolder(String path, List<String> suffixs, String word) throws Exception {
116 117 118 119 120 121 122 123
        File file = new File(path);
        if (file.exists()) {
            //获取文件夹下的文件
            File[] files = file.listFiles();
            if (null != files && files.length != 0) {
                for (File file2 : files) {
                    //是否是文件夹
                    if (file2.isDirectory()) {
124
                        traverseFolder(file2.getAbsolutePath(), suffixs, word);
125
                    } else {
126 127 128 129 130
                        for (String suffix : suffixs) {
                            //包含md结尾的文件
                            if (file2.getAbsolutePath().contains(suffix)) {
                                getParams(file2.getAbsolutePath(), word);
                            }
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
                        }
                    }
                }
            }
        }
    }

    /**
     * 判断文件是否存在
     *
     * @param classPath
     * @param word
     * @throws IOException
     */
    public static void getParams(String classPath, String word) throws IOException {
        File file = new File(classPath);
        //每行作为一个字符串,存为列表元素
        List<String> strings = Files.readLines(file, Charsets.UTF_8);
        for (String string : strings) {
            //判断是否包含方法名称,即指定字符串
            if (string.contains(word)) {
                //文件存在
                IS_EXIST = true;
            }
        }
    }

    /**
     * 获取图片名称
     *
     * @param path
     * @return
     */
    public static List<String> getPicName(String path) {
        List<String> picNames = new ArrayList<>();
        File file = new File(path);
        if (file.exists()) {
            //获取文件夹下的文件
            File[] files = file.listFiles();
            if (null != files && files.length != 0) {
                for (File file2 : files) {
                    //是否是文件夹
                    if (!file2.isDirectory()) {
                        //包含md结尾的文件
                        final String name = file2.getName();
                        picNames.add(name);
                    }
                }
            }
        }
        return picNames;
    }

    /**
     * 删除文件
     *
     * @param picPath
     */
    public static void deletePic(String picPath) {
        File file = new File(picPath);
        try {
            file.delete();
            System.out.printf("删除文件成功:%s%n", picPath);
        } catch (Exception e) {
            System.err.printf("无法删除的路径 %s%n%s", picPath, e);
        }
    }
}