本文整理汇总了Python中rfc6266.build_header函数的典型用法代码示例。如果您正苦于以下问题:Python build_header函数的具体用法?Python build_header怎么用?Python build_header使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了build_header函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: get_file
def get_file(request):
db_doc_id = request.validated['db_doc'].id
document = request.validated['document']
key = request.params.get('download')
if not any([key in i.url for i in request.validated['documents']]):
request.errors.add('url', 'download', 'Not Found')
request.errors.status = 404
return
filename = "{}_{}".format(document.id, key)
if request.registry.docservice_url and filename not in request.validated['db_doc']['_attachments']:
document = [i for i in request.validated['documents'] if key in i.url][-1]
if 'Signature=' in document.url and 'KeyID' in document.url:
url = document.url
else:
if 'download=' not in document.url:
key = urlparse(document.url).path.replace('/get/', '')
if not document.hash:
url = generate_docservice_url(request, key, prefix='{}/{}'.format(db_doc_id, document.id))
else:
url = generate_docservice_url(request, key)
request.response.content_type = document.format.encode('utf-8')
request.response.content_disposition = build_header(document.title, filename_compat=quote(document.title.encode('utf-8')))
request.response.status = '302 Moved Temporarily'
request.response.location = url
return url
else:
data = request.registry.db.get_attachment(db_doc_id, filename)
if data:
request.response.content_type = document.format.encode('utf-8')
request.response.content_disposition = build_header(document.title, filename_compat=quote(document.title.encode('utf-8')))
request.response.body_file = data
return request.response
request.errors.add('url', 'download', 'Not Found')
request.errors.status = 404
开发者ID:Krokop,项目名称:openprocurement.api,代码行数:34,代码来源:utils.py
示例2: get_file
def get_file(request):
plan_id = request.validated["plan_id"]
document = request.validated["document"]
key = request.params.get("download")
conn = getattr(request.registry, "s3_connection", None)
filename = "{}_{}".format(document.id, key)
if conn and filename not in request.validated["plan"]["_attachments"]:
filename = "{}/{}/{}".format(plan_id, document.id, key)
url = conn.generate_url(method="GET", bucket=request.registry.bucket_name, key=filename, expires_in=300)
request.response.content_type = document.format.encode("utf-8")
request.response.content_disposition = build_header(
document.title, filename_compat=quote(document.title.encode("utf-8"))
)
request.response.status = "302 Moved Temporarily"
request.response.location = url
return url
else:
filename = "{}_{}".format(document.id, key)
data = request.registry.db.get_attachment(plan_id, filename)
if data:
request.response.content_type = document.format.encode("utf-8")
request.response.content_disposition = build_header(
document.title, filename_compat=quote(document.title.encode("utf-8"))
)
request.response.body_file = data
return request.response
request.errors.add("url", "download", "Not Found")
request.errors.status = 404
开发者ID:openprocurement,项目名称:openprocurement.planning.api,代码行数:28,代码来源:utils.py
示例3: get_file
def get_file(request):
contract_id = request.validated['contract_id']
document = request.validated['document']
key = request.params.get('download')
conn = getattr(request.registry, 's3_connection', None)
filename = "{}_{}".format(document.id, key)
if conn and filename not in request.validated['contract']['_attachments']:
filename = "{}/{}/{}".format(contract_id, document.id, key)
url = conn.generate_url(method='GET',
bucket=request.registry.bucket_name,
key=filename, expires_in=300)
request.response.content_type = document.format.encode('utf-8')
request.response.content_disposition = build_header(
document.title, filename_compat=quote(document.title.encode('utf-8')))
request.response.status = '302 Moved Temporarily'
request.response.location = url
return url
else:
filename = "{}_{}".format(document.id, key)
data = request.registry.db.get_attachment(contract_id, filename)
if data:
request.response.content_type = document.format.encode('utf-8')
request.response.content_disposition = build_header(
document.title, filename_compat=quote(document.title.encode('utf-8')))
request.response.body_file = data
return request.response
request.errors.add('url', 'download', 'Not Found')
request.errors.status = 404
开发者ID:Leits,项目名称:openprocurement.contracting.api,代码行数:28,代码来源:utils.py
示例4: getattachment
def getattachment(request):
"""Fetch a message attachment
FIXME: par manque de caching, le bodystructure du message est
redemandé pour accéder aux headers de cette pièce jointe.
:param request: a ``Request`` object
"""
mbox = request.GET.get("mbox", None)
mailid = request.GET.get("mailid", None)
pnum = request.GET.get("partnumber", None)
if not mbox or not mailid or not pnum:
raise WebmailError(_("Invalid request"))
imapc = get_imapconnector(request)
partdef, payload = imapc.fetchpart(mailid, mbox, pnum)
resp = HttpResponse(decode_payload(partdef["encoding"], payload))
resp["Content-Type"] = partdef["Content-Type"]
resp["Content-Transfer-Encoding"] = partdef["encoding"]
if partdef["disposition"] != 'NIL':
disp = partdef["disposition"]
# FIXME : ugly hack, see fetch_parser.py for more explanation
# :p
if type(disp[1][0]) != dict:
cd = '%s; %s=%s' % (disp[0], disp[1][0], disp[1][1])
else:
cd = '%s; %s=%s' % (disp[0], disp[1][0]['struct'][0], disp[1][0]['struct'][1])
else:
cd = build_header(request.GET["fname"])
resp["Content-Disposition"] = cd
resp["Content-Length"] = partdef["size"]
return resp
开发者ID:ricardopadilha,项目名称:modoboa,代码行数:32,代码来源:views.py
示例5: upload
def upload(self, post_file, uuid=None):
filename = get_filename(post_file.filename)
content_type = post_file.type
in_file = post_file.file
bucket = self.connection.get_bucket(self.bucket)
if uuid is None:
uuid = uuid4().hex
path = '/'.join([format(i, 'x') for i in UUID(uuid).fields])
key = bucket.new_key(path)
else:
try:
path = '/'.join([format(i, 'x') for i in UUID(uuid).fields])
except ValueError:
raise KeyNotFound(uuid)
if path not in bucket:
raise KeyNotFound(uuid)
key = bucket.get_key(path)
if key.size != 0:
raise ContentUploaded(uuid)
md5 = key.get_metadata('hash')
if key.compute_md5(in_file)[0] != md5[4:]:
raise HashInvalid(md5)
key.set_metadata('Content-Type', content_type)
key.set_metadata("Content-Disposition", build_header(filename, filename_compat=quote(filename.encode('utf-8'))))
key.set_contents_from_file(in_file)
key.set_acl('private')
return uuid, 'md5:' + key.etag[1:-1], content_type, filename
开发者ID:openprocurement,项目名称:openprocurement.storage.s3,代码行数:27,代码来源:storage.py
示例6: create_mail_attachment
def create_mail_attachment(attdef, payload=None):
"""Create the MIME part corresponding to the given attachment.
Mandatory keys: 'fname', 'tmpname', 'content-type'
:param attdef: a dictionary containing the attachment definition
:return: a MIMEBase object
"""
from email import Encoders
from email.mime.base import MIMEBase
if "content-type" in attdef:
maintype, subtype = attdef["content-type"].split("/")
elif "Content-Type" in attdef:
maintype, subtype = attdef["Content-Type"].split("/")
else:
return None
res = MIMEBase(maintype, subtype)
if payload is None:
with open(os.path.join(
settings.MEDIA_ROOT, "webmail", attdef["tmpname"]), "rb") as fp:
res.set_payload(fp.read())
else:
res.set_payload(payload)
Encoders.encode_base64(res)
if isinstance(attdef['fname'], str):
attdef['fname'] = attdef['fname'].decode('utf-8')
res['Content-Disposition'] = build_header(attdef['fname'])
return res
开发者ID:digideskio,项目名称:modoboa-webmail,代码行数:29,代码来源:attachments.py
示例7: image
def image(self, subpath=None):
"""Return the image in a specific scale, either inline
(default) or as attachment.
:param subpath: [<image_scale>]/download] (optional).
When 'download' is the last element in subpath,
the image is served with a 'Content-Disposition:
attachment' header. <image_scale> has to be one of the
predefined image_scales - either from the defaults in
this module or one set with a
kotti.image_scales.<scale_name> in your app config ini
file.
:type subpath: str
:result: complete response object
:rtype: pyramid.response.Response
"""
if subpath is None:
subpath = self.request.subpath
width, height = (None, None)
subpath = list(subpath)
if (len(subpath) > 0) and (subpath[-1] == "download"):
disposition = "attachment"
subpath.pop()
else:
disposition = "inline"
if len(subpath) == 1:
scale = subpath[0]
if scale in image_scales:
# /path/to/image/scale/thumb
width, height = image_scales[scale]
if not (width and height):
return self.request.uploaded_file_response(
self.context.data, disposition)
image, format, size = scaleImage(self.context.data.file.read(),
width=width,
height=height,
direction="thumb")
res = Response(
headerlist=[
('Content-Disposition', '{0};filename="{1}"'.format(
disposition,
self.context.filename.encode('ascii', 'ignore'))),
('Content-Length', str(len(image))),
('Content-Type', str(self.context.mimetype)),
],
body=image,
)
res.content_disposition = rfc6266.build_header(
self.context.filename, disposition=disposition,
filename_compat=unidecode(self.context.filename))
return res
开发者ID:Kotti,项目名称:kotti_image,代码行数:59,代码来源:__init__.py
示例8: __init__
def __init__(self, f, request, disposition='attachment',
cache_max_age=604800, content_type=None,
content_encoding=None):
"""
:param f: the ``UploadedFile`` file field value.
:type f: :class:`depot.io.interfaces.StoredFile`
:param request: Current request.
:type request: :class:`pyramid.request.Request`
:param disposition:
:type disposition:
:param cache_max_age: The number of seconds that should be used to HTTP
cache this response.
:param content_type: The content_type of the response.
:param content_encoding: The content_encoding of the response.
It's generally safe to leave this set to
``None`` if you're serving a binary file.
This argument will be ignored if you also
leave ``content-type`` as ``None``.
"""
if f.public_url:
raise HTTPMovedPermanently(f.public_url)
content_encoding, content_type = self._get_type_and_encoding(
content_encoding, content_type, f)
super(StoredFileResponse, self).__init__(
conditional_response=True,
content_type=content_type,
content_encoding=content_encoding)
app_iter = None
if request is not None and \
not get_settings()['kotti.depot_replace_wsgi_file_wrapper']:
environ = request.environ
if 'wsgi.file_wrapper' in environ:
app_iter = environ['wsgi.file_wrapper'](f, _BLOCK_SIZE)
if app_iter is None:
app_iter = FileIter(f)
self.app_iter = app_iter
# assignment of content_length must come after assignment of app_iter
self.content_length = f.content_length
self.last_modified = f.last_modified
if cache_max_age is not None:
self.cache_expires = cache_max_age
self.cache_control.public = True
self.etag = self.generate_etag(f)
self.content_disposition = rfc6266.build_header(
f.filename, disposition=disposition,
filename_compat=unidecode(f.filename))
开发者ID:disko,项目名称:Kotti,代码行数:58,代码来源:filedepot.py
示例9: upload_file
def upload_file(request, blacklisted_fields=DOCUMENT_BLACKLISTED_FIELDS):
first_document = (
request.validated["documents"][0]
if "documents" in request.validated and request.validated["documents"]
else None
)
if request.content_type == "multipart/form-data":
data = request.validated["file"]
filename = get_filename(data)
content_type = data.type
in_file = data.file
else:
filename = first_document.title
content_type = request.content_type
in_file = request.body_file
if hasattr(request.context, "documents"):
# upload new document
model = type(request.context).documents.model_class
else:
# update document
model = type(request.context)
document = model({"title": filename, "format": content_type})
document.__parent__ = request.context
if "document_id" in request.validated:
document.id = request.validated["document_id"]
if first_document:
for attr_name in type(first_document)._fields:
if attr_name not in blacklisted_fields:
setattr(document, attr_name, getattr(first_document, attr_name))
key = generate_id()
document_route = request.matched_route.name.replace("collection_", "")
document_path = request.current_route_path(
_route_name=document_route, document_id=document.id, _query={"download": key}
)
document.url = "/" + "/".join(document_path.split("/")[3:])
conn = getattr(request.registry, "s3_connection", None)
if conn:
bucket = conn.get_bucket(request.registry.bucket_name)
filename = "{}/{}/{}".format(request.validated["plan_id"], document.id, key)
key = bucket.new_key(filename)
key.set_metadata("Content-Type", document.format)
key.set_metadata(
"Content-Disposition", build_header(document.title, filename_compat=quote(document.title.encode("utf-8")))
)
key.set_contents_from_file(in_file)
key.set_acl("private")
else:
filename = "{}_{}".format(document.id, key)
request.validated["plan"]["_attachments"][filename] = {
"content_type": document.format,
"data": b64encode(in_file.read()),
}
update_logging_context(request, {"file_size": in_file.tell()})
return document
开发者ID:openprocurement,项目名称:openprocurement.planning.api,代码行数:55,代码来源:utils.py
示例10: _export
def _export(content, filename):
"""Export a csv file's content
:param content: the content to export (string)
:param filename: the name that will appear into the response
:return: an ``HttpResponse`` object
"""
resp = HttpResponse(content)
resp["Content-Type"] = "text/csv"
resp["Content-Length"] = len(content)
resp["Content-Disposition"] = build_header(filename)
return resp
开发者ID:finid,项目名称:modoboa,代码行数:12,代码来源:export.py
示例11: download_filters_set
def download_filters_set(request, name):
sc = SieveClient(user=request.user.username,
password=request.session["password"])
try:
script = sc.getscript(name)
except SieveClientError as e:
return ajax_response(request, "ko", respmsg=str(e))
resp = HttpResponse(script)
resp["Content-Type"] = "text/plain; charset=utf-8"
resp["Content-Length"] = len(script)
resp["Content-Disposition"] = build_header('%s.txt' % name)
return resp
开发者ID:bearstech,项目名称:modoboa-sievefilters,代码行数:13,代码来源:views.py
示例12: upload_file
def upload_file(request, blacklisted_fields=DOCUMENT_BLACKLISTED_FIELDS):
first_document = request.validated['documents'][0] if 'documents' in request.validated and request.validated['documents'] else None
if request.content_type == 'multipart/form-data':
data = request.validated['file']
filename = get_filename(data)
content_type = data.type
in_file = data.file
else:
filename = first_document.title
content_type = request.content_type
in_file = request.body_file
if hasattr(request.context, "documents"):
# upload new document
model = type(request.context).documents.model_class
else:
# update document
model = type(request.context)
document = model({'title': filename, 'format': content_type})
document.__parent__ = request.context
if 'document_id' in request.validated:
document.id = request.validated['document_id']
if first_document:
for attr_name in type(first_document)._fields:
if attr_name not in blacklisted_fields:
setattr(document, attr_name, getattr(first_document, attr_name))
key = generate_id()
document_route = request.matched_route.name.replace("collection_", "")
document_path = request.current_route_path(_route_name=document_route, document_id=document.id, _query={'download': key})
document.url = '/' + '/'.join(document_path.split('/')[3:])
conn = getattr(request.registry, 's3_connection', None)
if conn:
bucket = conn.get_bucket(request.registry.bucket_name)
filename = "{}/{}/{}".format(request.validated['contract_id'],
document.id, key)
key = bucket.new_key(filename)
key.set_metadata('Content-Type', document.format)
key.set_metadata("Content-Disposition", build_header(document.title, filename_compat=quote(document.title.encode('utf-8'))))
key.set_contents_from_file(in_file)
key.set_acl('private')
else:
filename = "{}_{}".format(document.id, key)
request.validated['contract']['_attachments'][filename] = {
"content_type": document.format,
"data": b64encode(in_file.read())
}
update_logging_context(request, {'file_size': in_file.tell()})
return document
开发者ID:Leits,项目名称:openprocurement.contracting.api,代码行数:48,代码来源:utils.py
示例13: get_account_credentials
def get_account_credentials(request, accountid):
"""View to download a document."""
account = User.objects.get(pk=accountid)
if not request.user.can_access(account):
raise PermDeniedException()
fname = get_creds_filename(account)
if not os.path.exists(fname):
raise ModoboaException(_("No document available for this user"))
content = decrypt_file(fname)
if param_tools.get_global_parameter("delete_first_dl"):
os.remove(fname)
resp = HttpResponse(content)
resp["Content-Type"] = "application/pdf"
resp["Content-Length"] = len(content)
resp["Content-Disposition"] = build_header(os.path.basename(fname))
return resp
开发者ID:modoboa,项目名称:modoboa-pdfcredentials,代码行数:16,代码来源:views.py
示例14: zip_selected
def zip_selected(self, objects):
response = self.request.response
settings = getUtility(IRegistry).forInterface(IZipExportSettings)
with ZipGenerator() as generator:
for obj in objects:
repre = getMultiAdapter((obj, self.request),
interface=IZipRepresentation)
for path, pointer in repre.get_files():
if not pointer:
if settings.include_empty_folders:
generator.add_folder(path)
continue
try:
generator.add_file(path, pointer)
except LargeZipFile:
messages = IStatusMessage(self.request)
messages.add(_("statmsg_zip_file_too_big",
default=u"Content is too big "
"to export"),
type=u"error")
return self.request.response.redirect(
self.context.absolute_url())
# check if zip has files
if generator.is_empty:
raise NoExportableContent()
zip_file = generator.generate()
# Trigger the per container event
notify(ContainerZippedEvent(self.context))
# Generate response file
filename = u'%s.zip' % self.context.title
response.setHeader(
"Content-Disposition",
build_header(filename, disposition='attachment'))
response.setHeader("Content-type", "application/zip")
response.setHeader("Content-Length",
os.stat(zip_file.name).st_size)
return filestream_iterator(zip_file.name, 'rb')
开发者ID:4teamwork,项目名称:ftw.zipexport,代码行数:46,代码来源:zipexportview.py
示例15: create_mail_attachment
def create_mail_attachment(attdef):
"""Create the MIME part corresponding to the given attachment.
Mandatory keys: 'fname', 'tmpname', 'content-type'
:param attdef: a dictionary containing the attachment definition
:return: a MIMEBase object
"""
from email import Encoders
from email.mime.base import MIMEBase
maintype, subtype = attdef["content-type"].split("/")
res = MIMEBase(maintype, subtype)
fp = open(os.path.join(settings.MEDIA_ROOT, "webmail", attdef["tmpname"]), "rb")
res.set_payload(fp.read())
fp.close()
Encoders.encode_base64(res)
res['Content-Disposition'] = build_header(attdef['fname'].decode('utf-8'))
return res
开发者ID:ricardopadilha,项目名称:modoboa,代码行数:19,代码来源:lib.py
示例16: upload_file
def upload_file(request):
first_document = request.validated['documents'][0] if 'documents' in request.validated and request.validated['documents'] else None
if request.content_type == 'multipart/form-data':
data = request.validated['file']
filename = get_filename(data)
content_type = data.type
in_file = data.file
else:
filename = first_document.title
content_type = request.content_type
in_file = request.body_file
document = Document({
'title': filename,
'format': content_type
})
document.__parent__ = request.context
if 'document_id' in request.validated:
document.id = request.validated['document_id']
if first_document:
document.datePublished = first_document.datePublished
key = generate_id()
document_route = request.matched_route.name.replace("collection_", "")
document_path = request.current_route_path(_route_name=document_route, document_id=document.id, _query={'download': key})
document.url = '/tenders' + document_path.split('/tenders', 1)[1]
conn = getattr(request.registry, 's3_connection', None)
if conn:
bucket = conn.get_bucket(request.registry.bucket_name)
filename = "{}/{}/{}".format(request.validated['tender_id'], document.id, key)
key = bucket.new_key(filename)
key.set_metadata('Content-Type', document.format)
key.set_metadata("Content-Disposition", build_header(document.title, filename_compat=quote(document.title.encode('utf-8'))))
key.set_contents_from_file(in_file)
key.set_acl('private')
else:
filename = "{}_{}".format(document.id, key)
request.validated['tender']['_attachments'][filename] = {
"content_type": document.format,
"data": b64encode(in_file.read())
}
return document
开发者ID:spaceship-labs,项目名称:openprocurement.api,代码行数:40,代码来源:utils.py
示例17: upload
def upload(self, post_file, uuid=None):
filename = get_filename(post_file.filename)
content_type = post_file.type
in_file = post_file.file
if uuid is not None:
if uuid not in self.storage:
raise KeyNotFound(uuid)
if self.storage[uuid]['Content']:
raise ContentUploaded(uuid)
key = self.storage[uuid]
else:
uuid = uuid4().hex
key = self.storage[uuid] = {}
content = in_file.read()
key_md5 = key.get('hash')
md5hash = 'md5:' + md5(content).hexdigest()
if key_md5 and md5hash != key_md5:
raise HashInvalid(key_md5)
key['hash'] = md5hash
key['Content-Type'] = content_type
key["Content-Disposition"] = build_header(filename, filename_compat=quote(filename.encode('utf-8')))
key['Content'] = content
return uuid, md5hash, content_type, filename
开发者ID:openprocurement,项目名称:openprocurement.documentservice,代码行数:23,代码来源:storage.py
示例18: getattachment
def getattachment(request):
"""Fetch a message attachment
FIXME: par manque de caching, le bodystructure du message est
redemandé pour accéder aux headers de cette pièce jointe.
:param request: a ``Request`` object
"""
mbox = request.GET.get("mbox", None)
mailid = request.GET.get("mailid", None)
pnum = request.GET.get("partnumber", None)
fname = request.GET.get("fname", None)
if not mbox or not mailid or not pnum or not fname:
raise BadRequest(_("Invalid request"))
imapc = get_imapconnector(request)
partdef, payload = imapc.fetchpart(mailid, mbox, pnum)
resp = HttpResponse(decode_payload(partdef["encoding"], payload))
resp["Content-Type"] = partdef["Content-Type"]
resp["Content-Transfer-Encoding"] = partdef["encoding"]
resp["Content-Disposition"] = build_header(fname)
if int(partdef["size"]) < 200:
resp["Content-Length"] = partdef["size"]
return resp
开发者ID:jmfield2,项目名称:modoboa,代码行数:24,代码来源:views.py
示例19: make_content_disposition_header
def make_content_disposition_header(fn):
return build_header(os.path.basename(fn)).decode('ascii')
开发者ID:chrismattmann,项目名称:tika-python,代码行数:2,代码来源:tika.py
示例20: roundtrip
def roundtrip(filename):
return parse_headers(build_header(filename)).filename_unsafe
开发者ID:closeio,项目名称:rfc6266,代码行数:2,代码来源:test_rfc6266.py
注:本文中的rfc6266.build_header函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论