Browse Source

Small refactor of the new content-disposition parsing

pull/1951/head
Safihre 4 years ago
parent
commit
2285c6e430
  1. 41
      sabnzbd/urlgrabber.py

41
sabnzbd/urlgrabber.py

@ -60,27 +60,6 @@ _RARTING_FIELDS = (
)
def filename_from_content_disposition(content_disposition):
"""
Extract and validate filename from a Content-Disposition header.
Origin: https://github.com/httpie/httpie/blob/4c8633c6e51f388523ab4fa649040934402a4fc9/httpie/downloads.py#L98
:param content_disposition: Content-Disposition value
:type content_disposition: str
:return: the filename if present and valid, otherwise `None`
:example:
filename_from_content_disposition('attachment; filename=jakubroztocil-httpie-0.4.1-20-g40bd8f6.tar.gz')
should return: 'jakubroztocil-httpie-0.4.1-20-g40bd8f6.tar.gz'
"""
msg = Message(f"Content-Disposition: attachment; {content_disposition}")
filename = msg.get_filename()
if filename:
# Basic sanitation.
filename = os.path.basename(filename).lstrip(".").strip()
if filename:
return filename
class URLGrabber(Thread):
def __init__(self):
super().__init__()
@ -391,3 +370,23 @@ def _analyse(fetch_request: HTTPResponse, future_nzo: NzbObject):
return None, msg, True, when, data
return fetch_request, fetch_request.msg, False, 0, data
def filename_from_content_disposition(content_disposition: str) -> Optional[str]:
"""
Extract and validate filename from a Content-Disposition header.
Origin: https://github.com/httpie/httpie/blob/4c8633c6e51f388523ab4fa649040934402a4fc9/httpie/downloads.py#L98
:param content_disposition: Content-Disposition value
:type content_disposition: str
:return: the filename if present and valid, otherwise `None`
:example:
filename_from_content_disposition('attachment; filename=jakubroztocil-httpie-0.4.1-20-g40bd8f6.tar.gz')
should return: 'jakubroztocil-httpie-0.4.1-20-g40bd8f6.tar.gz'
"""
filename = Message(f"Content-Disposition: attachment; {content_disposition}").get_filename()
if filename:
# Basic sanitation
filename = os.path.basename(filename).lstrip(".").strip()
if filename:
return filename

Loading…
Cancel
Save