feat(files): cache served files and add HTML text viewer
Build and Push Docker Container / build-and-push (push) Successful in 2m37s

File cache:
- Add my_modules/filecache with metadata, blob, validator and store layers
- Cache convex.get_file via AsyncCache, TTL clamped to the file expiry
- Skip caching for missing, incomplete or already expired records
- Add a process-local, size-bounded LRU for immutable storage blobs
- Build the cache stack from FILE_CACHE_* env vars in build_file_cache
- Attach the cache to the app in my_modules/app/setup.py
- Expose app-bound helpers in filecache/access.py for routes

Serving:
- Send strong ETag, Last-Modified and Cache-Control on served files
- Answer If-None-Match and If-Modified-Since with 304 from metadata only
- Avoid pulling the blob from Convex on a revalidation hit
- Add a short Cloudflare-CDN-Cache-Control edge window for deleted files
- Record cache_status in the wide event on a 304
- Invalidate cached metadata after file update and delete in both routes

Text viewer:
- Add my_modules/textview to render text/plain and text/markdown as HTML
- Linkify http(s) URLs and trim trailing sentence punctuation
- Resolve links pointing at this instance to the referenced file
- Inline image previews, mark missing and expired targets
- Cap resolution at MAX_LOOKUPS backend lookups per document
- Serve the viewer only for real browser navigations, never for curl or CLI
- Keep raw bytes for ?raw=1, downloads and Accept headers without text/html
- Add templates/side/views/webpage/files/text_view.htm with a raw link

