feat: implement shared-album multi-user photo album
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.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package sharelinks
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"shared-album/internal/auth"
|
||||
"shared-album/internal/httpx"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
svc *Service
|
||||
}
|
||||
|
||||
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
|
||||
|
||||
type createRequest struct {
|
||||
ExpiresIn string `json:"expires_in"`
|
||||
}
|
||||
|
||||
func (h *Handler) Create(c *gin.Context) {
|
||||
var req createRequest
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
var ttl time.Duration
|
||||
if req.ExpiresIn != "" {
|
||||
d, err := time.ParseDuration(req.ExpiresIn)
|
||||
if err != nil {
|
||||
httpx.RespondError(c, httpx.BadRequest("invalid_expires_in", "invalid expires_in duration"))
|
||||
return
|
||||
}
|
||||
ttl = d
|
||||
}
|
||||
link, err := h.svc.Create(c.Request.Context(), c.Param("id"), auth.UserID(c), ttl)
|
||||
if err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"share_link": link})
|
||||
}
|
||||
|
||||
func (h *Handler) List(c *gin.Context) {
|
||||
list, err := h.svc.List(c.Request.Context(), c.Param("id"), auth.UserID(c))
|
||||
if err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"share_links": list})
|
||||
}
|
||||
|
||||
func (h *Handler) Revoke(c *gin.Context) {
|
||||
if err := h.svc.Revoke(c.Request.Context(), c.Param("id"), c.Param("link_id"), auth.UserID(c)); err != nil {
|
||||
httpx.RespondError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package sharelinks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"shared-album/internal/auth"
|
||||
"shared-album/internal/httpx"
|
||||
"shared-album/internal/models"
|
||||
"shared-album/internal/roles"
|
||||
)
|
||||
|
||||
type AlbumAccess interface {
|
||||
Access(ctx context.Context, albumID, userID string) (*models.Album, string, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
store *Store
|
||||
albums AlbumAccess
|
||||
}
|
||||
|
||||
func NewService(store *Store, albums AlbumAccess) *Service {
|
||||
return &Service{store: store, albums: albums}
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, albumID, actorID string, ttl time.Duration) (*models.ShareLink, error) {
|
||||
_, role, err := s.albums.Access(ctx, albumID, actorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !roles.CanManageShares(role) {
|
||||
return nil, httpx.Forbidden("forbidden", "only owner and admin can manage share links")
|
||||
}
|
||||
if ttl <= 0 {
|
||||
ttl = 7 * 24 * time.Hour
|
||||
}
|
||||
token, _, err := auth.NewRefreshToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l := &models.ShareLink{
|
||||
ID: auth.NewID(),
|
||||
AlbumID: albumID,
|
||||
Token: token,
|
||||
CreatedBy: actorID,
|
||||
ExpiresAt: time.Now().Add(ttl),
|
||||
}
|
||||
if err := s.store.Create(ctx, l); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, albumID, actorID string) ([]models.ShareLink, error) {
|
||||
_, role, err := s.albums.Access(ctx, albumID, actorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !roles.CanManageShares(role) {
|
||||
return nil, httpx.Forbidden("forbidden", "only owner and admin can manage share links")
|
||||
}
|
||||
return s.store.ListByAlbum(ctx, albumID)
|
||||
}
|
||||
|
||||
func (s *Service) Revoke(ctx context.Context, albumID, linkID, actorID string) error {
|
||||
_, role, err := s.albums.Access(ctx, albumID, actorID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !roles.CanManageShares(role) {
|
||||
return httpx.Forbidden("forbidden", "only owner and admin can manage share links")
|
||||
}
|
||||
if err := s.store.Revoke(ctx, linkID, albumID); err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return httpx.NotFound("share_link_not_found", "share link not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) Resolve(ctx context.Context, token string) (*models.ShareLink, error) {
|
||||
l, err := s.store.GetByToken(ctx, token)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return nil, httpx.NotFound("share_link_not_found", "share link not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !l.Active(time.Now()) {
|
||||
return nil, httpx.Forbidden("share_link_inactive", "share link is expired or revoked")
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user