Commit 9292eca1 authored by Nicolas Delaby's avatar Nicolas Delaby

Improve filename detection from http requests.

  - First read content-disposition header
  - if not found, then read real url used to download the file.
    Take into accounts HTTP redirections.
small refactoring to use dedicated utilities to parse urls and extract filename


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38375 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0e53ab03
...@@ -33,6 +33,9 @@ import string ...@@ -33,6 +33,9 @@ import string
import socket import socket
import md5 import md5
import urllib2, urllib import urllib2, urllib
import urlparse
from cgi import parse_header
import os
from AccessControl import ClassSecurityInfo, getSecurityManager from AccessControl import ClassSecurityInfo, getSecurityManager
from Products.ERP5Type.Globals import InitializeClass, DTMLFile from Products.ERP5Type.Globals import InitializeClass, DTMLFile
...@@ -166,17 +169,29 @@ class ContributionTool(BaseTool): ...@@ -166,17 +169,29 @@ class ContributionTool(BaseTool):
file = cStringIO.StringIO() file = cStringIO.StringIO()
file.write(data) file.write(data)
file.seek(0) file.seek(0)
# Create a file name based on the URL and quote it # if a content-disposition header is present,
file_name = url.split('/')[-1] or url.split('/')[-2] # try first to read the suggested filename from it.
file_name = urllib.quote(file_name, safe='') header_info = url_file.info()
file_name = file_name.replace('%', '') content_disposition = header_info.getheader('content-disposition', '')
file_name = parse_header(content_disposition)[1].get('filename')
if not file_name:
# Now read the filename from url.
# In case of http redirection, the real url must be read
# from file object returned by urllib2.urlopen.
# It can happens when the header 'Location' is present in request.
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
url = url_file.geturl()
# Create a file name based on the URL and quote it
file_name = urlparse.urlsplit(url)[-3]
file_name = os.path.basename(file_name)
file_name = urllib.quote(file_name, safe='')
file_name = file_name.replace('%', '')
# For URLs, we want an id by default equal to the encoded URL # For URLs, we want an id by default equal to the encoded URL
if id is None: id = self.encodeURL(url) if id is None:
if hasattr(url_file, 'headers'): id = self.encodeURL(url)
headers = url_file.headers content_type = header_info.gettype()
if hasattr(headers, 'type'): if content_type:
mime_type = headers.type kw['content_type'] = content_type
kw['content_type'] = mime_type
kw['file'] = file kw['file'] = file
# If the portal_type was provided, we can go faster # If the portal_type was provided, we can go faster
......
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