Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc8c0a6d9e | ||
|
|
394c7413c4 | ||
|
|
677e63329d | ||
|
|
1840a662d9 |
@@ -63,7 +63,7 @@ WHERE id = sqlc.arg(id);
|
||||
|
||||
-- name: CreatePostImage :exec
|
||||
INSERT INTO post_images (
|
||||
id, post_id, object_key, public_url, description, kind, position, width, height, created_at
|
||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
||||
)
|
||||
VALUES (
|
||||
sqlc.arg(id),
|
||||
@@ -71,7 +71,6 @@ VALUES (
|
||||
sqlc.arg(object_key),
|
||||
sqlc.arg(public_url),
|
||||
sqlc.arg(description),
|
||||
sqlc.arg(kind),
|
||||
sqlc.arg(position),
|
||||
sqlc.arg(width),
|
||||
sqlc.arg(height),
|
||||
@@ -84,7 +83,7 @@ WHERE post_id = sqlc.arg(post_id);
|
||||
|
||||
-- name: ListPostImages :many
|
||||
SELECT
|
||||
id, post_id, object_key, public_url, description, kind, position, width, height, created_at
|
||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
||||
FROM post_images
|
||||
WHERE post_id = sqlc.arg(post_id)
|
||||
ORDER BY position;
|
||||
@@ -103,7 +102,7 @@ WITH RECURSIVE thread AS (
|
||||
)
|
||||
SELECT
|
||||
images.id, images.post_id, images.object_key, images.public_url,
|
||||
images.description, images.kind, images.position, images.width, images.height, images.created_at
|
||||
images.description, images.position, images.width, images.height, images.created_at
|
||||
FROM post_images images
|
||||
JOIN thread ON thread.id = images.post_id
|
||||
ORDER BY images.post_id, images.position;
|
||||
|
||||
@@ -7,7 +7,6 @@ require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.43.7
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.37
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.107.3
|
||||
github.com/aws/smithy-go v1.27.8
|
||||
github.com/bwmarrin/discordgo v0.29.0
|
||||
github.com/go-chi/chi/v5 v5.3.1
|
||||
github.com/google/uuid v1.6.0
|
||||
@@ -28,6 +27,7 @@ require (
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.31 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.38 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.39 // indirect
|
||||
github.com/aws/smithy-go v1.27.8 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
|
||||
@@ -8,11 +8,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"github.com/aws/smithy-go/middleware"
|
||||
)
|
||||
|
||||
// Uploader stores public avatar objects.
|
||||
@@ -83,23 +81,10 @@ func NewSpaces(cfg SpacesConfig) Uploader {
|
||||
Region: cfg.Region,
|
||||
Credentials: credentials.NewStaticCredentialsProvider(cfg.Key, cfg.Secret, ""),
|
||||
BaseEndpoint: aws.String(cfg.Endpoint),
|
||||
RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
|
||||
APIOptions: []func(*middleware.Stack) error{
|
||||
spacesUnsignedPayload,
|
||||
},
|
||||
})
|
||||
return &spaces{client: client, cfg: cfg}
|
||||
}
|
||||
|
||||
// spacesUnsignedPayload signs Spaces PUTs as UNSIGNED-PAYLOAD so the client
|
||||
// can stream the body without hashing it first.
|
||||
func spacesUnsignedPayload(stack *middleware.Stack) error {
|
||||
if err := v4.SwapComputePayloadSHA256ForUnsignedPayloadMiddleware(stack); err != nil {
|
||||
return v4.AddUnsignedPayloadMiddleware(stack)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *spaces) Enabled() bool { return true }
|
||||
|
||||
func (s *spaces) Upload(ctx context.Context, obj FileUpload) (string, error) {
|
||||
|
||||
@@ -158,32 +158,6 @@ CREATE UNIQUE INDEX IF NOT EXISTS discord_post_links_thread_uidx
|
||||
return nil
|
||||
}
|
||||
|
||||
func migratePostImageVideo(ctx context.Context, exec execContext) error {
|
||||
steps := []struct {
|
||||
name string
|
||||
sql string
|
||||
}{
|
||||
{"add kind", `ALTER TABLE post_images ADD COLUMN IF NOT EXISTS kind TEXT NOT NULL DEFAULT 'image'`},
|
||||
{"drop kind check", `ALTER TABLE post_images DROP CONSTRAINT IF EXISTS post_images_kind_check`},
|
||||
{"add kind check", `ALTER TABLE post_images ADD CONSTRAINT post_images_kind_check CHECK (kind IN ('image', 'video'))`},
|
||||
{"drop width check", `ALTER TABLE post_images DROP CONSTRAINT IF EXISTS post_images_width_check`},
|
||||
{"drop height check", `ALTER TABLE post_images DROP CONSTRAINT IF EXISTS post_images_height_check`},
|
||||
{"add width check", `ALTER TABLE post_images ADD CONSTRAINT post_images_width_check CHECK (width >= 0)`},
|
||||
{"add height check", `ALTER TABLE post_images ADD CONSTRAINT post_images_height_check CHECK (height >= 0)`},
|
||||
{"drop image dims check", `ALTER TABLE post_images DROP CONSTRAINT IF EXISTS post_images_image_dims_check`},
|
||||
{"add image dims check", `ALTER TABLE post_images ADD CONSTRAINT post_images_image_dims_check CHECK (kind <> 'image' OR (width > 0 AND height > 0))`},
|
||||
{"drop position check", `ALTER TABLE post_images DROP CONSTRAINT IF EXISTS post_images_position_check`},
|
||||
{"add position check", `ALTER TABLE post_images ADD CONSTRAINT post_images_position_check CHECK (position BETWEEN 0 AND 4)`},
|
||||
{"one video index", `CREATE UNIQUE INDEX IF NOT EXISTS post_images_one_video_uidx ON post_images (post_id) WHERE kind = 'video'`},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if _, err := exec.ExecContext(ctx, step.sql); err != nil {
|
||||
return fmt.Errorf("%s: %w", step.name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func migratePostDate(ctx context.Context, exec execContext) error {
|
||||
steps := []struct {
|
||||
name string
|
||||
@@ -377,7 +351,6 @@ CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
{"009_drop_legacy_post_tables", migrateDropLegacyPostTables},
|
||||
{"010_post_images", migratePostImages},
|
||||
{"011_discord_post_links", migrateDiscordPostLinks},
|
||||
{"012_post_image_video", migratePostImageVideo},
|
||||
}
|
||||
for _, m := range migrations {
|
||||
if applied[m.version] {
|
||||
|
||||
@@ -87,12 +87,6 @@ CREATE TABLE users (
|
||||
if err := migrateDiscordPostLinks(ctx, conn); err != nil {
|
||||
t.Fatalf("discord post links migration is not idempotent: %v", err)
|
||||
}
|
||||
if err := migratePostImageVideo(ctx, conn); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := migratePostImageVideo(ctx, conn); err != nil {
|
||||
t.Fatalf("post image video migration is not idempotent: %v", err)
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, `
|
||||
INSERT INTO users (id, name, role)
|
||||
VALUES ('homeowner', 'Home Owner', 'user'), ('plumber', 'The Plumber', 'admin');
|
||||
@@ -124,9 +118,9 @@ VALUES ('homeowner', 'root-1', 1);`); err != nil {
|
||||
}
|
||||
imageQueries := sqlc.New(conn)
|
||||
for _, image := range []sqlc.CreatePostImageParams{
|
||||
{ID: "root-image-1", PostID: "root-1", ObjectKey: "posts/root-1/1.jpg", PublicUrl: "https://cdn.example/root-1.jpg", Description: "Valve", Kind: "image", Position: 0, Width: 1200, Height: 900, CreatedAt: "2026-08-26T08:00:00Z"},
|
||||
{ID: "root-image-2", PostID: "root-1", ObjectKey: "posts/root-1/2.png", PublicUrl: "https://cdn.example/root-2.png", Kind: "image", Position: 1, Width: 900, Height: 1200, CreatedAt: "2026-08-26T08:00:00Z"},
|
||||
{ID: "reply-image-1", PostID: "reply-1", ObjectKey: "posts/reply-1/1.jpg", PublicUrl: "https://cdn.example/reply-1.jpg", Description: "Cartridge", Kind: "image", Position: 0, Width: 1000, Height: 1000, CreatedAt: "2026-08-26T09:00:00Z"},
|
||||
{ID: "root-image-1", PostID: "root-1", ObjectKey: "posts/root-1/1.jpg", PublicUrl: "https://cdn.example/root-1.jpg", Description: "Valve", Position: 0, Width: 1200, Height: 900, CreatedAt: "2026-08-26T08:00:00Z"},
|
||||
{ID: "root-image-2", PostID: "root-1", ObjectKey: "posts/root-1/2.png", PublicUrl: "https://cdn.example/root-2.png", Position: 1, Width: 900, Height: 1200, CreatedAt: "2026-08-26T08:00:00Z"},
|
||||
{ID: "reply-image-1", PostID: "reply-1", ObjectKey: "posts/reply-1/1.jpg", PublicUrl: "https://cdn.example/reply-1.jpg", Description: "Cartridge", Position: 0, Width: 1000, Height: 1000, CreatedAt: "2026-08-26T09:00:00Z"},
|
||||
} {
|
||||
if err := imageQueries.CreatePostImage(ctx, image); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -150,24 +144,10 @@ VALUES ('homeowner', 'root-1', 1);`); err != nil {
|
||||
}
|
||||
if err := imageQueries.CreatePostImage(ctx, sqlc.CreatePostImageParams{
|
||||
ID: "too-many", PostID: "root-1", ObjectKey: "posts/root-1/5.jpg",
|
||||
PublicUrl: "https://cdn.example/root-5.jpg", Kind: "image", Position: 5,
|
||||
PublicUrl: "https://cdn.example/root-5.jpg", Position: 4,
|
||||
Width: 100, Height: 100, CreatedAt: "2026-08-26T08:00:00Z",
|
||||
}); err == nil {
|
||||
t.Fatal("position 5 unexpectedly succeeded")
|
||||
}
|
||||
if err := imageQueries.CreatePostImage(ctx, sqlc.CreatePostImageParams{
|
||||
ID: "root-video-1", PostID: "root-1", ObjectKey: "posts/root-1/clip.mp4",
|
||||
PublicUrl: "https://cdn.example/clip.mp4", Kind: "video", Position: 2,
|
||||
Width: 0, Height: 0, CreatedAt: "2026-08-26T08:00:00Z",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := imageQueries.CreatePostImage(ctx, sqlc.CreatePostImageParams{
|
||||
ID: "root-video-2", PostID: "root-1", ObjectKey: "posts/root-1/clip-2.mp4",
|
||||
PublicUrl: "https://cdn.example/clip-2.mp4", Kind: "video", Position: 3,
|
||||
Width: 0, Height: 0, CreatedAt: "2026-08-26T08:00:00Z",
|
||||
}); err == nil {
|
||||
t.Fatal("second video unexpectedly succeeded")
|
||||
t.Fatal("fifth image position unexpectedly succeeded")
|
||||
}
|
||||
if err := imageQueries.UpsertDiscordPostLink(ctx, sqlc.UpsertDiscordPostLinkParams{
|
||||
PostID: "root-1",
|
||||
|
||||
+3
-32
@@ -29,20 +29,16 @@ const (
|
||||
PostStateHidden PostState = "hidden"
|
||||
PostStateLocked PostState = "locked"
|
||||
MaxPostImages = 4
|
||||
MaxPostVideos = 1
|
||||
MaxImageDescriptionRunes = 500
|
||||
MediaKindImage = "image"
|
||||
MediaKindVideo = "video"
|
||||
)
|
||||
|
||||
// PostImage is one ordered public image or video attached to a post.
|
||||
// PostImage is one ordered public image attached to a post.
|
||||
type PostImage struct {
|
||||
ID string
|
||||
PostID string
|
||||
ObjectKey string
|
||||
PublicURL string
|
||||
Description string
|
||||
Kind string
|
||||
Position int
|
||||
Width int
|
||||
Height int
|
||||
@@ -204,13 +200,12 @@ func preparePost(p *Post) error {
|
||||
}
|
||||
|
||||
func preparePostImages(p *Post) error {
|
||||
if len(p.Images) > MaxPostImages+MaxPostVideos {
|
||||
return fmt.Errorf("%w: at most %d images and %d video are allowed", ErrInvalidPost, MaxPostImages, MaxPostVideos)
|
||||
if len(p.Images) > MaxPostImages {
|
||||
return fmt.Errorf("%w: at most %d images are allowed", ErrInvalidPost, MaxPostImages)
|
||||
}
|
||||
ids := make(map[string]bool, len(p.Images))
|
||||
keys := make(map[string]bool, len(p.Images))
|
||||
now := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
images, videos := 0, 0
|
||||
for i := range p.Images {
|
||||
image := &p.Images[i]
|
||||
image.ID = strings.TrimSpace(image.ID)
|
||||
@@ -218,13 +213,6 @@ func preparePostImages(p *Post) error {
|
||||
image.ObjectKey = strings.TrimSpace(image.ObjectKey)
|
||||
image.PublicURL = strings.TrimSpace(image.PublicURL)
|
||||
image.Description = strings.TrimSpace(image.Description)
|
||||
image.Kind = strings.TrimSpace(image.Kind)
|
||||
if image.Kind == "" {
|
||||
image.Kind = MediaKindImage
|
||||
}
|
||||
if image.Kind != MediaKindImage && image.Kind != MediaKindVideo {
|
||||
return fmt.Errorf("%w: invalid media kind", ErrInvalidPost)
|
||||
}
|
||||
if image.ID == "" {
|
||||
image.ID = uuid.NewString()
|
||||
}
|
||||
@@ -240,19 +228,10 @@ func preparePostImages(p *Post) error {
|
||||
if len([]rune(image.Description)) > MaxImageDescriptionRunes {
|
||||
return fmt.Errorf("%w: image description is too long", ErrInvalidPost)
|
||||
}
|
||||
if image.Kind == MediaKindImage {
|
||||
images++
|
||||
if image.Width <= 0 || image.Height <= 0 ||
|
||||
image.Width > math.MaxInt32 || image.Height > math.MaxInt32 {
|
||||
return fmt.Errorf("%w: invalid image dimensions", ErrInvalidPost)
|
||||
}
|
||||
} else {
|
||||
videos++
|
||||
if image.Width < 0 || image.Height < 0 ||
|
||||
image.Width > math.MaxInt32 || image.Height > math.MaxInt32 {
|
||||
return fmt.Errorf("%w: invalid video dimensions", ErrInvalidPost)
|
||||
}
|
||||
}
|
||||
if ids[image.ID] || keys[image.ObjectKey] {
|
||||
return fmt.Errorf("%w: duplicate image", ErrInvalidPost)
|
||||
}
|
||||
@@ -263,12 +242,6 @@ func preparePostImages(p *Post) error {
|
||||
image.CreatedAt = now
|
||||
}
|
||||
}
|
||||
if images > MaxPostImages {
|
||||
return fmt.Errorf("%w: at most %d images are allowed", ErrInvalidPost, MaxPostImages)
|
||||
}
|
||||
if videos > MaxPostVideos {
|
||||
return fmt.Errorf("%w: at most %d video is allowed", ErrInvalidPost, MaxPostVideos)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -280,7 +253,6 @@ func createPostImages(ctx context.Context, q *sqlc.Queries, images []PostImage)
|
||||
ObjectKey: image.ObjectKey,
|
||||
PublicUrl: image.PublicURL,
|
||||
Description: image.Description,
|
||||
Kind: image.Kind,
|
||||
Position: int16(image.Position),
|
||||
Width: int32(image.Width),
|
||||
Height: int32(image.Height),
|
||||
@@ -299,7 +271,6 @@ func postImageFromSQL(image sqlc.PostImage) PostImage {
|
||||
ObjectKey: image.ObjectKey,
|
||||
PublicURL: image.PublicUrl,
|
||||
Description: image.Description,
|
||||
Kind: image.Kind,
|
||||
Position: int(image.Position),
|
||||
Width: int(image.Width),
|
||||
Height: int(image.Height),
|
||||
|
||||
@@ -338,36 +338,6 @@ func TestMemoryPostImages(t *testing.T) {
|
||||
if err := mem.CreatePost(ctx, tooMany); !errors.Is(err, ErrInvalidPost) {
|
||||
t.Fatalf("five-image create error = %v, want ErrInvalidPost", err)
|
||||
}
|
||||
|
||||
withVideo := &Post{
|
||||
AuthorID: homeowner.ID,
|
||||
Title: "With video",
|
||||
Body: "Four photos and a clip.",
|
||||
Images: append(validPostImages(4), PostImage{
|
||||
ID: "clip-1",
|
||||
ObjectKey: "posts/clip-1.mp4",
|
||||
PublicURL: "https://cdn.example/clip-1.mp4",
|
||||
Kind: MediaKindVideo,
|
||||
}),
|
||||
}
|
||||
if err := mem.CreatePost(ctx, withVideo); err != nil {
|
||||
t.Fatalf("four images and one video: %v", err)
|
||||
}
|
||||
if withVideo.Images[4].Kind != MediaKindVideo || withVideo.Images[4].Position != 4 {
|
||||
t.Fatalf("video not stored: %+v", withVideo.Images[4])
|
||||
}
|
||||
twoVideos := &Post{
|
||||
AuthorID: homeowner.ID,
|
||||
Title: "Two clips",
|
||||
Body: "Not allowed.",
|
||||
Images: []PostImage{
|
||||
{ObjectKey: "posts/a.mp4", PublicURL: "https://cdn.example/a.mp4", Kind: MediaKindVideo},
|
||||
{ObjectKey: "posts/b.mp4", PublicURL: "https://cdn.example/b.mp4", Kind: MediaKindVideo},
|
||||
},
|
||||
}
|
||||
if err := mem.CreatePost(ctx, twoVideos); !errors.Is(err, ErrInvalidPost) {
|
||||
t.Fatalf("two-video create error = %v, want ErrInvalidPost", err)
|
||||
}
|
||||
}
|
||||
|
||||
func validPostImages(count int) []PostImage {
|
||||
|
||||
@@ -35,7 +35,6 @@ type PostImage struct {
|
||||
ObjectKey string
|
||||
PublicUrl string
|
||||
Description string
|
||||
Kind string
|
||||
Position int16
|
||||
Width int32
|
||||
Height int32
|
||||
|
||||
@@ -59,7 +59,7 @@ func (q *Queries) CreatePost(ctx context.Context, arg CreatePostParams) error {
|
||||
|
||||
const createPostImage = `-- name: CreatePostImage :exec
|
||||
INSERT INTO post_images (
|
||||
id, post_id, object_key, public_url, description, kind, position, width, height, created_at
|
||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
@@ -70,8 +70,7 @@ VALUES (
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9,
|
||||
$10
|
||||
$9
|
||||
)
|
||||
`
|
||||
|
||||
@@ -81,7 +80,6 @@ type CreatePostImageParams struct {
|
||||
ObjectKey string
|
||||
PublicUrl string
|
||||
Description string
|
||||
Kind string
|
||||
Position int16
|
||||
Width int32
|
||||
Height int32
|
||||
@@ -95,7 +93,6 @@ func (q *Queries) CreatePostImage(ctx context.Context, arg CreatePostImageParams
|
||||
arg.ObjectKey,
|
||||
arg.PublicUrl,
|
||||
arg.Description,
|
||||
arg.Kind,
|
||||
arg.Position,
|
||||
arg.Width,
|
||||
arg.Height,
|
||||
@@ -204,7 +201,7 @@ func (q *Queries) GetRootPostVoteSummary(ctx context.Context, arg GetRootPostVot
|
||||
|
||||
const listPostImages = `-- name: ListPostImages :many
|
||||
SELECT
|
||||
id, post_id, object_key, public_url, description, kind, position, width, height, created_at
|
||||
id, post_id, object_key, public_url, description, position, width, height, created_at
|
||||
FROM post_images
|
||||
WHERE post_id = $1
|
||||
ORDER BY position
|
||||
@@ -225,7 +222,6 @@ func (q *Queries) ListPostImages(ctx context.Context, postID string) ([]PostImag
|
||||
&i.ObjectKey,
|
||||
&i.PublicUrl,
|
||||
&i.Description,
|
||||
&i.Kind,
|
||||
&i.Position,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
@@ -331,7 +327,7 @@ WITH RECURSIVE thread AS (
|
||||
)
|
||||
SELECT
|
||||
images.id, images.post_id, images.object_key, images.public_url,
|
||||
images.description, images.kind, images.position, images.width, images.height, images.created_at
|
||||
images.description, images.position, images.width, images.height, images.created_at
|
||||
FROM post_images images
|
||||
JOIN thread ON thread.id = images.post_id
|
||||
ORDER BY images.post_id, images.position
|
||||
@@ -352,7 +348,6 @@ func (q *Queries) ListPostThreadImages(ctx context.Context, rootID string) ([]Po
|
||||
&i.ObjectKey,
|
||||
&i.PublicUrl,
|
||||
&i.Description,
|
||||
&i.Kind,
|
||||
&i.Position,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
|
||||
@@ -55,9 +55,6 @@ func (s *Server) postEvent(post, root *store.Post, author *store.User) events.Po
|
||||
if n := len(post.Images); n > 0 {
|
||||
ev.Images = make([]events.Image, 0, n)
|
||||
for _, img := range post.Images {
|
||||
if img.Kind == store.MediaKindVideo {
|
||||
continue
|
||||
}
|
||||
ev.Images = append(ev.Images, events.Image{
|
||||
URL: img.PublicURL,
|
||||
Description: img.Description,
|
||||
|
||||
@@ -236,21 +236,3 @@ func assertPostEvent(t *testing.T, got, want events.PostEvent) {
|
||||
t.Fatalf("event = %+v, want %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostEventOmitsVideos(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv, _ := newTestServer(t, Config{})
|
||||
got := srv.postEvent(&store.Post{
|
||||
ID: "root-1",
|
||||
Title: "Clip",
|
||||
Body: "Photo and video.",
|
||||
Images: []store.PostImage{
|
||||
{PublicURL: "https://cdn.example/a.jpg", Description: "Still", Kind: store.MediaKindImage},
|
||||
{PublicURL: "https://cdn.example/a.mp4", Description: "Walkthrough", Kind: store.MediaKindVideo},
|
||||
},
|
||||
}, nil, nil)
|
||||
if len(got.Images) != 1 || got.Images[0].URL != "https://cdn.example/a.jpg" {
|
||||
t.Fatalf("event images = %+v", got.Images)
|
||||
}
|
||||
}
|
||||
|
||||
+13
-175
@@ -28,8 +28,7 @@ import (
|
||||
const (
|
||||
defaultRequestBodyBytes = 3 << 20
|
||||
postImageMaxFileBytes = 5 << 20
|
||||
postVideoMaxFileBytes = 25 << 20
|
||||
postImageMaxRequestBytes = 50 << 20 // 4 images + 1 video + form fields
|
||||
postImageMaxRequestBytes = 22 << 20
|
||||
postImageMultipartMemory = 2 << 20
|
||||
postImageMaxSourceDim = 6000
|
||||
postImageMaxSourcePixels = 16_000_000
|
||||
@@ -68,7 +67,7 @@ func parsePostMutationForm(w http.ResponseWriter, r *http.Request) (func(), bool
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil && strings.HasPrefix(strings.ToLower(contentType), "multipart/") {
|
||||
http.Error(w, "Could not read upload.", http.StatusBadRequest)
|
||||
http.Error(w, "Could not read image upload.", http.StatusBadRequest)
|
||||
return func() {}, false
|
||||
}
|
||||
if mediaType != "multipart/form-data" {
|
||||
@@ -98,10 +97,10 @@ func writePostImageRequestError(w http.ResponseWriter, err error) {
|
||||
}
|
||||
var maxErr *http.MaxBytesError
|
||||
if errors.As(err, &maxErr) {
|
||||
http.Error(w, "Upload is too large.", http.StatusRequestEntityTooLarge)
|
||||
http.Error(w, "Image upload is too large.", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Could not read upload.", http.StatusBadRequest)
|
||||
http.Error(w, "Could not read image upload.", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func (s *Server) postImagesFromForm(
|
||||
@@ -122,23 +121,9 @@ func (s *Server) postImagesFromForm(
|
||||
if len(descriptions) > len(files) {
|
||||
return nil, nil, invalidPostImage("Image descriptions do not match selected images.", nil)
|
||||
}
|
||||
kinds := make([]string, len(files))
|
||||
newImages, newVideos := 0, 0
|
||||
for i, header := range files {
|
||||
kinds[i] = sniffPostMedia(header)
|
||||
if kinds[i] == store.MediaKindVideo {
|
||||
newVideos++
|
||||
} else {
|
||||
newImages++
|
||||
}
|
||||
}
|
||||
retainedImages, retainedVideos := countPostMedia(retained)
|
||||
if retainedImages+newImages > store.MaxPostImages {
|
||||
if len(retained)+len(files) > store.MaxPostImages {
|
||||
return nil, nil, invalidPostImage("You can attach up to 4 images.", nil)
|
||||
}
|
||||
if retainedVideos+newVideos > store.MaxPostVideos {
|
||||
return nil, nil, invalidPostImage("You can attach one video.", nil)
|
||||
}
|
||||
if len(files) > 0 && !s.cfg.Blob.Enabled() {
|
||||
return nil, nil, &postImageRequestError{
|
||||
status: http.StatusServiceUnavailable,
|
||||
@@ -157,68 +142,11 @@ func (s *Server) postImagesFromForm(
|
||||
s.deletePostImageObjects(newKeys)
|
||||
return nil, nil, invalidPostImage("Image descriptions must be 500 characters or fewer.", nil)
|
||||
}
|
||||
item, objectKey, err := s.uploadPostMedia(ctx, postID, header, kinds[i], description)
|
||||
prepared, err := preparePostImage(header)
|
||||
if err != nil {
|
||||
s.deletePostImageObjects(newKeys)
|
||||
return nil, nil, err
|
||||
}
|
||||
newKeys = append(newKeys, objectKey)
|
||||
images = append(images, item)
|
||||
}
|
||||
return images, newKeys, nil
|
||||
}
|
||||
|
||||
func countPostMedia(items []store.PostImage) (images, videos int) {
|
||||
for _, item := range items {
|
||||
if item.Kind == store.MediaKindVideo {
|
||||
videos++
|
||||
} else {
|
||||
images++
|
||||
}
|
||||
}
|
||||
return images, videos
|
||||
}
|
||||
|
||||
func (s *Server) uploadPostMedia(
|
||||
ctx context.Context,
|
||||
postID string,
|
||||
header *multipart.FileHeader,
|
||||
kind, description string,
|
||||
) (store.PostImage, string, error) {
|
||||
if kind == store.MediaKindVideo {
|
||||
prepared, err := preparePostVideo(header)
|
||||
if err != nil {
|
||||
return store.PostImage{}, "", err
|
||||
}
|
||||
defer prepared.body.Close()
|
||||
mediaID := uuid.NewString()
|
||||
objectKey := path.Join("post-videos", postID, mediaID+prepared.extension)
|
||||
publicURL, err := s.cfg.Blob.Upload(ctx, blob.FileUpload{
|
||||
Key: objectKey,
|
||||
Body: prepared.body,
|
||||
ContentType: prepared.contentType,
|
||||
Size: prepared.size,
|
||||
})
|
||||
if err != nil {
|
||||
return store.PostImage{}, "", &postImageRequestError{
|
||||
status: http.StatusServiceUnavailable,
|
||||
message: "Could not upload video. Try again later.",
|
||||
cause: err,
|
||||
}
|
||||
}
|
||||
return store.PostImage{
|
||||
ID: mediaID,
|
||||
PostID: postID,
|
||||
ObjectKey: objectKey,
|
||||
PublicURL: publicURL,
|
||||
Description: description,
|
||||
Kind: store.MediaKindVideo,
|
||||
}, objectKey, nil
|
||||
}
|
||||
prepared, err := preparePostImage(header)
|
||||
if err != nil {
|
||||
return store.PostImage{}, "", err
|
||||
}
|
||||
imageID := uuid.NewString()
|
||||
objectKey := path.Join("post-images", postID, imageID+prepared.extension)
|
||||
publicURL, err := s.cfg.Blob.Upload(ctx, blob.FileUpload{
|
||||
@@ -228,22 +156,25 @@ func (s *Server) uploadPostMedia(
|
||||
Size: int64(len(prepared.body)),
|
||||
})
|
||||
if err != nil {
|
||||
return store.PostImage{}, "", &postImageRequestError{
|
||||
s.deletePostImageObjects(newKeys)
|
||||
return nil, nil, &postImageRequestError{
|
||||
status: http.StatusServiceUnavailable,
|
||||
message: "Could not upload image. Try again later.",
|
||||
cause: err,
|
||||
}
|
||||
}
|
||||
return store.PostImage{
|
||||
newKeys = append(newKeys, objectKey)
|
||||
images = append(images, store.PostImage{
|
||||
ID: imageID,
|
||||
PostID: postID,
|
||||
ObjectKey: objectKey,
|
||||
PublicURL: publicURL,
|
||||
Description: description,
|
||||
Kind: store.MediaKindImage,
|
||||
Width: prepared.width,
|
||||
Height: prepared.height,
|
||||
}, objectKey, nil
|
||||
})
|
||||
}
|
||||
return images, newKeys, nil
|
||||
}
|
||||
|
||||
func retainedPostImages(form *multipart.Form, existing []store.PostImage) ([]store.PostImage, error) {
|
||||
@@ -367,99 +298,6 @@ func preparePostImage(header *multipart.FileHeader) (preparedPostImage, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func sniffPostMedia(header *multipart.FileHeader) string {
|
||||
if header == nil {
|
||||
return store.MediaKindImage
|
||||
}
|
||||
file, err := header.Open()
|
||||
if err != nil {
|
||||
return store.MediaKindImage
|
||||
}
|
||||
defer file.Close()
|
||||
peek := make([]byte, 512)
|
||||
n, err := io.ReadFull(file, peek)
|
||||
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) && !errors.Is(err, io.EOF) {
|
||||
return store.MediaKindImage
|
||||
}
|
||||
return mediaKindFromBytes(peek[:n])
|
||||
}
|
||||
|
||||
func mediaKindFromBytes(raw []byte) string {
|
||||
switch http.DetectContentType(raw) {
|
||||
case "video/mp4", "video/webm":
|
||||
return store.MediaKindVideo
|
||||
case "image/jpeg", "image/png", "image/webp":
|
||||
return store.MediaKindImage
|
||||
}
|
||||
if isMP4(raw) || isWebM(raw) {
|
||||
return store.MediaKindVideo
|
||||
}
|
||||
return store.MediaKindImage
|
||||
}
|
||||
|
||||
func isMP4(raw []byte) bool {
|
||||
return len(raw) >= 8 && string(raw[4:8]) == "ftyp"
|
||||
}
|
||||
|
||||
func isWebM(raw []byte) bool {
|
||||
return len(raw) >= 4 && raw[0] == 0x1a && raw[1] == 0x45 && raw[2] == 0xdf && raw[3] == 0xa3
|
||||
}
|
||||
|
||||
type preparedPostVideo struct {
|
||||
body io.ReadCloser
|
||||
size int64
|
||||
extension string
|
||||
contentType string
|
||||
}
|
||||
|
||||
func preparePostVideo(header *multipart.FileHeader) (preparedPostVideo, error) {
|
||||
if header == nil {
|
||||
return preparedPostVideo{}, invalidPostImage("Select a valid video.", nil)
|
||||
}
|
||||
if header.Size == 0 {
|
||||
return preparedPostVideo{}, invalidPostImage("Videos cannot be empty.", nil)
|
||||
}
|
||||
if header.Size > postVideoMaxFileBytes {
|
||||
return preparedPostVideo{}, &postImageRequestError{
|
||||
status: http.StatusRequestEntityTooLarge,
|
||||
message: "Each video must be 25 MB or smaller.",
|
||||
}
|
||||
}
|
||||
file, err := header.Open()
|
||||
if err != nil {
|
||||
return preparedPostVideo{}, invalidPostImage("Could not read video.", err)
|
||||
}
|
||||
peek := make([]byte, 512)
|
||||
n, err := io.ReadFull(file, peek)
|
||||
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) && !errors.Is(err, io.EOF) {
|
||||
file.Close()
|
||||
return preparedPostVideo{}, invalidPostImage("Could not read video.", err)
|
||||
}
|
||||
if n == 0 {
|
||||
file.Close()
|
||||
return preparedPostVideo{}, invalidPostImage("Videos cannot be empty.", nil)
|
||||
}
|
||||
switch mediaKindFromBytes(peek[:n]) {
|
||||
case store.MediaKindVideo:
|
||||
default:
|
||||
file.Close()
|
||||
return preparedPostVideo{}, invalidPostImage("Videos must be MP4 or WebM.", nil)
|
||||
}
|
||||
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
||||
file.Close()
|
||||
return preparedPostVideo{}, invalidPostImage("Could not read video.", err)
|
||||
}
|
||||
result := preparedPostVideo{body: file, size: header.Size}
|
||||
if isWebM(peek[:n]) {
|
||||
result.extension = ".webm"
|
||||
result.contentType = "video/webm"
|
||||
return result, nil
|
||||
}
|
||||
result.extension = ".mp4"
|
||||
result.contentType = "video/mp4"
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func jpegOrientation(raw []byte) int {
|
||||
metadata, err := exif.Decode(bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -57,47 +56,6 @@ func TestPreparePostImage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparePostVideo(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mp4 := tinyMP4()
|
||||
prepared, err := preparePostVideoHeader(t, "clip.mp4", mp4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer prepared.body.Close()
|
||||
got, err := io.ReadAll(prepared.body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if prepared.extension != ".mp4" || prepared.contentType != "video/mp4" ||
|
||||
prepared.size != int64(len(mp4)) || !bytes.Equal(got, mp4) {
|
||||
t.Fatalf("prepared MP4 = %+v len(body)=%d", prepared, len(got))
|
||||
}
|
||||
|
||||
webm := tinyWebM()
|
||||
prepared, err = preparePostVideoHeader(t, "clip.webm", webm)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer prepared.body.Close()
|
||||
if prepared.extension != ".webm" || prepared.contentType != "video/webm" ||
|
||||
prepared.size != int64(len(webm)) {
|
||||
t.Fatalf("prepared WebM = %+v", prepared)
|
||||
}
|
||||
if _, err := preparePostVideoHeader(t, "empty.mp4", nil); err == nil {
|
||||
t.Fatal("empty video unexpectedly succeeded")
|
||||
}
|
||||
if _, err := preparePostVideoHeader(t, "notes.txt", []byte("not a video")); err == nil {
|
||||
t.Fatal("text video upload unexpectedly succeeded")
|
||||
}
|
||||
_, err = preparePostVideoHeader(t, "too-large.mp4", make([]byte, postVideoMaxFileBytes+1))
|
||||
var requestErr *postImageRequestError
|
||||
if !errors.As(err, &requestErr) || requestErr.status != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("oversized video error = %v, want 413 request error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrientPostImage(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -245,87 +203,6 @@ func TestPostImageMultipartLifecycle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostVideoMultipartLifecycle(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
blobs := &recordingImageBlob{}
|
||||
srv, mem := newTestServer(t, Config{Blob: blobs})
|
||||
handler := srv.Handler()
|
||||
homeowner := seedUser(t, mem, uniq("video"), "hunter22", store.RoleUser)
|
||||
cookies := loginUser(t, handler, homeowner.Username, "hunter22")
|
||||
csrf := csrfForCookies(t, handler, cookies)
|
||||
|
||||
rec := multipartPost(t, handler, "/submit", map[string][]string{
|
||||
"_csrf": {csrf},
|
||||
"title": {"Valve clip"},
|
||||
"body": {"A photo and a video."},
|
||||
"city": {"Oakland"},
|
||||
"image_description": {"Still", "Walkthrough"},
|
||||
}, []multipartTestFile{
|
||||
{name: "still.png", body: solidPNG(t, 40, 20)},
|
||||
{name: "walk.mp4", body: tinyMP4()},
|
||||
}, cookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("root video upload status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
roots, err := mem.ListRootPosts(context.Background(), pacific.Today(), homeowner.ID)
|
||||
if err != nil || len(roots) != 1 {
|
||||
t.Fatalf("roots = %+v, %v", roots, err)
|
||||
}
|
||||
root, err := mem.GetPost(context.Background(), roots[0].ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(root.Images) != 2 ||
|
||||
root.Images[0].Kind != store.MediaKindImage ||
|
||||
root.Images[1].Kind != store.MediaKindVideo ||
|
||||
root.Images[1].Description != "Walkthrough" ||
|
||||
!strings.HasPrefix(root.Images[1].ObjectKey, "post-videos/") {
|
||||
t.Fatalf("root media = %+v", root.Images)
|
||||
}
|
||||
clip := tinyMP4()
|
||||
var streamed recordedImageUpload
|
||||
for _, upload := range blobs.recordedUploads() {
|
||||
if strings.HasPrefix(upload.key, "post-videos/") {
|
||||
streamed = upload
|
||||
break
|
||||
}
|
||||
}
|
||||
if streamed.size != int64(len(clip)) || !bytes.Equal(streamed.body, clip) {
|
||||
t.Fatalf("streamed video upload = %+v", streamed)
|
||||
}
|
||||
|
||||
rec = multipartPost(t, handler, "/posts/"+root.ID+"/edit", map[string][]string{
|
||||
"_csrf": {csrf},
|
||||
"body": {"Keep the clip."},
|
||||
"existing_image_id": {root.Images[1].ID},
|
||||
"existing_image_description": {"Kept clip"},
|
||||
}, nil, cookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("retain video status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
edited, err := mem.GetPost(context.Background(), root.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(edited.Images) != 1 || edited.Images[0].Kind != store.MediaKindVideo ||
|
||||
edited.Images[0].Description != "Kept clip" {
|
||||
t.Fatalf("retained video = %+v", edited.Images)
|
||||
}
|
||||
|
||||
rec = multipartPost(t, handler, "/posts", map[string][]string{
|
||||
"_csrf": {csrf},
|
||||
"parent_id": {root.ID},
|
||||
"body": {"Two clips."},
|
||||
}, []multipartTestFile{
|
||||
{name: "a.mp4", body: tinyMP4()},
|
||||
{name: "b.mp4", body: tinyMP4()},
|
||||
}, cookies)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("two-video status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostImageUploadCompensation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -441,12 +318,6 @@ func multipartPost(
|
||||
return rec
|
||||
}
|
||||
|
||||
func preparePostVideoHeader(t *testing.T, name string, body []byte) (preparedPostVideo, error) {
|
||||
t.Helper()
|
||||
header := multipartFileHeader(t, name, body)
|
||||
return preparePostVideo(header)
|
||||
}
|
||||
|
||||
func preparePostImageHeader(t *testing.T, name string, body []byte) (preparedPostImage, error) {
|
||||
t.Helper()
|
||||
var requestBody bytes.Buffer
|
||||
@@ -470,41 +341,6 @@ func preparePostImageHeader(t *testing.T, name string, body []byte) (preparedPos
|
||||
return preparePostImage(req.MultipartForm.File["images"][0])
|
||||
}
|
||||
|
||||
func multipartFileHeader(t *testing.T, name string, body []byte) *multipart.FileHeader {
|
||||
t.Helper()
|
||||
var requestBody bytes.Buffer
|
||||
writer := multipart.NewWriter(&requestBody)
|
||||
part, err := writer.CreateFormFile("images", name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := part.Write(body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/posts", &requestBody)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
if err := req.ParseMultipartForm(postImageMultipartMemory); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = req.MultipartForm.RemoveAll() })
|
||||
return req.MultipartForm.File["images"][0]
|
||||
}
|
||||
|
||||
func tinyMP4() []byte {
|
||||
body := make([]byte, 16)
|
||||
body[3] = 16
|
||||
copy(body[4:], "ftypisom")
|
||||
copy(body[12:], "isom")
|
||||
return body
|
||||
}
|
||||
|
||||
func tinyWebM() []byte {
|
||||
return []byte{0x1a, 0x45, 0xdf, 0xa3, 0x01, 0x00, 0x00, 0x00}
|
||||
}
|
||||
|
||||
func solidPNG(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
@@ -538,7 +374,6 @@ func solidJPEG(t *testing.T, width, height int) []byte {
|
||||
type recordedImageUpload struct {
|
||||
key string
|
||||
contentType string
|
||||
size int64
|
||||
body []byte
|
||||
}
|
||||
|
||||
@@ -566,7 +401,6 @@ func (b *recordingImageBlob) Upload(_ context.Context, object blob.FileUpload) (
|
||||
b.uploads = append(b.uploads, recordedImageUpload{
|
||||
key: object.Key,
|
||||
contentType: object.ContentType,
|
||||
size: object.Size,
|
||||
body: body,
|
||||
})
|
||||
return "https://cdn.example/" + object.Key, nil
|
||||
@@ -580,13 +414,9 @@ func (b *recordingImageBlob) Delete(_ context.Context, key string) error {
|
||||
}
|
||||
|
||||
func (b *recordingImageBlob) uploadCount() int {
|
||||
return len(b.recordedUploads())
|
||||
}
|
||||
|
||||
func (b *recordingImageBlob) recordedUploads() []recordedImageUpload {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return append([]recordedImageUpload(nil), b.uploads...)
|
||||
return len(b.uploads)
|
||||
}
|
||||
|
||||
func (b *recordingImageBlob) deletedKeys() []string {
|
||||
|
||||
+3
-13
@@ -48,23 +48,13 @@ CREATE TABLE IF NOT EXISTS post_images (
|
||||
object_key TEXT NOT NULL UNIQUE,
|
||||
public_url TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '' CHECK (char_length(description) <= 500),
|
||||
kind TEXT NOT NULL DEFAULT 'image',
|
||||
position SMALLINT NOT NULL,
|
||||
width INTEGER NOT NULL,
|
||||
height INTEGER NOT NULL,
|
||||
position SMALLINT NOT NULL CHECK (position BETWEEN 0 AND 3),
|
||||
width INTEGER NOT NULL CHECK (width > 0),
|
||||
height INTEGER NOT NULL CHECK (height > 0),
|
||||
created_at TEXT NOT NULL,
|
||||
CONSTRAINT post_images_kind_check CHECK (kind IN ('image', 'video')),
|
||||
CONSTRAINT post_images_width_check CHECK (width >= 0),
|
||||
CONSTRAINT post_images_height_check CHECK (height >= 0),
|
||||
CONSTRAINT post_images_image_dims_check CHECK (kind <> 'image' OR (width > 0 AND height > 0)),
|
||||
CONSTRAINT post_images_position_check CHECK (position BETWEEN 0 AND 4),
|
||||
UNIQUE (post_id, position)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS post_images_one_video_uidx
|
||||
ON post_images (post_id)
|
||||
WHERE kind = 'video';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS post_votes (
|
||||
user_id TEXT NOT NULL REFERENCES users(id),
|
||||
post_id TEXT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
||||
|
||||
Reference in New Issue
Block a user