Fix setting cover image from local file

This commit is contained in:
Teemu Ikonen 2022-08-16 14:18:46 +03:00
parent 93b636b6ca
commit dc7ebf494d
1 changed files with 27 additions and 0 deletions

View File

@ -25,6 +25,8 @@
import logging
import os
import shutil
import urllib.parse
import gpodder
from gpodder import util, youtube
@ -67,6 +69,31 @@ class CoverDownloader(object):
if os.path.exists(filename + extension):
return filename + extension
# Handle local files
if cover_url is not None and cover_url.startswith('file://'):
try:
path = urllib.parse.unquote(cover_url).replace('file://', '')
if not os.path.exists(path):
raise ValueError('Cover file not found: %s' % (path))
extension = None
with open(path, 'rb') as fp:
data = fp.read(512)
for filetype, check in list(self.SUPPORTED_EXTENSIONS.items()):
if check(data):
extension = filetype
break
if extension is None:
raise ValueError(
'Unknown file type: %s (%r)' % (cover_url, data[:6]))
# File is ok, copy it
shutil.copyfile(path, filename + extension)
return filename + extension
except Exception as e:
logger.warning('Setting cover art from file failed: %s', e)
return self._fallback_filename(title)
# If allowed to download files, do so here
if download:
# YouTube-specific cover art image resolver