Skip to content

Commit 5c8f1c1

Browse files
author
Pierre Ayoub
authored
Fix convert plugin attempting to process a non-media file (#5261)
## Description My library is managed using Beets for organization and [git-annex](https://git-annex.branchable.com/) as storage backend. Therefore when using this system, while my library files always exists on my filesystem, some files may be empty (without content). In this case, when I'm running the `convert` plugin, I don't wants it to process files which are empty (same apply for any Beets plugin). Hence, I added a check that the file is readable as a `MediaFile` before doing any process. Before this fix, trying to encode an empty file would have lead to an error while leaving `convert` doing its side-effects **and** `convert` would also copy empty files to destination for files that doesn't need to be re-encoded. In my case, this is empty files, but the problem can be anything else (depending on the storage backend) and/or corrupted files. Conclusion, I think **checking that the file is readable is always recommended before proceeding to heavy operation** like this.
1 parent fe28957 commit 5c8f1c1

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

beetsplug/convert.py

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import threading
2323
from string import Template
2424

25+
import mediafile
2526
from confuse import ConfigTypeError, Optional
2627

2728
from beets import art, config, plugins, ui, util
@@ -351,6 +352,15 @@ def convert_item(
351352
item = yield (item, original, converted)
352353
dest = item.destination(basedir=dest_dir, path_formats=path_formats)
353354

355+
# Ensure that desired item is readable before processing it. Needed
356+
# to avoid any side-effect of the conversion (linking, keep_new,
357+
# refresh) if we already know that it will fail.
358+
try:
359+
mediafile.MediaFile(util.syspath(item.path))
360+
except mediafile.UnreadableFileError as exc:
361+
self._log.error("Could not open file to convert: {0}", exc)
362+
continue
363+
354364
# When keeping the new file in the library, we first move the
355365
# current (pristine) file to the destination. We'll then copy it
356366
# back to its old path or transcode it to a new path.

docs/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ Bug fixes:
519519
`requests` timeout.
520520
* Fix cover art resizing logic to support multiple steps of resizing
521521
:bug:`5151`
522+
* :doc:`/plugins/convert`: Fix attempt to convert and perform side-effects if
523+
library file is not readable.
522524

523525
For plugin developers:
524526

0 commit comments

Comments
 (0)