4

炸了!3年图片都没了 - 艾小仙

 1 year ago
source link: https://www.cnblogs.com/ilovejaney/p/17125810.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一直都是用的 Typora+微博免费图床写作,前段时间突然发现图片全都裂了,打开一看都是 403 。

image-20230215120315859.png

开始我以为图片微博都给我删了,找了一堆平台去翻我的历史文章看图片是不是都在,找了半天发现图片要么不够清晰,要么就是文章不全,要么呢就是有水印,所有的原图基本上是找不回来了,跟不用说还有一些文章是没有在任何平台发表过的。

这就很难受了,所以这件事情就拖了好几个月都没处理,最近突然看到一篇文章才发现,哦,原来 403 是限制了Referrer,写代码还是可以下载下来的,试了试果然是可以的,那问题就简单了。

那我只要:扫描我的所有 md 文件=>找到所有的微博图床的图片链接=>下载所有图片=>上传到新图床=>替换图片链接,不就搞定了嘛。

扫描文件#

首先找到我的文章的目录,扫描所有的文件,把 md 文件全部找出来。

private static List<File> getFiles(String workUrl){
    return FileUtil.loopFiles(workUrl).stream()
    .filter(file -> file.isFile())
    .filter(file -> file.getName().endsWith(".md"))
    .collect(Collectors.toList());
}

获取链接#

根据参考文章里大佬写的正则,根据正则匹配到每篇文章里的链接地址,然后根据微博图床的名称过滤得到所有使用微博图床的图片地址,扫描最终得到的图片有大概 2000 张。

private static Set<String> getUrls(List<File> files)throws Exception{
    Set<String> urlList = new HashSet<>();
    Pattern pattern = Pattern.compile(
            "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" + "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov"
                    + "|mil|biz|info|mobi|name|aero|jobs|museum" + "|travel|[a-z]{2}))(:[\\d]{1,5})?"
                    + "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" + "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?"
                    + "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" + "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?"
                    + "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" + "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
    files.forEach(file->{
        String mdContent = FileReader.create(file).readString();
        Matcher matcher = pattern.matcher(mdContent);
        while(matcher.find()){
            urlList.add(matcher.group());
        }
    });

    return urlList.stream().filter(url->url.contains("sinaimg.cn")).collect(Collectors.toSet());
}

下载|上传图片#

得到所有图片地址后,直接把图片下载到本地,这里为了方便,直接截取出图片中的图片名称保存,那么之后就可以直接把图片上传到其他的图床了。

private static void download(Set<String> urls) throws Exception{
    for (String str : urls) {
        OutputStream os = null;
        InputStream is = null;
        try {
            URL url = new URL(str);
            URLConnection con = url.openConnection();
            is = con.getInputStream();
            byte[] bs = new byte[1024];
            int len;
            String fileName = str.substring(str.lastIndexOf("/")+1);
            os = Files.newOutputStream(Paths.get("/Users/user/Desktop/" + fileName));
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } finally {
            if (os != null) {
                os.close();
            }
            if (is != null) {
                is.close();
            }
        }
    }
}

这里我选择用的是阿里云 OSS,直接手动全丢进去就行了。

替换链接#

现在我的图片名称都是一样的,只要替换掉域名那部分就行了,最后写回文件。

private static void replace(List<File> files){
        //替换 md 文件中的微博图床文件
        files.forEach(file->{
            String mdContent = FileReader.create(file).readString();
            String newMDContent = mdContent.replaceAll("https://tva1.sinaimg.cn/large","http://cdn.aixiaoxian.vip/blog");
            FileWriter.create(file).write(newMDContent);
        });

    }

另外一方面就是关于费用的问题,目前因为基本都是我自己在用,一天都花了 7 分,一个月岂不是 2 块钱,有点太贵了!!!后面再看看有没有跟便宜点的。

image-20230215122346669.png

参考:https://juejin.cn/post/7189651446306963514


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK