mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-03-09 12:50:23 -05:00
Merge 4f46e5a323
into 05c8023a27
This commit is contained in:
commit
6f2aabe982
1 changed files with 29 additions and 0 deletions
|
@ -1475,12 +1475,41 @@ def _prepare_filename(self, info_dict, *, outtmpl=None, tmpl_type=None):
|
||||||
self.report_error('Error in output template: ' + str(err) + ' (encoding: ' + repr(preferredencoding()) + ')')
|
self.report_error('Error in output template: ' + str(err) + ' (encoding: ' + repr(preferredencoding()) + ')')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def validate_destination_filename(self, filename):
|
||||||
|
"""Check if the destination filename is valid in the OS. In particular
|
||||||
|
it checks if the length does not exceed the OS limit. However currently
|
||||||
|
any error is simply raised, independent of the cause.
|
||||||
|
|
||||||
|
Without this, download would fail only after the entire file is downloaded."""
|
||||||
|
cwd = os.getcwd()
|
||||||
|
with tempfile.TemporaryDirectory() as d:
|
||||||
|
os.chdir(d)
|
||||||
|
try:
|
||||||
|
with open(filename, 'w') as f:
|
||||||
|
f.close()
|
||||||
|
except OSError as e:
|
||||||
|
if (os.name == 'nt' and e.errno == 206) or (e.errno == errno.ENAMETOOLONG):
|
||||||
|
# The first condition is for windows,
|
||||||
|
# and the second for unix-ish systems.
|
||||||
|
|
||||||
|
# An improvement idea:
|
||||||
|
# by default, retry (exec yt-dlp itself) by
|
||||||
|
# -o "%(id)s.%(ext)s" --write-info-json,
|
||||||
|
# but respect the directory from --output of the original call.
|
||||||
|
self.to_screen('''[Notice] The file name to be saved is too long, exceeding the OS limit.
|
||||||
|
[Notice] Consider options --trim-filenames or -o (--output).''')
|
||||||
|
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
os.chdir(cwd)
|
||||||
|
|
||||||
def prepare_filename(self, info_dict, dir_type='', *, outtmpl=None, warn=False):
|
def prepare_filename(self, info_dict, dir_type='', *, outtmpl=None, warn=False):
|
||||||
"""Generate the output filename"""
|
"""Generate the output filename"""
|
||||||
if outtmpl:
|
if outtmpl:
|
||||||
assert not dir_type, 'outtmpl and dir_type are mutually exclusive'
|
assert not dir_type, 'outtmpl and dir_type are mutually exclusive'
|
||||||
dir_type = None
|
dir_type = None
|
||||||
filename = self._prepare_filename(info_dict, tmpl_type=dir_type, outtmpl=outtmpl)
|
filename = self._prepare_filename(info_dict, tmpl_type=dir_type, outtmpl=outtmpl)
|
||||||
|
self.validate_destination_filename(filename)
|
||||||
if not filename and dir_type not in ('', 'temp'):
|
if not filename and dir_type not in ('', 'temp'):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue