diff --git a/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java b/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java index 3d152f9239e68f82cbc8445dc7a6ce46078306aa..c35c51dcd70dd24bbc0099416111f98fdeb0dcba 100644 --- a/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java +++ b/src/main/java/com/kwan/springbootkwan/utils/FileContainsUtil.java @@ -1,6 +1,7 @@ package com.kwan.springbootkwan.utils; +import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.StopWatch; import com.alibaba.fastjson2.JSON; import com.google.common.base.Charsets; @@ -32,24 +33,46 @@ public class FileContainsUtil { * 博客路径 */ private static final String BLOG_FOLDER = "/Users/qinyingjie/Documents/idea-workspace/blog/"; + /** + * 白名单 + */ + private static final String[] WHITELISTS = {"http://qinyingjie.top/blogImg/image-20230324112725149.png"}; - public static void main(String[] args) throws IOException { + + public static void main(String[] args) throws Exception { StopWatch stopWatch = new StopWatch(); - stopWatch.start(); + stopWatch.start("删除未用到的图片"); + //不存在的图片集合 final List isNotExist = new ArrayList<>(); - //获取picPath下面所有的文件名 + //获取所有图片名称 final List picNames = getPicName(PIC_PATH); System.out.println("图片总数为" + picNames.size()); - for (String word : picNames) { - IS_EXIST = false; - //指定类型的文件 - String suffix = ".md"; - //包含某个字符串 - traverseFolder(BLOG_FOLDER, suffix, word); - //文件不存在 - if (!IS_EXIST) { - isNotExist.add(word); - deletePic(PIC_PATH + word); + if (CollectionUtil.isNotEmpty(picNames)) { + for (String picName : picNames) { + //是白名单里面的图片,直接忽略 + boolean isWhite = false; + for (String whitelist : WHITELISTS) { + if (whitelist.contains(picName)) { + isWhite = true; + break; + } + } + if (isWhite) { + continue; + } + //默认不存在 + IS_EXIST = false; + //指定类型的文件 + List suffix = new ArrayList<>(); + suffix.add(".md"); + suffix.add(".js"); + //包含某个字符串 + traverseFolder(BLOG_FOLDER, suffix, picName); + //文件不存在 + if (!IS_EXIST) { + isNotExist.add(picName); + deletePic(PIC_PATH + picName); + } } } System.out.println("不存在图片总数为" + isNotExist.size()); @@ -66,7 +89,7 @@ public class FileContainsUtil { * @param word * @throws IOException */ - public static void traverseFolder(String path, String suffix, String word) throws IOException { + public static void traverseFolder(String path, List suffixs, String word) throws Exception { File file = new File(path); if (file.exists()) { //获取文件夹下的文件 @@ -75,11 +98,13 @@ public class FileContainsUtil { for (File file2 : files) { //是否是文件夹 if (file2.isDirectory()) { - traverseFolder(file2.getAbsolutePath(), suffix, word); + traverseFolder(file2.getAbsolutePath(), suffixs, word); } else { - //包含md结尾的文件 - if (file2.getAbsolutePath().contains(suffix)) { - getParams(file2.getAbsolutePath(), word); + for (String suffix : suffixs) { + //包含md结尾的文件 + if (file2.getAbsolutePath().contains(suffix)) { + getParams(file2.getAbsolutePath(), word); + } } } }