From 8ed2c7cff9744bc7da7b7dc8cd9ee65c839741e2 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta Date: Mon, 15 Apr 2024 09:56:21 +0530 Subject: [PATCH] [server] Pick base publicHost url from config (#1443) ## Description ## Tests Ran locally with the config in local.yaml and verified that it's modified as well when I had put localhost:3002 in the museum.yaml config --- server/cmd/museum/main.go | 2 +- server/configurations/local.yaml | 5 +++++ server/pkg/controller/public_collection.go | 4 ++-- server/pkg/repo/collection.go | 2 +- server/pkg/repo/public_collection.go | 20 ++++++++++++++++++-- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go index 4cbc006122..c451b8b9c0 100644 --- a/server/cmd/museum/main.go +++ b/server/cmd/museum/main.go @@ -163,7 +163,7 @@ func main() { ObjectCopiesRepo: objectCopiesRepo, UsageRepo: usageRepo} familyRepo := &repo.FamilyRepository{DB: db} trashRepo := &repo.TrashRepository{DB: db, ObjectRepo: objectRepo, FileRepo: fileRepo, QueueRepo: queueRepo} - publicCollectionRepo := &repo.PublicCollectionRepository{DB: db} + publicCollectionRepo := repo.NewPublicCollectionRepository(db, viper.GetString("apps.public-albums")) collectionRepo := &repo.CollectionRepository{DB: db, FileRepo: fileRepo, PublicCollectionRepo: publicCollectionRepo, TrashRepo: trashRepo, SecretEncryptionKey: secretEncryptionKeyBytes, QueueRepo: queueRepo, LatencyLogger: latencyLogger} pushRepo := &repo.PushTokenRepository{DB: db} diff --git a/server/configurations/local.yaml b/server/configurations/local.yaml index bbad3f2784..0e456a53af 100644 --- a/server/configurations/local.yaml +++ b/server/configurations/local.yaml @@ -71,6 +71,11 @@ http: # By default, this is false, and museum will bind to 8080 without TLS. # use-tls: true +# Specify the base endpoints for various apps +apps: + public-albums: "https://albums.ente.io" + + # Database connection parameters db: host: localhost diff --git a/server/pkg/controller/public_collection.go b/server/pkg/controller/public_collection.go index 76a6049376..694db8bb10 100644 --- a/server/pkg/controller/public_collection.go +++ b/server/pkg/controller/public_collection.go @@ -80,7 +80,7 @@ func (c *PublicCollectionController) CreateAccessToken(ctx context.Context, req } } response := ente.PublicURL{ - URL: fmt.Sprintf(repo.BaseShareURL, accessToken), + URL: c.PublicCollectionRepo.GetAlbumUrl(accessToken), ValidTill: req.ValidTill, DeviceLimit: req.DeviceLimit, EnableDownload: true, @@ -151,7 +151,7 @@ func (c *PublicCollectionController) UpdateSharedUrl(ctx context.Context, req en return ente.PublicURL{}, stacktrace.Propagate(err, "") } return ente.PublicURL{ - URL: fmt.Sprintf(repo.BaseShareURL, publicCollectionToken.Token), + URL: c.PublicCollectionRepo.GetAlbumUrl(publicCollectionToken.Token), DeviceLimit: publicCollectionToken.DeviceLimit, ValidTill: publicCollectionToken.ValidTill, EnableDownload: publicCollectionToken.EnableDownload, diff --git a/server/pkg/repo/collection.go b/server/pkg/repo/collection.go index 38700dec4d..16ae853244 100644 --- a/server/pkg/repo/collection.go +++ b/server/pkg/repo/collection.go @@ -216,7 +216,7 @@ pct.access_token, pct.valid_till, pct.device_limit, pct.created_at, pct.updated_ if _, ok := addPublicUrlMap[pctToken.String]; !ok { addPublicUrlMap[pctToken.String] = true url := ente.PublicURL{ - URL: fmt.Sprintf(BaseShareURL, pctToken.String), + URL: repo.PublicCollectionRepo.GetAlbumUrl(pctToken.String), DeviceLimit: int(pctDeviceLimit.Int32), ValidTill: pctValidTill.Int64, EnableDownload: pctEnableDownload.Bool, diff --git a/server/pkg/repo/public_collection.go b/server/pkg/repo/public_collection.go index 0b1d2514f2..d4aa81bf29 100644 --- a/server/pkg/repo/public_collection.go +++ b/server/pkg/repo/public_collection.go @@ -16,7 +16,23 @@ const BaseShareURL = "https://albums.ente.io?t=%s" // PublicCollectionRepository defines the methods for inserting, updating and // retrieving entities related to public collections type PublicCollectionRepository struct { - DB *sql.DB + DB *sql.DB + albumHost string +} + +// NewPublicCollectionRepository .. +func NewPublicCollectionRepository(db *sql.DB, albumHost string) *PublicCollectionRepository { + if albumHost == "" { + panic("albumHost can not be empty") + } + return &PublicCollectionRepository{ + DB: db, + albumHost: albumHost, + } +} + +func (pcr *PublicCollectionRepository) GetAlbumUrl(token string) string { + return fmt.Sprintf("%s?t=%s", pcr.albumHost, token) } func (pcr *PublicCollectionRepository) Insert(ctx context.Context, @@ -59,7 +75,7 @@ func (pcr *PublicCollectionRepository) GetCollectionToActivePublicURLMap(ctx con if err = rows.Scan(&collectionID, &accessToken, &publicUrl.ValidTill, &publicUrl.DeviceLimit, &publicUrl.EnableDownload, &publicUrl.EnableCollect, &nonce, &memLimit, &opsLimit); err != nil { return nil, stacktrace.Propagate(err, "") } - publicUrl.URL = fmt.Sprintf(BaseShareURL, accessToken) + publicUrl.URL = pcr.GetAlbumUrl(accessToken) if nonce != nil { publicUrl.Nonce = nonce publicUrl.MemLimit = memLimit