Resolve public_base_url at SignURL time so admin settings apply without restart. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -24,20 +24,19 @@ type Service struct {
|
||||
db *gorm.DB
|
||||
cache *cache.Store
|
||||
images *imagestore.Store
|
||||
hub *Hub
|
||||
publicURL string
|
||||
waitSec int64
|
||||
hub *Hub
|
||||
waitSec int64
|
||||
mu sync.Mutex
|
||||
running map[string]context.CancelFunc
|
||||
}
|
||||
|
||||
func NewService(db *gorm.DB, c *cache.Store, images *imagestore.Store, publicURL string, waitSec int64) *Service {
|
||||
func NewService(db *gorm.DB, c *cache.Store, images *imagestore.Store, waitSec int64) *Service {
|
||||
if waitSec <= 0 {
|
||||
waitSec = 60
|
||||
}
|
||||
return &Service{
|
||||
db: db, cache: c, images: images, hub: NewHub(),
|
||||
publicURL: strings.TrimRight(publicURL, "/"), waitSec: waitSec,
|
||||
waitSec: waitSec,
|
||||
running: make(map[string]context.CancelFunc),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,15 +17,22 @@ import (
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
db *gorm.DB
|
||||
baseDir string
|
||||
signKey string
|
||||
publicURL string
|
||||
ttl time.Duration
|
||||
db *gorm.DB
|
||||
baseDir string
|
||||
signKey string
|
||||
publicURL func() string
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func New(db *gorm.DB, baseDir, publicURL, signKey string, ttl time.Duration) *Store {
|
||||
return &Store{db: db, baseDir: baseDir, publicURL: strings.TrimRight(publicURL, "/"), signKey: signKey, ttl: ttl}
|
||||
func New(db *gorm.DB, baseDir string, publicURL func() string, signKey string, ttl time.Duration) *Store {
|
||||
return &Store{db: db, baseDir: baseDir, publicURL: publicURL, signKey: signKey, ttl: ttl}
|
||||
}
|
||||
|
||||
func (s *Store) baseURL() string {
|
||||
if s.publicURL == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimRight(s.publicURL(), "/")
|
||||
}
|
||||
|
||||
func (s *Store) EnsureDir() error {
|
||||
@@ -76,7 +83,7 @@ func (s *Store) ListByPrediction(predictionID string) ([]model.StoredImage, erro
|
||||
func (s *Store) SignURL(imageID string) string {
|
||||
exp := time.Now().Add(s.ttl).Unix()
|
||||
sig := s.sign(imageID, exp)
|
||||
return fmt.Sprintf("%s/files/%s?exp=%d&sig=%s", s.publicURL, imageID, exp, sig)
|
||||
return fmt.Sprintf("%s/files/%s?exp=%d&sig=%s", s.baseURL(), imageID, exp, sig)
|
||||
}
|
||||
|
||||
func (s *Store) Verify(imageID string, exp int64, sig string) bool {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package imagestore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSignURLUsesDynamicPublicBase(t *testing.T) {
|
||||
current := "http://localhost:8293"
|
||||
store := New(nil, t.TempDir(), func() string { return current }, "test-key", time.Hour)
|
||||
|
||||
if got := store.SignURL("img-1"); !hasPrefix(got, "http://localhost:8293/files/img-1") {
|
||||
t.Fatalf("unexpected url: %s", got)
|
||||
}
|
||||
|
||||
current = "https://gateway.example.com"
|
||||
if got := store.SignURL("img-2"); !hasPrefix(got, "https://gateway.example.com/files/img-2") {
|
||||
t.Fatalf("expected updated base url, got: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func hasPrefix(s, prefix string) bool {
|
||||
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
||||
}
|
||||
Reference in New Issue
Block a user