def test_temptest(self): self.makefile(self.fname, 'test_content') result = send_file.send_file(self.fname, as_attachment=True, conditional=True) expected = '<Response streamed [200 OK]>' assert str(result) == expected with pytest.raises(TypeError): send_file.send_file(None, as_attachment=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 _convert(self, doc): from emeraldtree import ElementTree as ET from moin.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)