Web plugin cannot serve files with relative paths in Beets 2.14
Web plugin cannot serve files with relative paths in Beets 2.14
Description
With Beets 2.14.1, the Web plugin can fail to serve media files when the library database stores paths relative to the configured library directory.
The /item/<id>/file endpoint passes item.path directly to flask.send_file(). In the Web request context, item.path is relative, so Flask resolves it relative to the Web plugin's working directory rather than Beets' configured library directory.
This results in a FileNotFoundError.
Environment
- Beets: 2.14.1
- Python: 3.12.14
- OS: Linux
- Beets Web plugin
- Library directory:
/music - Library database:
/config/musiclibrary.blb
Configuration
plugins: musicbrainz fetchart embedart convert scrub replaygain lastgenre chroma web picardnaming
pluginpath:
- /config/beetsplug
directory: /music
library: /config/musiclibrary.blb
web:
host: 0.0.0.0
port: 8337
include_paths: true
cors: '*'Reproduction
The database contains a relative path:
A/Alleviate/EP/(2022) About Love [Digital Media - FLAC]/01. About Love (Original Mix) [9m44s] [16bit 44.1KHz 2ch].flacThe actual file exists at:
/music/A/Alleviate/EP/(2022) About Love [Digital Media - FLAC]/01. About Love (Original Mix) [9m44s] [16bit 44.1KHz 2ch].flacThe Web API correctly reports the relative path, but requesting:
/item/2406/fileresults in the Web plugin attempting to open:
/lsiopy/lib/python3.12/site-packages/beetsplug/web/A/Alleviate/EP/(2022) About Love [Digital Media - FLAC]/...instead of:
/music/A/Alleviate/EP/(2022) About Love [Digital Media - FLAC]/...I also confirmed this inside the live Web request handler:
WEB DEBUG item.path:
b'A/Alleviate/EP/(2022) About Love [Digital Media - FLAC]/...'
WEB DEBUG item.filepath:
PosixPath('A/Alleviate/EP/(2022) About Love [Digital Media - FLAC]/...')
WEB DEBUG item_path:
'A/Alleviate/EP/(2022) About Love [Digital Media - FLAC]/...'So using item.filepath does not solve the problem in this context because it is also relative.
Working fix
I worked around the issue with a small custom plugin which resolves the path against g.lib.directory before calling send_file():
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 UnicodeError:
safe_filename = unidecode(base_filename)
else:
safe_filename = base_filename
return flask.send_file(
item_path,
as_attachment=True,
download_name=safe_filename,
)
class WebPathFixPlugin(BeetsPlugin):
def __init__(self):
super().__init__()
app.view_functions["item_file"] = fixed_item_fileWith this workaround, /item/2406/file returns 200 OK and successfully serves the file.
Expected behaviour
The Web plugin should resolve a relative item.path against the library's configured directory before passing it to flask.send_file().
In this example:
item.path = A/Alleviate/...
g.lib.directory = /musicshould result in:
/music/A/Alleviate/...rather than resolving the path relative to the Web plugin's package directory.
Additional note
This appears related to the relative-path/portable-library changes introduced in Beets 2.14. The normal Beets CLI correctly constructs the library using the configured directory, but the Web request context does not appear to apply that path resolution when accessing item.path.
I would be happy to provide additional debugging output or test a proposed fix.
Source: beetbox/beets