Other:
- Add tests for cache layers, serving, text rendering and the route
- Bump multidict to 7.2.1 in uv.lock
- Update the my_helpers submodule pointer
This commit is contained in:
2026-10-25 18:54:46 +01:00
parent f6a70b5ced
commit 3db374b1ab
21 changed files with 1692 additions and 97 deletions
+3
View File
@@ -19,6 +19,7 @@ from quart import current_app
from my_modules.app.setup import LIMITER
from my_modules.expiry import ensure_utc, parse_expires
from my_modules.file_meta import format_size, iso_stamp_filename
from my_modules.filecache.access import drop_file_cache
from servicelink import Forbidden, InvalidParams, NotFound, Router, Unauthorized, any_verifier, bearer_verifier, create_link_blueprint, shared_secret_verifier
MAX_RPC_BODY = 16 * 1024 * 1024
@@ -109,6 +110,7 @@ async def files_update(params, ctx):
user_id=_user_id(ctx),
preserve_missing=True,
)
await drop_file_cache(file_id)
return {'updated': True}
@router.method('files.delete')
@@ -117,6 +119,7 @@ async def files_delete(params, ctx):
if not file_id:
raise InvalidParams('file_id is required')
await current_app.convex.delete_file(file_id, _user_id(ctx))
await drop_file_cache(file_id)
return {'deleted': True}
async def _decode_access_token(token):
+69 -7
View File
@@ -1,8 +1,14 @@
from io import BytesIO
from wsgiref.handlers import format_date_time as http_date
from my_modules.decoratory.header import login_required
from my_modules.functions import get_ip
from my_modules.app.setup import LIMITER
from my_modules.app.logger import logger
from my_modules.expiry import parse_expires
from my_modules.filecache import build_etag, cache_control, edge_cache_control, is_fresh
from my_modules.filecache.access import browser_max_age, drop_file_cache, edge_max_age, load_blob, load_file
from my_modules.textview import build_view_context, wants_text_view
from quart_common.web.wide_event import add_wide_event_context
from quart import (
@@ -12,6 +18,7 @@ from quart import (
Response,
send_file,
render_template,
make_response,
abort,
current_app,
jsonify,
@@ -25,11 +32,33 @@ def find_file(files: list[dict], file_id: str):
return file_data
return None
def request_host() -> str:
return (request.headers.get("X-Forwarded-Host") or request.host).split(",")[0].strip()
def build_share_url(file_id: str) -> str:
scheme = (request.headers.get("X-Forwarded-Proto") or request.scheme or "http").split(",")[0].strip()
host = (request.headers.get("X-Forwarded-Host") or request.host).split(",")[0].strip()
root_path = request.root_path.rstrip("/")
return f"{scheme}://{host}{root_path}/-{file_id}"
return f"{scheme}://{request_host()}{root_path}/-{file_id}"
async def render_text_view(file_id: str, file_data: dict):
"""Render a pasted text file as HTML so links are clickable and own files resolve."""
root_path = request.root_path.rstrip("/")
raw = await load_blob(file_data.get("db_image_url"))
async def lookup(linked_file_id: str):
return await load_file(linked_file_id)
context = await build_view_context(
raw=raw,
file_name=file_data.get("file_name") or file_id,
host=request_host(),
root_path=root_path,
raw_url=f"{root_path}/-{file_id}?raw=1",
lookup=lookup,
)
response = await make_response(await render_template("views/webpage/files/text_view.htm", **context))
response.headers["Cache-Control"] = "private, no-store"
return response
@side_main_bp.route('/')
@LIMITER.limit("20 per minute;500 per hour;")
@@ -115,6 +144,7 @@ async def file_edit_api(file_id, user):
expires_at=expires_at,
user_id=user["sub"],
)
await drop_file_cache(file_id)
return jsonify({"ok": True})
@@ -127,6 +157,7 @@ async def file_delete_api(file_id, user):
return jsonify({"ok": False, "error": "File not found"}), 404
await current_app.convex.delete_file(file_id=file_id, user_id=user["sub"])
await drop_file_cache(file_id)
return jsonify({"ok": True})
@side_main_bp.route("/-<path:file_id>")
@@ -138,7 +169,7 @@ async def serve_file(file_id: str):
abort(404)
add_wide_event_context(nanoshare={"operation": "serve_file", "file_id": file_id})
file_data = await current_app.convex.get_file(file_id=file_id)
file_data = await load_file(file_id)
disable_logging = False
if not file_data:
@@ -173,12 +204,43 @@ async def serve_file(file_id: str):
if not disable_logging:
await current_app.convex.add_file_access(file_id=file_id, ip_address=get_ip(), user_agent=request.user_agent, status="ok")
return await send_file(
filename_or_io=await current_app.convex.get_from_storage(file_data.get('db_image_url')),
if wants_text_view(
content_type=content_type,
accept_header=request.headers.get("Accept", ""),
raw_param=request.args.get("raw"),
force_download=force_download,
):
return await render_text_view(file_id=file_id, file_data=file_data)
last_modified = int(file_data["uploaded_at"]) / 1000
etag = build_etag(file_id, file_data, download=force_download)
expires_at = file_data.get("expires_at")
freshness = {
"ETag": etag,
"Last-Modified": http_date(last_modified),
"Cache-Control": cache_control(expires_at, browser_max_age()),
# Cloudflare-only, not proxied downstream: keeps the edge window short so a
# deleted or expired file stops being served long before the browser TTL ends.
"Cloudflare-CDN-Cache-Control": edge_cache_control(expires_at, edge_max_age()),
}
# Answered from metadata only, so a revalidation never pulls the blob from Convex.
if is_fresh(
request.headers.get("If-None-Match"),
request.headers.get("If-Modified-Since"),
etag,
last_modified,
):
add_wide_event_context(nanoshare={"cache_status": "not_modified"})
return Response(status=304, headers=freshness)
response = await send_file(
filename_or_io=BytesIO(await load_blob(file_data["db_image_url"])),
mimetype=content_type,
as_attachment=force_download,
attachment_filename=file_name,
conditional=True,
cache_timeout=60,
last_modified=int(file_data['uploaded_at']) / 1000
last_modified=last_modified,
)
response.headers.update(freshness)
return response