Skip to content

Commit

Permalink
Don't add unsupported media files to library (#885)
Browse files Browse the repository at this point in the history
* Don't add unsupported media files to library
Fixes #883
  • Loading branch information
luzip665 committed Jul 10, 2023
1 parent f078cf3 commit 233923b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 7 additions & 0 deletions xl/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,10 @@ def update_track(
# on windows, unknown why fix isn't needed on linux.
elif not tr._init:
self.collection.add(tr)

if not tr.is_supported():
return None

return tr

def rescan(
Expand Down Expand Up @@ -839,6 +843,9 @@ def rescan(
if not gloc.query_exists(None):
removals.append(tr)

if not tr.is_supported():
removals.append(tr)

for tr in removals:
logger.debug("Removing %s", tr)
self.collection.remove(tr)
Expand Down
35 changes: 29 additions & 6 deletions xl/trax/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ class Track:
"""

# save a little memory this way
__slots__ = ["__tags", "_scan_valid", "_dirty", "__weakref__", "_init"]
__slots__ = [
"__tags",
"_scan_valid",
"_dirty",
"__weakref__",
"_init",
"_is_supported",
]
# this is used to enforce the one-track-per-uri rule
__tracksdict = weakref.WeakValueDictionary()
# store a copy of the settings values here - much faster (0.25 cpu
Expand Down Expand Up @@ -249,6 +256,7 @@ def __init__(self, uri: Optional[str] = None, scan: bool = True, _unpickles=None

self.__tags = {}
self._scan_valid = None # whether our last tag read attempt worked
self._is_supported = None

# This is not used by write_tags, this is used by the collection to
# indicate that the tags haven't been written to the collection
Expand Down Expand Up @@ -388,13 +396,13 @@ def read_tags(self, force=True, notify_changed=True):
Returns False if unsuccessful, and a Format object from
`xl.metadata` otherwise.
"""

if not self.is_supported():
self._scan_valid = False
return False

loc = self.get_loc_for_io()
try:
f = metadata.get_format(loc)
if f is None:
self._scan_valid = False
return False # not a supported type

# Retrieve file specific metadata
gloc = Gio.File.new_for_uri(loc)
if hasattr(Gio.FileInfo, 'get_modification_date_time'): # GLib >=2.62
Expand All @@ -409,6 +417,7 @@ def read_tags(self, force=True, notify_changed=True):
).get_modification_time()
mtime = mtime.tv_sec + (mtime.tv_usec / 100000.0)

f = metadata.get_format(loc)
if not force and self.__tags.get('__modified', 0) >= mtime:
return f

Expand Down Expand Up @@ -460,6 +469,20 @@ def is_local(self):
return True
return False

def is_supported(self):
"""
Determines if a file has a supported media format
"""
if self._is_supported is None:
loc = self.get_loc_for_io()
f = metadata.get_format(loc)
if f is None:
self._is_supported = False
else:
self._is_supported = True

return self._is_supported

def get_size(self):
"""
Get the raw size of the file. Potentially slow.
Expand Down
3 changes: 3 additions & 0 deletions xl/trax/trackdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ def add_tracks(self, tracks: Iterable[Track]) -> None:
# Don't add duplicates -- track URLs are unique
if location in self.tracks:
continue
# Don't add unsupported media files
if not tr.is_supported():
continue
locations += [location]
self.tracks[location] = TrackHolder(tr, self._key)
self._key += 1
Expand Down

0 comments on commit 233923b

Please sign in to comment.