async def avatar(from_: str, code: str) -> quart.Response: try: etag = request.headers["if-none-match"] except KeyError: etag = None address = base64.urlsafe_b64decode(repad(from_)).decode("utf-8") info = await client.get_avatar(address, metadata_only=True) bin_hash = binascii.a2b_hex(info["sha1"]) new_etag = base64.urlsafe_b64encode(bin_hash).decode("ascii").rstrip("=") cache_ttl = timedelta(seconds=current_app.config.get( "AVATAR_CACHE_TTL", 300, )) response = Response("", mimetype=info["type"]) response.headers["etag"] = new_etag # XXX: It seems to me that quart expects localtime(?!) in this field... response.expires = datetime.now() + cache_ttl response.headers["Content-Security-Policy"] = \ "frame-ancestors 'none'; default-src 'none'; style-src 'unsafe-inline'" if etag is not None and new_etag == etag: response.status_code = 304 return response data = await client.get_avatar_data(address, info["sha1"]) if data is None: response.status_code = 404 return response response.status_code = 200 if request.method == "HEAD": response.content_length = len(data) return response response.set_data(data) return response
def good_response(fileobj, headers=None, download=False): headers = headers or {} content_type = fileobj.content_type or 'application/octet-stream' if not isinstance(content_type, str): content_type = content_type.decode('utf8') headers['Content-Type'] = content_type modified = fileobj.modified or fileobj.created headers['Last-Modified'] = modified.strftime("%a, %d %b %Y %H:%M:%S GMT") headers['x-amz-version-id'] = str(fileobj.rev) for key, value in fileobj.meta.items(): if isinstance(value, bytes): value = value.decode('utf8') else: value = str(value) headers['x-amz-meta-%s' % key] = value if download: headers[ 'Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename( fileobj.path) status = 200 headers['Accept-Ranges'] = 'bytes' brange = request.headers.get('range') body = b'' to_read = -1 ranger = None if not brange: etag = fileobj.meta.get('md5') or fileobj.meta.get('sha256') or '' if etag: headers['Etag'] = etag if etag and request.headers.get('if-none-match') == etag: status = 304 body = b'' to_read = 0 else: if_date = request.headers.get('If-Modified-Since', '') if if_date: ts = datetime.strptime(if_date, "%a, %d %b %Y %H:%M:%S %Z") if ts >= modified: status = 304 body = b'' to_read = 0 else: try: ranger = Range.from_header(brange) except ValueError: print('BAD RANGE HEADER', brange) else: fr = ranger.ranges[0] headers['Content-Range'] = ContentRange( ranger.units, fr.begin, fr.end, fileobj.length).to_header() fileobj.seek(fr.begin) to_read = (fr.end - fr.begin) + 1 status = 206 if request.method == 'HEAD': resp = Response('', headers=headers) resp.content_length = fileobj.length return resp if status >= 200 and (fileobj.length >= 65535 and (to_read == -1 or to_read >= 65535)): if to_read == -1: to_read = fileobj.length async def iterator(to_read): blocksize = getattr(fileobj, 'bs', 8192) # blocksize = 16384 with fileobj: while to_read > 0: chunk = await asynread(fileobj, min(to_read, blocksize)) if chunk: yield chunk to_read -= len(chunk) else: break return Response(iterator(to_read), status=status, headers=headers, content_type=content_type) else: if to_read: with fileobj: body = fileobj.read(to_read) # print(repr(body)) return Response(body, headers=headers, status=status, content_type=content_type)