| index b6f840f..cde07d9 100644 |
| --- a/internal/slides/assets.go |
| +++ b/internal/slides/assets.go |
| @@ -142,6 +142,37 @@ type presigner interface { |
| PresignGet(hash string, ttl time.Duration) (string, error) |
| } |
| |
| +// ThroughApp is a bound object store with its presigning hidden, so |
| +// every byte goes through this app: an upload is PUT to |
| +// /d/{sqid}/uploads/{key} (putUpload), a picture is streamed from |
| +// serveAsset, and an export from serveExport — the paths development's |
| +// blobs.Dir always took. |
| +// |
| +// Production binds the platform's bucket through it (cmd/slides). The |
| +// presigned design (spec §2.7) has the browser PUT to the bucket and |
| +// load pictures from it, and neither can work as deployed: the page's |
| +// CSP is default-src 'self' and img-src 'self' data: (serving.go), so |
| +// the browser refuses both, and the platform's bucket answers no CORS |
| +// for this origin. Found when the store was first bound, 2026-11-11 — |
| +// the path had never run. Going direct again needs the bucket's origin |
| +// in connect-src and img-src and a CORS rule on the bucket; until then |
| +// this is the path that works. The cost is the bytes passing through |
| +// one instance. On the way out a picture is streamed (serveAsset). On |
| +// the way in rastrillo's blobs.S3.Put holds the body in memory (twice, |
| +// as of v0.27), and registration reads it again to sniff and promote, |
| +// so one 20 MiB upload can take ~60 MiB for a moment — fine for one |
| +// team, and a rastrillo streaming Put is the fix when it is not. |
| +func ThroughApp(s blobs.Store) blobs.Store { |
| + if s == nil { |
| + return nil |
| + } |
| + return throughApp{s} |
| +} |
| + |
| +// throughApp embeds only the blobs.Store interface, so a type assertion |
| +// to presigner fails on it whatever the store underneath can do. |
| +type throughApp struct{ blobs.Store } |
| + |
| // presignTTL is how long a minted URL lives. An hour for a GET, because |
| // that is the life of one person reading a deck; the same for a PUT, |
| // because a 20 MiB file on a bad connection is not a five-minute job. |
| @@ -310,12 +341,10 @@ func (a *app) signUpload(w http.ResponseWriter, r *http.Request) { |
| } |
| out["url"] = url |
| } else { |
| - // blobs.Dir has nothing to sign, so development uploads the |
| - // same way through this app. Production with a real bucket |
| - // never reaches this branch and never proxies a byte — see |
| - // putUpload's own refusal. ObjectKey stays empty: a |
| - // content-addressed store picks the key itself, and putUpload |
| - // records which one. |
| + // A store that does not presign — blobs.Dir in development, and |
| + // production's bucket behind ThroughApp — takes the bytes |
| + // through this app. ObjectKey stays empty: a content-addressed |
| + // store picks the key itself, and putUpload records which one. |
| out["url"] = "/d/" + d.Sqid + "/uploads/" + key |
| } |
| if err := a.guarded(r.Context(), a.deckGuard(r.Context(), u, d), func(db *gorm.DB) error { return db.Create(&up).Error }); err != nil { |
| @@ -330,15 +359,13 @@ func (a *app) signUpload(w http.ResponseWriter, r *http.Request) { |
| |
| // --- PUT /d/{sqid}/uploads/{ref} --- |
| |
| -// putUpload is the development-only receiving end of the URL signUpload |
| -// hands back when the bound store cannot presign — blobs.Dir, a |
| -// directory beside the database. |
| +// putUpload is the receiving end of the URL signUpload hands back when |
| +// the bound store does not presign — blobs.Dir in development, and the |
| +// platform's bucket behind ThroughApp in production. |
| // |
| -// It refuses outright on a store that CAN presign, rather than quietly |
| -// working: the whole point of the presigned path is that 20 MiB does |
| -// not travel through an app process that has one small instance's |
| -// memory, and an endpoint that silently accepted it anyway would be a |
| -// performance cliff nobody could see in the code. |
| +// It refuses outright on a store that DOES presign: that store's uploads |
| +// go to the bucket, and an endpoint that also accepted them here would |
| +// be a second path nobody chose. |
| // |
| // It takes a staging key, not a ref, and resolves the receipt for it |
| // before it reads a byte (codex 1). Without that this endpoint was its |
| @@ -793,12 +820,19 @@ func (a *app) serveAsset(w http.ResponseWriter, r *http.Request, d Deck) { |
| return |
| } |
| defer rc.Close() |
| - raw, err := io.ReadAll(io.LimitReader(rc, maxAssetBytes+1)) |
| - if err != nil { |
| + // Streamed, not read whole: the bucket is served through this app |
| + // (ThroughApp), and a deck of large photos opened by a few readers |
| + // at once would otherwise hold 20 MiB per picture per request. The |
| + // sniff needs only the head (sniffImage reads 1 KiB), which is read |
| + // first and sent ahead of the rest. |
| + head := make([]byte, 1024) |
| + n, err := io.ReadFull(rc, head) |
| + if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) && !errors.Is(err, io.EOF) { |
| a.fail(w, r, "read an asset", err) |
| return |
| } |
| - kind := sniffImage(raw) |
| + head = head[:n] |
| + kind := sniffImage(head) |
| if kind == "" { |
| a.notFound(w, r) |
| return |
| @@ -808,7 +842,14 @@ func (a *app) serveAsset(w http.ResponseWriter, r *http.Request, d Deck) { |
| // is honest. Private rather than public — the address is the deck's |
| // capability, not the object's. |
| w.Header().Set("Cache-Control", "private, max-age=31536000, immutable") |
| - http.ServeContent(w, r, ref, time.Time{}, bytes.NewReader(raw)) |
| + // The ref is the content hash, so it is an honest strong validator. |
| + w.Header().Set("ETag", `"`+ref+`"`) |
| + if match := r.Header.Get("If-None-Match"); match != "" && strings.Contains(match, `"`+ref+`"`) { |
| + w.WriteHeader(http.StatusNotModified) |
| + return |
| + } |
| + w.Write(head) |
| + io.Copy(w, io.LimitReader(rc, maxAssetBytes-int64(len(head)))) |
| } |
| |
| // assetHeaders is the header set every served asset carries. |