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
import posixpath
import re
import shutil
import urlparse
try:
from slapos.libnetworkcache import NetworkcacheClient, UploadError, \
......@@ -136,14 +137,15 @@ def upload_network_cached(dir_url, cache_url, external_url, path, logger,
def get_filename_from_url(url):
"""Inspired how pip get filename from url.
"""
url = url.split('#', 1)[0]
url = url.rstrip('/')
name = posixpath.basename(url)
name_split_list = name.split('?', 1)
if name_split_list[0]:
name = name_split_list[0]
parsed_url = urlparse.urlparse(url)
if parsed_url.query and parsed_url.path.endswith('/'):
name = parsed_url.query.split('?', 1)[0]
elif parsed_url.path.endswith('/') and not parsed_url.query:
name = parsed_url.path.split('/')[-2]
else:
name = name_split_list[1]
name = posixpath.basename(parsed_url.path)
name = name.split('#', 1)[0]
assert name, (
'URL %r produced no filename' % url)
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