Пример #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)
Пример #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
Пример #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
Пример #4
0
def searchAndImportPlugin(cfg, type, name, what=None):
    type2classname = {
    }
    if what is None:
        what = type2classname[type]
    mt = MimeType(name)
    plugin = None
    for module_name in mt.module_name():
        try:
            plugin = importPlugin(cfg, type, module_name, what)
            break
        except PluginMissingError:
            pass
    else:
        raise PluginMissingError("Plugin not found! ({0!r} {1!r} {2!r})".format(type, name, what))
    return plugin
Пример #5
0
 def _get_meta(self, itemname, path):
     try:
         st = os.stat(path)
     except OSError as e:
         if e.errno == errno.ENOENT:
             raise KeyError(itemname)
         raise
     meta = {}
     meta[NAME] = itemname
     meta[MTIME] = int(st.st_mtime)  # use int, not float
     meta[REVID] = unicode(self._encode('%s.%d' % (meta[NAME], meta[MTIME])))
     meta[ITEMID] = meta[REVID]
     meta[HASH_ALGORITHM] = u''  # XXX crap, but sendfile needs it for etag
     if stat.S_ISDIR(st.st_mode):
         # directory
         # we create a virtual wiki page listing links to subitems:
         ct = u'text/x.moin.wiki;charset=utf-8'
         size = 0
     elif stat.S_ISREG(st.st_mode):
         # normal file
         ct = unicode(MimeType(filename=itemname).content_type())
         size = int(st.st_size)  # use int instead of long
     else:
         # symlink, device file, etc.
         ct = u'application/octet-stream'
         size = 0
     meta[CONTENTTYPE] = ct
     meta[SIZE] = size
     return meta
Пример #6
0
def searchAndImportPlugin(cfg, type, name, what=None):
    type2classname = {}
    if what is None:
        what = type2classname[type]
    mt = MimeType(name)
    plugin = None
    for module_name in mt.module_name():
        try:
            plugin = importPlugin(cfg, type, module_name, what)
            break
        except PluginMissingError:
            pass
    else:
        raise PluginMissingError(
            "Plugin not found! ({0!r} {1!r} {2!r})".format(type, name, what))
    return plugin
Пример #7
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)
Пример #8
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,
        )
Пример #9
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)
Пример #10
0
 def __init__(self, item_name, attach_name, attpath, editlog, acl):
     try:
         meta = editlog.find_attach(attach_name)
     except KeyError:
         meta = {  # make something up
             MTIME: int(os.path.getmtime(attpath)),
             ACTION: ACTION_SAVE,
         }
     meta[NAME] = [u'{0}/{1}'.format(item_name, attach_name)]
     if acl is not None:
         meta[ACL] = acl
     meta[CONTENTTYPE] = unicode(MimeType(filename=attach_name).content_type())
     f = open(attpath, 'rb')
     size, hash_name, hash_digest = hash_hexdigest(f)
     f.seek(0)
     self.data = f
     meta[hash_name] = hash_digest
     meta[SIZE] = size
     meta[ITEMID] = make_uuid()
     meta[REVID] = make_uuid()
     meta[REV_NUMBER] = 1
     meta[ITEMTYPE] = ITEMTYPE_DEFAULT
     self.meta = meta