Backend (Go/Gin/pgx): env config, pgx pool with retries, Garage S3 client (path-style, region garage), embedded golang-migrate migrations, JWT auth with refresh rotation, albums CRUD, members/roles/invites with no-escalation matrix, photo upload + async imaging/goexif WebP variants, cursor pagination, presigned URLs, likes/comments, SSE hub (album + personal), notifications, share links. Frontend (SvelteKit 2 + Svelte 5 + Tailwind v4): API wrapper with 401 refresh, auth store, all screens, Uploader/PhotoGrid/Lightbox/Comments, SSE client with reconnect + REST catch-up, three themes + system via data-theme. Deploy without Docker: Makefile builds the frontend into backend/static and embeds it (go:embed) into a single statically linked binary; systemd unit and nginx SSE example included. Use gen2brain/webp (-tags nodynamic) instead of cgo kolesa-team/go-webp to keep CGO_ENABLED=0 static builds; design.md/proposal.md updated accordingly.
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package sharelinks
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"shared-album/internal/models"
|
|
)
|
|
|
|
var ErrNotFound = errors.New("not found")
|
|
|
|
type Store struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewStore(pool *pgxpool.Pool) *Store { return &Store{pool: pool} }
|
|
|
|
func (s *Store) Create(ctx context.Context, l *models.ShareLink) error {
|
|
return s.pool.QueryRow(ctx,
|
|
`INSERT INTO share_links (id, album_id, token, created_by, expires_at)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING created_at`,
|
|
l.ID, l.AlbumID, l.Token, l.CreatedBy, l.ExpiresAt,
|
|
).Scan(&l.CreatedAt)
|
|
}
|
|
|
|
func (s *Store) ListByAlbum(ctx context.Context, albumID string) ([]models.ShareLink, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, album_id, token, created_by, created_at, expires_at, revoked_at
|
|
FROM share_links WHERE album_id = $1 ORDER BY created_at DESC`, albumID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []models.ShareLink{}
|
|
for rows.Next() {
|
|
var l models.ShareLink
|
|
if err := rows.Scan(&l.ID, &l.AlbumID, &l.Token, &l.CreatedBy, &l.CreatedAt, &l.ExpiresAt, &l.RevokedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, l)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) GetByToken(ctx context.Context, token string) (*models.ShareLink, error) {
|
|
var l models.ShareLink
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT id, album_id, token, created_by, created_at, expires_at, revoked_at
|
|
FROM share_links WHERE token = $1`, token).
|
|
Scan(&l.ID, &l.AlbumID, &l.Token, &l.CreatedBy, &l.CreatedAt, &l.ExpiresAt, &l.RevokedAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &l, nil
|
|
}
|
|
|
|
func (s *Store) Revoke(ctx context.Context, id, albumID string) error {
|
|
tag, err := s.pool.Exec(ctx,
|
|
`UPDATE share_links SET revoked_at = now() WHERE id = $1 AND album_id = $2 AND revoked_at IS NULL`,
|
|
id, albumID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|