From e874e6aa01643488381eb4d79be2df521973b0c1 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Sat, 8 Dec 2018 17:03:43 -0600 Subject: [PATCH] [Audio] Don't remove active .part files (#1481) --- cogs/audio.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/cogs/audio.py b/cogs/audio.py index 0f6caee42..0ec3299b6 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -360,14 +360,15 @@ class Audio: self.queue[server.id][QueueKey.QUEUE].appendleft(queued_song) def _cache_desired_files(self): - filelist = [] + filelist = set() + for server in self.downloaders: song = self.downloaders[server].song try: - filelist.append(song.id) + filelist.add(song.id) except AttributeError: pass - shuffle(filelist) + return filelist def _cache_max(self): @@ -380,13 +381,15 @@ class Audio: def _cache_required_files(self): queue = copy.deepcopy(self.queue) - filelist = [] + filelist = set() + for server in queue: now_playing = queue[server].get(QueueKey.NOW_PLAYING) try: - filelist.append(now_playing.id) + filelist.add(now_playing.id) except AttributeError: pass + return filelist def _cache_size(self): @@ -552,16 +555,22 @@ class Audio: prev_size = self._cache_size() for file in os.listdir(self.cache_path): - if file not in reqd: - if ignore_desired or file not in opt: - try: - os.remove(os.path.join(self.cache_path, file)) - except OSError: - # A directory got in the cache? - pass - except WindowsError: - # Removing a file in use, reqd failed - pass + if file in reqd: + continue + elif not ignore_desired: + if file in opt: + continue + elif file.endswith('.part') and file[:-5] in opt: + continue + + try: + os.remove(os.path.join(self.cache_path, file)) + except OSError: + # A directory got in the cache? + pass + except WindowsError: + # Removing a file in use, reqd failed + pass post_size = self._cache_size() dumped = prev_size - post_size