Proxy media that aren't cached

This commit is contained in:
Thomas Sileo 2019-08-18 11:40:57 +02:00
parent 9746508dab
commit 5e4f57a8b3
2 changed files with 21 additions and 1 deletions

20
app.py
View file

@ -3,8 +3,10 @@ import logging
import os import os
import traceback import traceback
from datetime import datetime from datetime import datetime
from typing import Any
from uuid import uuid4 from uuid import uuid4
import requests
from bson.errors import InvalidId from bson.errors import InvalidId
from bson.objectid import ObjectId from bson.objectid import ObjectId
from flask import Flask from flask import Flask
@ -226,12 +228,30 @@ def robots_txt():
@app.route("/microblogpub-0.0.jsonld") @app.route("/microblogpub-0.0.jsonld")
def microblogpub_jsonld(): def microblogpub_jsonld():
"""Returns our AP context (embedded in activities @context)."""
return Response( return Response(
response=json.dumps(jsonld.MICROBLOGPUB), response=json.dumps(jsonld.MICROBLOGPUB),
headers={"Content-Type": "application/ld+json"}, headers={"Content-Type": "application/ld+json"},
) )
@app.route("/p/<path:url>")
def proxy(url: str) -> Any:
req_headers = {
k: v
for k, v in dict(request.headers).items()
if k.lower() not in ["host", "cookie"]
}
resp = requests.get(url, stream=True, headers=req_headers)
app.logger.info(f"proxied req {url}: {resp!r}")
def data():
for chunk in resp.raw.stream(decode_content=False):
yield chunk
return Response(data(), headers=dict(resp.raw.headers))
@app.route("/media/<media_id>") @app.route("/media/<media_id>")
@noindex @noindex
def serve_media(media_id): def serve_media(media_id):

View file

@ -249,7 +249,7 @@ def _get_file_url(url, size, kind) -> str:
# MEDIA_CACHE.cache(url, kind) # MEDIA_CACHE.cache(url, kind)
_logger.error(f"cache not available for {url}/{size}/{kind}") _logger.error(f"cache not available for {url}/{size}/{kind}")
return url return f"/p/{url}"
@filters.app_template_filter() @filters.app_template_filter()