73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package album_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/album"
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
|
|
"gocloud.dev/blob/memblob"
|
|
)
|
|
|
|
// installAndLookup shows how application or plugin code obtains the published *album.Service.
|
|
func installAndLookup(app *backpack.App) (*album.Service, error) {
|
|
svc, err := album.Install(app)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if found, ok := album.ServiceFrom(app); ok {
|
|
return found, nil
|
|
}
|
|
return svc, nil
|
|
}
|
|
|
|
// ExampleStaticHandler demonstrates zero-DB-hit serving of an immutable media blob.
|
|
func ExampleStaticHandler() {
|
|
ctx := context.Background()
|
|
bucket := memblob.OpenBucket(nil)
|
|
defer bucket.Close()
|
|
|
|
key := "media/abc123456789/summer-banner.png"
|
|
_ = bucket.WriteAll(ctx, key, []byte("png-bytes"), attach.UploadWriterOptions("image/png"))
|
|
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/media/abc123456789/summer-banner.png", nil)
|
|
album.StaticHandler(bucket, "/media").ServeHTTP(rec, req)
|
|
|
|
fmt.Println(rec.Code)
|
|
fmt.Println(rec.Header().Get("Cache-Control"))
|
|
fmt.Println(rec.Header().Get("X-Content-Type-Options"))
|
|
// Output:
|
|
// 200
|
|
// public, max-age=31536000, immutable
|
|
// nosniff
|
|
}
|
|
|
|
// ExampleItem_Srcset demonstrates generating an HTML srcset string from recorded WebP variants.
|
|
func ExampleItem_Srcset() {
|
|
item := album.Item{
|
|
ShortID: "abc123456789",
|
|
BlobKey: "media/abc123456789/hero.jpg",
|
|
Variants: album.Variants{
|
|
{Width: 480, Height: 320, Key: "media/abc123456789/hero-480w.webp"},
|
|
{Width: 960, Height: 640, Key: "media/abc123456789/hero-960w.webp"},
|
|
},
|
|
}
|
|
fmt.Println(item.PublicURL(nil))
|
|
fmt.Println(item.Srcset(nil))
|
|
// Output:
|
|
// /media/abc123456789/hero.jpg
|
|
// /media/abc123456789/hero-480w.webp 480w, /media/abc123456789/hero-960w.webp 960w
|
|
}
|
|
|
|
func TestInstallAndLookup(t *testing.T) {
|
|
svc, err := installAndLookup(backpack.New(nil))
|
|
if err != nil || svc == nil {
|
|
t.Fatalf("installAndLookup: svc=%v err=%v", svc, err)
|
|
}
|
|
}
|