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
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] = ['{0}/{1}'.format(item_name, attach_name)] if acl is not None: meta[ACL] = acl meta[CONTENTTYPE] = str(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 meta[WIKINAME] = app.cfg.sitename # old 1.9 sitename is not available for attr in (COMMENT, SUMMARY, ): meta[attr] = "" for attr in (EXTERNALLINKS, ITEMLINKS, ITEMTRANSCLUSIONS, NAME_OLD, TAGS, ): meta[attr] = [] self.meta = meta
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
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
def _convert(self, doc): from emeraldtree import ElementTree as ET from moin.converters 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 BytesIO object # With the appropriate namespace # TODO: Some other operation should probably be done here too # like adding a doctype file_to_send = BytesIO() 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 BytesIO, 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)
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)
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] = ['{0}/{1}'.format(item_name, attach_name)] if acl is not None: meta[ACL] = acl meta[CONTENTTYPE] = str(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