예제 #1
0
def get_mimetype(name):
    """Try to guess the mimetype given the name. To guess from the name we
    need to extract the type extension, we use an heuristic for this task,
    but it needs to be improved because there are many patterns:

    <name>                                 README
    <name>.<type>                          index.html
    <name>.<type>.<language>               index.html.en
    <name>.<type>.<language>.<encoding>    index.html.en.UTF-8
    <name>.<type>.<compression>            itools.tar.gz
    etc...

    And even more complex, the name could contain dots, or the filename
    could start by a dot (a hidden file in Unix systems).
    """
    name, extension, language = FileName.decode(name)
    # Figure out the mimetype from the filename extension
    if extension is not None:
        mimetype, encoding = guess_type('%s.%s' % (name, extension))
        # FIXME Compression schemes are not mimetypes, see /etc/mime.types
        if encoding == 'gzip':
            if mimetype == 'application/x-tar':
                return 'application/x-tgz'
            return 'application/x-gzip'
        elif encoding == 'bzip2':
            if mimetype == 'application/x-tar':
                return 'application/x-tbz2'
            return 'application/x-bzip2'
        elif mimetype is not None:
            return mimetype

    return 'application/octet-stream'
예제 #2
0
def get_mimetype(name):
    """Try to guess the mimetype given the name. To guess from the name we
    need to extract the type extension, we use an heuristic for this task,
    but it needs to be improved because there are many patterns:

    <name>                                 README
    <name>.<type>                          index.html
    <name>.<type>.<language>               index.html.en
    <name>.<type>.<language>.<encoding>    index.html.en.UTF-8
    <name>.<type>.<compression>            itools.tar.gz
    etc...

    And even more complex, the name could contain dots, or the filename
    could start by a dot (a hidden file in Unix systems).
    """
    name, extension, language = FileName.decode(name)
    # Figure out the mimetype from the filename extension
    if extension is not None:
        mimetype, encoding = guess_type('%s.%s' % (name, extension))
        # FIXME Compression schemes are not mimetypes, see /etc/mime.types
        if encoding == 'gzip':
            if mimetype == 'application/x-tar':
                return 'application/x-tgz'
            return 'application/x-gzip'
        elif encoding == 'bzip2':
            if mimetype == 'application/x-tar':
                return 'application/x-tbz2'
            return 'application/x-bzip2'
        elif mimetype is not None:
            return mimetype

    return 'application/octet-stream'
예제 #3
0
파일: csv_views.py 프로젝트: hforge/crm
 def get_csv_filename(self, resource, context, writer):
     filename = self.csv_filename
     if filename is None:
         filename = resource.name
     if guess_type(filename) == (None, None):
         filename = "{0}.{1}".format(filename, writer.extension)
     return filename
예제 #4
0
파일: datatypes.py 프로젝트: hforge/ikaaro
def guess_mimetype(filename, default):
    """Override itools function 'guess_type' to intercept the encoding.
    """
    name, extension, language = FileName.decode(filename)
    filename = FileName.encode((name, extension, None))

    mimetype, encoding = guess_type(filename)
    return encoding_map.get(encoding, mimetype or default)
예제 #5
0
def guess_mimetype(filename, default):
    """Override itools function 'guess_type' to intercept the encoding.
    """
    name, extension, language = FileName.decode(filename)
    filename = FileName.encode((name, extension, None))

    mimetype, encoding = guess_type(filename)
    return encoding_map.get(encoding, mimetype or default)