一坨屎一样的代码 Typora 去除无用本地图片+同步图片到 github 本地 Repository

2018-11-11 19:52:08 +08:00
 KingEngine

一坨屎一样的代码 Typora 去除无用本地图片+同步图片到 github 本地 Repository

接上次帖子

感谢 10 楼的 idea !

目的去除 md 笔记目录下的无用图片,顺便将图片同步到 github 本地仓库,方便提交做远程……

使用粗鲁的死循环,可打成 jar 用 cmd 运行…对于需要的人有点用,否则就看看热闹。

public class Main {
    static ArrayList<String> list = new ArrayList<>();
    static StringBuilder text = new StringBuilder();
    static String sycDir="D:\\坚果云\\CodeNotes"; //笔记本同步路径
    static String hubDir="D:\\github\\assets";  //github 库本地路径
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("正在处理中……");
        while (true) {
            mainClear();
            sleep(30000); //间隔时间
        }
    }
    public static void mainClear() throws IOException {
        ArrayList<String> picList = new ArrayList<>();
        File file = new File(sycDir);
        func(file);
        func(new File(hubDir));
        String ContentArea = text.toString();
        String regex = "\\(assets/.*?\\.\\w+\\)";
        Pattern pt = Pattern.compile(regex);
        Matcher mt = pt.matcher(ContentArea);
        while (mt.find()) {
            String replace = mt.group().replace("(assets/", "").replace(")", "");
            picList.add(replace);
        }
        for (String s : list) {
            String fileName = s.substring(s.lastIndexOf("\\") + 1, s.length());//获取文件名
            int count = 0;
            for (String pic : picList) {
                if (pic.equals(fileName)) {
                    count++;
                }
            }
            if (count <= 0) {
                System.out.println(s+"已放入"+sycDir+"回收站");
                String hs = file + "\\回收站\\";
                if (!new File(hs).exists())
                    new File(hs).mkdir();
                if (!new File(s).renameTo(new File(hs + fileName))) {
                    new File(hs + fileName).delete();
                    new File(s).renameTo(new File(hs + fileName));
                }
            }
        }
        text = new StringBuilder();
        list.clear();
    }
    private static void func(File file) throws IOException {
        File[] fs = file.listFiles();
        String fName;
        for (File f : fs) {
            if (f.isDirectory() && !f.toString().contains("回收站") && !f.toString().contains(".git"))     //排除回收站目录
                func(f);
            if (f.isFile()) {      //若是文件,直接打印详细路径
                String s = f.toString();
                if (s.endsWith(".md")) {//获取 md 文件内容
                    text.append(readToString(s));
                } else {
                    list.add(s);
                    if (s.contains(sycDir) && !s.contains(hubDir)) {
                        fName = hubDir +"\\"+ s.substring(s.lastIndexOf("\\") + 1, s.length());
                        if (!new File(fName).exists()) {
                            System.out.println(s + "已同步");
                            Files.copy(new File(s).toPath(), new File(fName).toPath());
                        }
                    }
                }
            }
        }
    }
    public static String readToString(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        StringBuilder txt = new StringBuilder();
        while ((line = br.readLine()) != null) {
            txt.append(line);
        }
        br.close();
        return txt.toString();
    }
}

效果图:

4161 次点击
所在节点    程序员
4 条回复
KingEngine
2018-11-11 20:04:31 +08:00
至于为什么图片文件都储存在相对目录./assets 下只是因为安卓上“易码” app 可以直接阅读坚果云 assets 下图片而已,方便,结
ThirdFlame
2018-11-11 20:16:08 +08:00
试试 vnote+git 同步?
xml123
2018-11-11 20:53:01 +08:00
vnote 了解一下,自动清理无用图片
KingEngine
2018-11-11 21:11:45 +08:00
@ThirdFlame
@xml123
或许我会试试的,目前暂用

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/506772

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX