Exemplo n.º 1
0
 def _do_get(self, hash, member=None, force_attachment=False, mimetype=None):
     if member:  # content = file contained within a archive item revision
         path, filename = os.path.split(member)
         mt = MimeType(filename=filename)
         content_length = None
         file_to_send = self.get_member(member)
         # force attachment download, so it uses attachment_filename
         # otherwise it will use the itemname from the URL for saving
         force_attachment = True
     else:  # content = item revision
         rev = self.rev
         filename = get_download_file_name(rev.item.fqname)
         try:
             mimestr = rev.meta[CONTENTTYPE]
         except KeyError:
             mt = MimeType(filename=filename)
         else:
             mt = MimeType(mimestr=mimestr)
         content_length = rev.meta[SIZE]
         file_to_send = rev.data
     if mimetype:
         content_type = mimetype
     else:
         content_type = mt.content_type()
     as_attachment = force_attachment or mt.as_attachment(app.cfg)
     return send_file(file=file_to_send,
                      mimetype=content_type,
                      as_attachment=as_attachment, attachment_filename=filename,
                      cache_timeout=10,  # wiki data can change rapidly
                      add_etags=True, etag=hash, conditional=True)
Exemplo n.º 2
0
def file_headers(filename=None, content_type=None, content_length=None):
        """
        Compute http headers for sending a file

        :param filename: filename for autodetecting content_type (unicode, default: None)
        :param content_type: content-type header value (str, default: autodetect from filename)
        :param content_length: for content-length header (int, default:None)
        """
        if filename:
            # make sure we just have a simple filename (without path)
            filename = os.path.basename(filename)
            mt = MimeType(filename=filename)
        else:
            mt = None

        if content_type is None:
            if mt is not None:
                content_type = mt.content_type()
            else:
                content_type = 'application/octet-stream'
        else:
            mt = MimeType(mimestr=content_type)

        headers = [('Content-Type', content_type)]
        if content_length is not None:
            headers.append(('Content-Length', str(content_length)))
        return headers
Exemplo n.º 3
0
def file_headers(filename=None, content_type=None, content_length=None):
    """
        Compute http headers for sending a file

        :param filename: filename for autodetecting content_type (unicode, default: None)
        :param content_type: content-type header value (str, default: autodetect from filename)
        :param content_length: for content-length header (int, default:None)
        """
    if filename:
        # make sure we just have a simple filename (without path)
        filename = os.path.basename(filename)
        mt = MimeType(filename=filename)
    else:
        mt = None

    if content_type is None:
        if mt is not None:
            content_type = mt.content_type()
        else:
            content_type = 'application/octet-stream'
    else:
        mt = MimeType(mimestr=content_type)

    headers = [('Content-Type', content_type)]
    if content_length is not None:
        headers.append(('Content-Length', str(content_length)))
    return headers
Exemplo n.º 4
0
    def _convert(self, doc):
        from emeraldtree import ElementTree as ET
        from MoinMoin.converter import default_registry as reg

        doc = self._expand_document(doc)

        # We convert the internal representation of the document
        # into a DocBook document
        conv = reg.get(type_moin_document, Type('application/docbook+xml'))

        doc = conv(doc)

        # We determine the different namespaces of the output form
        output_namespaces = {
            docbook.namespace: '',
            xlink.namespace: 'xlink',
        }

        # We convert the result into a StringIO object
        # With the appropriate namespace
        # TODO: Some other operation should probably be done here too
        # like adding a doctype
        file_to_send = StringIO()
        tree = ET.ElementTree(doc)
        tree.write(file_to_send, namespaces=output_namespaces)

        # We determine the different parameters for the reply
        mt = MimeType(mimestr='application/docbook+xml;charset=utf-8')
        content_type = mt.content_type()
        as_attachment = mt.as_attachment(app.cfg)
        # After creation of the StringIO, we are at the end of the file
        # so position is the size the file.
        # and then we should move it back at the beginning of the file
        content_length = file_to_send.tell()
        file_to_send.seek(0)
        # Important: empty filename keeps flask from trying to autodetect filename,
        # as this would not work for us, because our file's are not necessarily fs files.
        return send_file(
            file=file_to_send,
            mimetype=content_type,
            as_attachment=as_attachment,
            attachment_filename=None,
            cache_timeout=10,  # wiki data can change rapidly
            add_etags=False,
            etag=None,
            conditional=True)
Exemplo n.º 5
0
    def _convert(self, doc):
        from emeraldtree import ElementTree as ET
        from MoinMoin.converter import default_registry as reg

        doc = self._expand_document(doc)

        # We convert the internal representation of the document
        # into a DocBook document
        conv = reg.get(type_moin_document, Type("application/docbook+xml"))

        doc = conv(doc)

        # We determine the different namespaces of the output form
        output_namespaces = {docbook.namespace: "", xlink.namespace: "xlink"}

        # We convert the result into a StringIO object
        # With the appropriate namespace
        # TODO: Some other operation should probably be done here too
        # like adding a doctype
        file_to_send = StringIO()
        tree = ET.ElementTree(doc)
        tree.write(file_to_send, namespaces=output_namespaces)

        # We determine the different parameters for the reply
        mt = MimeType(mimestr="application/docbook+xml;charset=utf-8")
        content_type = mt.content_type()
        as_attachment = mt.as_attachment(app.cfg)
        # After creation of the StringIO, we are at the end of the file
        # so position is the size the file.
        # and then we should move it back at the beginning of the file
        content_length = file_to_send.tell()
        file_to_send.seek(0)
        # Important: empty filename keeps flask from trying to autodetect filename,
        # as this would not work for us, because our file's are not necessarily fs files.
        return send_file(
            file=file_to_send,
            mimetype=content_type,
            as_attachment=as_attachment,
            attachment_filename=None,
            cache_timeout=10,  # wiki data can change rapidly
            add_etags=False,
            etag=None,
            conditional=True,
        )
Exemplo n.º 6
0
 def _do_get(self,
             hash,
             member=None,
             force_attachment=False,
             mimetype=None):
     if member:  # content = file contained within a archive item revision
         path, filename = os.path.split(member)
         mt = MimeType(filename=filename)
         content_length = None
         file_to_send = self.get_member(member)
         # force attachment download, so it uses attachment_filename
         # otherwise it will use the itemname from the URL for saving
         force_attachment = True
     else:  # content = item revision
         rev = self.rev
         filename = get_download_file_name(rev.item.fqname)
         try:
             mimestr = rev.meta[CONTENTTYPE]
         except KeyError:
             mt = MimeType(filename=filename)
         else:
             mt = MimeType(mimestr=mimestr)
         content_length = rev.meta[SIZE]
         file_to_send = rev.data
     if mimetype:
         content_type = mimetype
     else:
         content_type = mt.content_type()
     as_attachment = force_attachment or mt.as_attachment(app.cfg)
     return send_file(
         file=file_to_send,
         mimetype=content_type,
         as_attachment=as_attachment,
         attachment_filename=filename,
         cache_timeout=10,  # wiki data can change rapidly
         add_etags=True,
         etag=hash,
         conditional=True)