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.
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
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
|
|
}
|