【NAS】搭建 Chevereto 圖床 & Typora 上傳指南
【NAS】搭建 Chevereto 圖床 & Typora 上傳指南
在 Markdown 新解決方案:Typora+本地備份+GitHub 圖床 中,我給出的 Markdown 圖片管理方案是採用本地備份加上 GitHub 同步的思路;簡言之,為了保證圖片的安全性採用了本地儲存,需要進行分享的話將其中的圖片連線改為 GitHub 圖床,這種方案在穩定性上表現得很好,但是操作上到底還是有些繁瑣(需要手動上傳圖片,並執行連結替換的程式碼)。
疫情在家,這樣的方案也十分有效,最近回了學校,重新用起了 NAS,於是參考這篇文章 http://guiu.xyz/p/ab70f304.html (已失效),配合 Chevereto 探索了一套新的方案;相較於之前的那種,這裡的優勢在於配合程式碼實現了圖片的自動上傳,因此可以直接分享;但與此同時帶來的問題在於,服務依賴於圖床的穩定性,由於是自用的 NAS 所以相對來說還是可以的,用了一個多月下來體驗不錯(域名選擇的話,直接用了 Synology 免費的那個 id.synology.me 還是比較可靠的)。
搭建 Chevereto 圖床
圖床搭建的話,可以參考這一篇 https://post.smzdm.com/p/a3gvxnon/ ,沒什麼好多說的,我把當時參考的連結放在下面。
- Chevereto 的 GitHub 主頁在這裡 https://github.com/Chevereto/Chevereto-Free ;這是中文文件 https://ch.cndrew.cn/cn/Preface/Introduction/
- 另外可能需要圖片的轉移,參見 http://guiu.xyz/p/1bcf5275.html 寫的非常好
- Typora 結合 Chevereto 方案:python編寫typora外掛實現傳圖到chevereto
搭建完成之後可以收穫一個支援多賬號的線上圖床服務,可以自行探索,我不在用也就不多說了。
利用 Python 實現 Typora 圖片上傳
原始方案來自 http://guiu.xyz/p/ab70f304.html 不過網頁已失效,這裡 https://zhuanlan.zhihu.com/p/150785463 有一個備份可供參考,下面也贅述一番。
在合適的資料夾下新建一個 upload.py
檔案,內容如下(記得當時好像做了一點修改,原始的可參考上面的知乎連結)
#!/usr/bin/env python3 # -*- encoding: utf-8 -*- # author: guiu # data: 2020.2.28 import requests import json import mimetypes import argparse import sys APP_DESC = """ 一個上傳圖片到chevereto圖床的命令列工具 """ print(APP_DESC) if len(sys.argv) == 1: sys.argv.append('--help') parser = argparse.ArgumentParser() parser.add_argument('-s', '--source', type=str, nargs='+', help="", required=True) parser.add_argument('-c', '--config', default="./config.json", help="讀取配置檔案", required=True) args = parser.parse_args() # 從引數中獲取要上傳的檔案列表 img_list = args.source # print(img_list) def read_conf(path): with open(path,"r",encoding="utf-8") as f: confstr = f.read() conf = json.loads(confstr) return conf def up_to_chevereto(img_list): # 獲得本地圖片路徑後,上傳至圖床並記錄返回的json欄位 for img in img_list: # 先判斷傳過來的是本地路徑還是遠端圖片地址 if "http" == img[:4]: # 非本地圖片的話可以考慮下載到本地再上傳,但是沒這個必要 print(img) continue else: try: res_json = upload(formatSource(img)) parse_response_url(res_json,img) except: print(img+"\t上傳失敗") def upload(files): # 圖床api # APIKey = "THERE PUT YOUR APIKEY" conf = read_conf(args.config) url = conf['url'] + "?key=" + conf['APIKEY'] r = requests.post(url, files=files) return json.loads(r.text) def formatSource(filename): imageList = [] mime_type = mimetypes.guess_type(filename)[0] imageList.append( ('source', (filename, open(filename, 'rb'), mime_type)) ) #print (imageList) return imageList def parse_response_url(json, img_path): # 從返回的json中解析欄位 if json['status_code'] != 200: print("{}\tweb端返回失敗,可能是APIKey不對. status_code {} .".format( img_path, json['status_code']) ) else: img_url = json["image"]["url"] print(img_url) up_to_chevereto(img_list)
可以看到需要呼叫 Chevereto 的 APIKEY。需要在 Chevereto 的 儀表板-設定
中,在設定旁邊的下列選單中選擇 API
,即可找到 key,預設有一個也可以自定義。
新建一個 config.json
儲存配置
{
"APIKEY": "YOUR API KEY",
"url": "http://your_website/api/1/upload/"
}
注意將其中的 key 值和網址做相應的替換。
Typora 中進行相應設定
配置如下
其中的上傳程式碼為,下面是我的配置,注意在使用的時候,將 Python 地址、程式碼地址、配置檔案地址作相應的替換。
~/miniconda3/bin/python ~/SynologyDrive/Markdown/upload.py -c ~/SynologyDrive/Markdown/config.json -s
不知道為什麼直接用 python
不可以,只好寫了個全地址,上傳的速度還是可以的。