Skip to content

Commit

Permalink
Merge pull request #27 from wisp3rwind/pr-fix-format-change
Browse files Browse the repository at this point in the history
Don't corrupt files on format change
  • Loading branch information
Thomas Scholtes authored Nov 24, 2018
2 parents cf7fadd + df44bac commit ba6162d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Change Log
with current beets)
* Bugfix: Explicitly write tags after encoding instead of relying on the
encoder to do so
* Bugfix: If the `formats` config option is modified, don't move files if the
extension would change, but re-encode

## v0.8.2 - 2015-05-31
* Fix a bug that made the plugin crash when reading unicode strings
Expand Down
11 changes: 8 additions & 3 deletions beetsplug/alternatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ def parse_config(self, config):

def matched_item_action(self, item):
path = self.get_path(item)
actions = []
if path and os.path.isfile(syspath(path)):
dest = self.destination(item)
_, path_ext = os.path.splitext(path)
_, dest_ext = os.path.splitext(dest)
if not path_ext == dest_ext:
# formats config option changed
return (item, [self.REMOVE, self.ADD])
actions = []
if not util.samefile(path, dest):
actions.append(self.MOVE)
item_mtime_alt = os.path.getmtime(syspath(path))
Expand All @@ -134,9 +139,9 @@ def matched_item_action(self, item):
(item_mtime_alt
< os.path.getmtime(syspath(album.artpath)))):
actions.append(self.SYNC_ART)
return (item, actions)
else:
actions.append(self.ADD)
return (item, actions)
return (item, [self.ADD])

def items_actions(self):
matched_ids = set()
Expand Down
20 changes: 18 additions & 2 deletions test/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def setUp(self):
self.external_config = self.config['alternatives']['myexternal']

def test_convert(self):
item = self.add_track(myexternal='true', format='mp4')
item = self.add_track(myexternal='true', format='m4a')
self.runcli('alt', 'update', 'myexternal')
item.load()
converted_path = self.get_path(item)
Expand All @@ -324,7 +324,7 @@ def test_convert_and_embed(self):
self.assertHasEmbeddedArtwork(self.get_path(item))

def test_convert_write_tags(self):
item = self.add_track(myexternal='true', format='mp4', title=u'TITLE')
item = self.add_track(myexternal='true', format='m4a', title=u'TITLE')

# We "convert" by copying the file. Setting the title simulates
# a badly behaved converter
Expand Down Expand Up @@ -356,6 +356,22 @@ def test_skip_convert_for_alternative_format(self):
converted_path = self.get_path(item)
self.assertNotFileTag(converted_path, b'ISOGG')

def test_no_move_on_extension_change(self):
item = self.add_track(myexternal='true', format='m4a')
self.runcli('alt', 'update', 'myexternal')

self.config['convert']['formats'] = {
'mp3': 'bash -c "cp \'$source\' \'$dest\';' +
'printf ISMP3 >> \'$dest\'"'
}
self.config['alternatives']['myexternal']['formats'] = 'mp3'

# Assert that this re-encodes instead of copying the ogg file
self.runcli('alt', 'update', 'myexternal')
item.load()
converted_path = self.get_path(item)
self.assertFileTag(converted_path, b'ISMP3')


class ExternalRemovableTest(TestHelper):

Expand Down
8 changes: 6 additions & 2 deletions test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,15 @@ def add_track(self, **kwargs):
'title': 'track 1',
'artist': 'artist 1',
'album': 'album 1',
'format': 'mp3',
}
values.update(kwargs)
assert(values['format'] in 'mp3 m4a ogg'.split())

item = Item.from_path(os.path.join(self.fixture_dir,
bytestring_path('min.mp3')))
item = Item.from_path(os.path.join(
self.fixture_dir,
bytestring_path('min.' + values['format'])
))
item.add(self.lib)
item.update(values)
item.move(MoveOperation.COPY)
Expand Down

0 comments on commit ba6162d

Please sign in to comment.