Commit 094cc670 authored by Lucas Carvalho's avatar Lucas Carvalho

Stabilise filename parsing from url.

Whenever special sign ? is found in url analyse it in order to have
correct filename.

In case of http://host/path/filename/? use "filename" as part to
generate filename.

This fuzzy logic allows to generate quite sane filenames.
parent 320843e8
...@@ -18,6 +18,7 @@ import os ...@@ -18,6 +18,7 @@ import os
import posixpath import posixpath
import re import re
import shutil import shutil
import urlparse
try: try:
from slapos.libnetworkcache import NetworkcacheClient, UploadError, \ from slapos.libnetworkcache import NetworkcacheClient, UploadError, \
...@@ -136,14 +137,15 @@ def upload_network_cached(dir_url, cache_url, external_url, path, logger, ...@@ -136,14 +137,15 @@ def upload_network_cached(dir_url, cache_url, external_url, path, logger,
def get_filename_from_url(url): def get_filename_from_url(url):
"""Inspired how pip get filename from url. """Inspired how pip get filename from url.
""" """
url = url.split('#', 1)[0] parsed_url = urlparse.urlparse(url)
url = url.rstrip('/') if parsed_url.query and parsed_url.path.endswith('/'):
name = posixpath.basename(url) name = parsed_url.query.split('?', 1)[0]
name_split_list = name.split('?', 1) elif parsed_url.path.endswith('/') and not parsed_url.query:
if name_split_list[0]: name = parsed_url.path.split('/')[-2]
name = name_split_list[0]
else: else:
name = name_split_list[1] name = posixpath.basename(parsed_url.path)
name = name.split('#', 1)[0]
assert name, ( assert name, (
'URL %r produced no filename' % url) 'URL %r produced no filename' % url)
return name return name
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment