handleTutorialPoster: GET /tutorials/posters/{slug}. Cloud only. Serves the poster the catalog names for slug, fetched once from getpad.dev and kept in memory. Only an https://www.getpad.dev/ image is ever fetched.
(w http.ResponseWriter, r *http.Request)
| 242 | // poster the catalog names for slug, fetched once from getpad.dev and kept in |
| 243 | // memory. Only an https://www.getpad.dev/ image is ever fetched. |
| 244 | func (s *Server) handleTutorialPoster(w http.ResponseWriter, r *http.Request) { |
| 245 | if !s.cloudMode { |
| 246 | writeError(w, http.StatusNotFound, "not_found", "Not found") |
| 247 | return |
| 248 | } |
| 249 | slug := chi.URLParam(r, "slug") |
| 250 | cat := s.tutorialCatalogNow(r.Context()) |
| 251 | var src string |
| 252 | if cat != nil { |
| 253 | for _, e := range append(append([]tutorialEntry{}, cat.Tutorials...), cat.Demos...) { |
| 254 | if e.Slug == slug && e.Poster != nil { |
| 255 | src = *e.Poster |
| 256 | break |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | if src == "" { |
| 261 | writeError(w, http.StatusNotFound, "not_found", "Not found") |
| 262 | return |
| 263 | } |
| 264 | |
| 265 | c := &s.tutorials |
| 266 | c.mu.Lock() |
| 267 | p, ok := c.posters[src] |
| 268 | c.mu.Unlock() |
| 269 | if !ok { |
| 270 | data, ctype, err := s.fetchTutorialPoster(r.Context(), src) |
| 271 | if err != nil { |
| 272 | writeError(w, http.StatusBadGateway, "upstream_unavailable", "Poster unavailable") |
| 273 | return |
| 274 | } |
| 275 | p = tutorialPoster{data: data, contentType: ctype} |
| 276 | c.mu.Lock() |
| 277 | if c.posters == nil { |
| 278 | c.posters = map[string]tutorialPoster{} |
| 279 | } |
| 280 | c.posters[src] = p |
| 281 | c.mu.Unlock() |
| 282 | } |
| 283 | w.Header().Set("Content-Type", p.contentType) |
| 284 | w.Header().Set("Cache-Control", "public, max-age=3600") |
| 285 | w.Header().Set("X-Content-Type-Options", "nosniff") |
| 286 | _, _ = w.Write(p.data) |
| 287 | } |
| 288 | |
| 289 | func (s *Server) fetchTutorialPoster(ctx context.Context, src string) ([]byte, string, error) { |
| 290 | if !strings.HasPrefix(src, tutorialsPosterOrigin) { |
nothing calls this directly
no test coverage detected