Spaces:
Sleeping

File size: 1,083 Bytes
189ed5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Public guidance examples; distinct from the evaluated sample set."""
import json
from pathlib import Path
from fastapi import HTTPException
from fastapi.responses import FileResponse


def register_tutorial_routes(app, root):
    root = Path(root)
    data_root = root / 'data'
    manifest_path = data_root / 'tutorials.json'

    def examples():
        if not manifest_path.is_file():
            return []
        return json.loads(manifest_path.read_text())

    @app.get('/api/tutorials')
    def tutorials():
        return examples()

    @app.get('/api/tutorial-media/{name}')
    def tutorial_media(name: str):
        allowed = {Path(im['src']).name for row in examples() for im in row['images']}
        if name not in allowed or Path(name).name != name:
            raise HTTPException(404)
        base = (data_root / 'tutorials').resolve()
        path = (base / name).resolve()
        if not path.is_relative_to(base) or not path.is_file():
            raise HTTPException(404)
        return FileResponse(path, headers={'Cache-Control': 'public, max-age=3600'})