fix:白名单图片处理

上级 779aadfa
package com.kwan.springbootkwan.utils; package com.kwan.springbootkwan.utils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.StopWatch; import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
...@@ -32,24 +33,46 @@ public class FileContainsUtil { ...@@ -32,24 +33,46 @@ public class FileContainsUtil {
* 博客路径 * 博客路径
*/ */
private static final String BLOG_FOLDER = "/Users/qinyingjie/Documents/idea-workspace/blog/"; 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 stopWatch = new StopWatch();
stopWatch.start(); stopWatch.start("删除未用到的图片");
//不存在的图片集合
final List<String> isNotExist = new ArrayList<>(); final List<String> isNotExist = new ArrayList<>();
//获取picPath下面所有的文件名 //获取所有图片名称
final List<String> picNames = getPicName(PIC_PATH); final List<String> picNames = getPicName(PIC_PATH);
System.out.println("图片总数为" + picNames.size()); System.out.println("图片总数为" + picNames.size());
for (String word : picNames) { if (CollectionUtil.isNotEmpty(picNames)) {
IS_EXIST = false; for (String picName : picNames) {
//指定类型的文件 //是白名单里面的图片,直接忽略
String suffix = ".md"; boolean isWhite = false;
//包含某个字符串 for (String whitelist : WHITELISTS) {
traverseFolder(BLOG_FOLDER, suffix, word); if (whitelist.contains(picName)) {
//文件不存在 isWhite = true;
if (!IS_EXIST) { break;
isNotExist.add(word); }
deletePic(PIC_PATH + word); }
if (isWhite) {
continue;
}
//默认不存在
IS_EXIST = false;
//指定类型的文件
List<String> 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()); System.out.println("不存在图片总数为" + isNotExist.size());
...@@ -66,7 +89,7 @@ public class FileContainsUtil { ...@@ -66,7 +89,7 @@ public class FileContainsUtil {
* @param word * @param word
* @throws IOException * @throws IOException
*/ */
public static void traverseFolder(String path, String suffix, String word) throws IOException { public static void traverseFolder(String path, List<String> suffixs, String word) throws Exception {
File file = new File(path); File file = new File(path);
if (file.exists()) { if (file.exists()) {
//获取文件夹下的文件 //获取文件夹下的文件
...@@ -75,11 +98,13 @@ public class FileContainsUtil { ...@@ -75,11 +98,13 @@ public class FileContainsUtil {
for (File file2 : files) { for (File file2 : files) {
//是否是文件夹 //是否是文件夹
if (file2.isDirectory()) { if (file2.isDirectory()) {
traverseFolder(file2.getAbsolutePath(), suffix, word); traverseFolder(file2.getAbsolutePath(), suffixs, word);
} else { } else {
//包含md结尾的文件 for (String suffix : suffixs) {
if (file2.getAbsolutePath().contains(suffix)) { //包含md结尾的文件
getParams(file2.getAbsolutePath(), word); if (file2.getAbsolutePath().contains(suffix)) {
getParams(file2.getAbsolutePath(), word);
}
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册