mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2026-10-20 18:12:43 -04:00
[utils] Improve Content-Disposition filename parsing (#17727)
Authored by: doe1080
This commit is contained in:
@@ -3,6 +3,7 @@ import re
|
||||
from .common import InfoExtractor
|
||||
from ..utils import (
|
||||
determine_ext,
|
||||
disposition_filename,
|
||||
extract_attributes,
|
||||
filter_dict,
|
||||
get_element_by_class,
|
||||
@@ -184,9 +185,7 @@ class GoogleDriveIE(InfoExtractor):
|
||||
def add_source_format(urlh):
|
||||
nonlocal title
|
||||
if not title:
|
||||
title = self._search_regex(
|
||||
r'\bfilename="([^"]+)"', urlh.headers.get('Content-Disposition'),
|
||||
'title', default=None)
|
||||
title = disposition_filename(urlh.headers) or None
|
||||
formats.append({
|
||||
# Use redirect URLs as download URLs in order to calculate
|
||||
# correct cookies in _calc_cookies.
|
||||
@@ -198,7 +197,7 @@ class GoogleDriveIE(InfoExtractor):
|
||||
'format_id': 'source',
|
||||
'quality': 1,
|
||||
})
|
||||
if urlh.headers.get('Content-Disposition'):
|
||||
if urlh.headers.get_content_disposition():
|
||||
add_source_format(urlh)
|
||||
else:
|
||||
confirmation_webpage = self._webpage_read_content(
|
||||
@@ -209,7 +208,7 @@ class GoogleDriveIE(InfoExtractor):
|
||||
get_element_html_by_id('download-form', confirmation_webpage) or '').get('action')
|
||||
if confirmed_source_url:
|
||||
urlh = request_source_file(confirmed_source_url, 'confirmed source', data=b'')
|
||||
if urlh and urlh.headers.get('Content-Disposition'):
|
||||
if urlh and urlh.headers.get_content_disposition():
|
||||
add_source_format(urlh)
|
||||
else:
|
||||
self.report_warning(
|
||||
|
||||
+17
-9
@@ -3129,18 +3129,26 @@ def get_compatible_ext(*, vcodecs, acodecs, vexts, aexts, preferences=None):
|
||||
return 'mkv' if allow_mkv else preferences[-1]
|
||||
|
||||
|
||||
def urlhandle_detect_ext(url_handle, default=NO_DEFAULT):
|
||||
getheader = url_handle.headers.get
|
||||
def disposition_filename(headers):
|
||||
filename = None
|
||||
for name, value in headers.get_params(header='Content-Disposition') or ():
|
||||
if name.lower() != 'filename':
|
||||
continue
|
||||
filename = value
|
||||
if isinstance(value, tuple): # Prefer filename* over filename
|
||||
break
|
||||
|
||||
if cd := getheader('Content-Disposition'):
|
||||
if m := re.match(r'attachment;\s*filename="(?P<filename>[^"]+)"', cd):
|
||||
if ext := determine_ext(m.group('filename'), default_ext=None):
|
||||
return ext
|
||||
return email.utils.collapse_rfc2231_value(filename).strip() if filename is not None else None
|
||||
|
||||
|
||||
def urlhandle_detect_ext(url_handle, default=NO_DEFAULT):
|
||||
headers = url_handle.headers
|
||||
|
||||
return (
|
||||
determine_ext(getheader('x-amz-meta-name'), default_ext=None)
|
||||
or getheader('x-amz-meta-file-type')
|
||||
or mimetype2ext(getheader('Content-Type'), default=default))
|
||||
determine_ext(disposition_filename(headers), default_ext=None)
|
||||
or determine_ext(headers.get('x-amz-meta-name'), default_ext=None)
|
||||
or headers.get('x-amz-meta-file-type')
|
||||
or mimetype2ext(headers.get('Content-Type'), default=default))
|
||||
|
||||
|
||||
def encode_data_uri(data, mime_type):
|
||||
|
||||
Reference in New Issue
Block a user