在 Beets 2.14 中,Web 插件无法服务具有相对路径的文件
import os from pathlib import Path import flask from unidecode import unidecode from beets.plugins import BeetsPlugin from beetsplug.web import app, g
def fixed_item_file(item_id): item = g.lib.get_item(item_id) if not item: return flask.abort(404, f"Item with id {item_id} not found") item_path = Path(os.fsdecode(item.path)) if not item_path.is_absolute(): item_path = Path(os.fsdecode(g.lib.directory)) / item_path item_path = item_path.resolve() if not item_path.is_file(): return flask.abort(404, f"File not found: {item_path}") base_filename = item_path.name try: base_filename.encode("latin-1", "strict") except: return flask.abort(404, f"File name not valid: {base_filename}") return flask.send_file(item_path, as_attachment=True, filename=base_filename)
内容来源: beetbox/beets