picgo配置my-easy-pic-bed本地图床,并和typora连用
要解决的问题:
markdown文件插入图片时,图片的位置是个大问题。如果写相对路径,那么不利于迁移,如果写网络路径,那么就需要将图片上传到一个图床上,而各种图床有各种弊端:有的要收费,有的空间有限等等。
我的解决方案:
将图片上传到本地图床my-easy-pic-bed,使用picgo来管理上传后的图片。然后typora配置上picgo,实现插入图片自动上传。
下面是我自己实践时总结的一些关键点。当然在安装和配置过程中还有各种其他问题,这里就不详细记录了。
技术要点有:
My-Easy-Pic-Bed的改动
1. 修改配置My-Easy-Pic-Bed\config.ini 修改内容:port = 1234 。因为这个默认端口是80,所以要改。
2. 添加一个上传方法,来作为上传API提供给picgo使用。
@app.route('/uploadapi', methods=['POST', 'GET'] )
def upload_file_api():
app.logger.info('this is url')
if request.method == 'POST':
# 检查post请求中是否有文件
if 'file' not in request.files:
flash('你没有上传文件!')
return redirect(request.url)
file = request.files['file']
print(file)
imgUrl = ''
if file.filename == '':
flash('你没有选择文件!')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = str(int(time.time())) + str(random.randint(1, 99999)) + secure_filename(str(random.randint(1, 7887)) + file.filename)
try:
file.save(os.path.join(upload_folder, filename))
database = db.get_db()
database.execute(
'INSERT INTO pics (filename)'
' VALUES (?)',
(filename,)
)
database.commit()
if app.config['running_port'] != 80:
imgUrl = app.config['running_domain'] + ':' + str(app.config['running_port']) + url_for('uploaded_file', filename=filename)
flash(imgUrl)
else:
imgUrl = app.config['running_domain'] + url_for('uploaded_file', filename=filename)
flash(imgUrl)
except Exception as e:
flash('出现错误!')
print(e.args)
return jsonify({'data': 'http://'+imgUrl}),200
else:
flash('不被服务器支持的文件!')
return redirect(url_for('upload_file'))
database = db.get_db()
pcnum = database.execute("SELECT Count(*) FROM pics").fetchone()[0]
print(pcnum)
print('this is url')
print(json.dumps({'data': imgUrl}))
return jsonify({'data': imgUrl}),200
picgo的配置
1. 添加插件:自定义图床(增加图片URL前缀)
2. 配置自定义图床插件:
3.配置解释:
1)API地址是上传路径。
2)POST参数名就是API中上传的文件的参数名称例如flask中获取上传文件用file = request.files['file']。这里的POST参数名就是file。
3)JSON路径是接口返回的json参数中,图片的访问路径。这个路径会在相册中作为图片的URL,用来显示图片。注意返回的必须有http头。否则没法预览
4)API必须返回200,插件才会知道上传成功了。然后相册中才会出现图片。
4)这里的uploadapi是my-easy-pic-bed自己添加的方法,仅仅用来给picgo当做上传API。
5)uploadapi接口,返回一个json,格式是:{"data":"http://127.0.0.1:1234/uploads/1633676149386296478.png"}
4.
picgo设置自定义链接格式。
5.相册数据在picgo.db中。根据picgo源码store/DBStore.ts at dev · PicGo/store · GitHub和store/ZlibAdapter.ts 可以看到这是lowdb数据库文件LowDB静态JSON文件数据库,并且是压缩后的文件。
zlib是个著名的开源解压缩库,gzip是一种压缩文件格式。
6. picgo.db改名为picgo.gzip,然后解压缩,就可以看到相册的json数据了。