Fix link for locker

This commit is contained in:
Neeraj Gupta
2025-08-25 10:50:05 +05:30
parent f2a26ba391
commit bc6506cb10
4 changed files with 35 additions and 18 deletions

View File

@@ -181,7 +181,7 @@ func (c *CollectionController) UpdateShareeMagicMetadata(ctx *gin.Context, req e
}
// ShareURL generates a public auth-token for the given collectionID
func (c *CollectionController) ShareURL(ctx context.Context, userID int64, req ente.CreatePublicAccessTokenRequest) (
func (c *CollectionController) ShareURL(ctx *gin.Context, userID int64, req ente.CreatePublicAccessTokenRequest) (
ente.PublicURL, error) {
collection, err := c.CollectionRepo.Get(req.CollectionID)
if err != nil {
@@ -206,7 +206,7 @@ func (c *CollectionController) ShareURL(ctx context.Context, userID int64, req e
// UpdateShareURL updates the shared url configuration
func (c *CollectionController) UpdateShareURL(
ctx context.Context,
ctx *gin.Context,
userID int64,
req ente.UpdatePublicAccessTokenRequest,
) (*ente.PublicURL, error) {

View File

@@ -60,13 +60,14 @@ type CollectionLinkController struct {
JwtSecret []byte
}
func (c *CollectionLinkController) CreateLink(ctx context.Context, req ente.CreatePublicAccessTokenRequest) (ente.PublicURL, error) {
func (c *CollectionLinkController) CreateLink(ctx *gin.Context, req ente.CreatePublicAccessTokenRequest) (ente.PublicURL, error) {
accessToken := shortuuid.New()[0:AccessTokenLength]
app := auth.GetApp(ctx)
err := c.CollectionLinkRepo.
Insert(ctx, req.CollectionID, accessToken, req.ValidTill, req.DeviceLimit, req.EnableCollect, req.EnableJoin)
if err != nil {
if errors.Is(err, ente.ErrActiveLinkAlreadyExists) {
collectionToPubUrlMap, err2 := c.CollectionLinkRepo.GetCollectionToActivePublicURLMap(ctx, []int64{req.CollectionID})
collectionToPubUrlMap, err2 := c.CollectionLinkRepo.GetCollectionToActivePublicURLMap(ctx, []int64{req.CollectionID}, app)
if err2 != nil {
return ente.PublicURL{}, stacktrace.Propagate(err2, "")
}
@@ -81,8 +82,9 @@ func (c *CollectionLinkController) CreateLink(ctx context.Context, req ente.Crea
return ente.PublicURL{}, stacktrace.Propagate(err, "")
}
}
response := ente.PublicURL{
URL: c.CollectionLinkRepo.GetAlbumUrl(accessToken),
URL: c.CollectionLinkRepo.GetAlbumUrl(app, accessToken),
ValidTill: req.ValidTill,
DeviceLimit: req.DeviceLimit,
EnableDownload: true,
@@ -124,7 +126,7 @@ func (c *CollectionLinkController) Disable(ctx context.Context, cID int64) error
return stacktrace.Propagate(err, "")
}
func (c *CollectionLinkController) UpdateSharedUrl(ctx context.Context, req ente.UpdatePublicAccessTokenRequest) (ente.PublicURL, error) {
func (c *CollectionLinkController) UpdateSharedUrl(ctx *gin.Context, req ente.UpdatePublicAccessTokenRequest) (ente.PublicURL, error) {
publicCollectionToken, err := c.CollectionLinkRepo.GetActiveCollectionLinkRow(ctx, req.CollectionID)
if err != nil {
return ente.PublicURL{}, err
@@ -159,8 +161,9 @@ func (c *CollectionLinkController) UpdateSharedUrl(ctx context.Context, req ente
if err != nil {
return ente.PublicURL{}, stacktrace.Propagate(err, "")
}
app := auth.GetApp(ctx)
return ente.PublicURL{
URL: c.CollectionLinkRepo.GetAlbumUrl(publicCollectionToken.Token),
URL: c.CollectionLinkRepo.GetAlbumUrl(app, publicCollectionToken.Token),
DeviceLimit: publicCollectionToken.DeviceLimit,
ValidTill: publicCollectionToken.ValidTill,
EnableDownload: publicCollectionToken.EnableDownload,

View File

@@ -61,12 +61,12 @@ func (repo *CollectionRepository) Create(c ente.Collection) (ente.Collection, er
// Get returns a collection identified by the collectionID
func (repo *CollectionRepository) Get(collectionID int64) (ente.Collection, error) {
row := repo.DB.QueryRow(`SELECT collection_id, owner_id, encrypted_key, key_decryption_nonce, name, encrypted_name, name_decryption_nonce, type, attributes, updation_time, is_deleted, magic_metadata, pub_magic_metadata
row := repo.DB.QueryRow(`SELECT collection_id, app, owner_id, encrypted_key, key_decryption_nonce, name, encrypted_name, name_decryption_nonce, type, attributes, updation_time, is_deleted, magic_metadata, pub_magic_metadata
FROM collections
WHERE collection_id = $1`, collectionID)
var c ente.Collection
var name, encryptedName, nameDecryptionNonce sql.NullString
if err := row.Scan(&c.ID, &c.Owner.ID, &c.EncryptedKey, &c.KeyDecryptionNonce, &name, &encryptedName, &nameDecryptionNonce, &c.Type, &c.Attributes, &c.UpdationTime, &c.IsDeleted, &c.MagicMetadata, &c.PublicMagicMetadata); err != nil {
if err := row.Scan(&c.ID, &c.App, &c.Owner.ID, &c.EncryptedKey, &c.KeyDecryptionNonce, &name, &encryptedName, &nameDecryptionNonce, &c.Type, &c.Attributes, &c.UpdationTime, &c.IsDeleted, &c.MagicMetadata, &c.PublicMagicMetadata); err != nil {
return c, stacktrace.Propagate(err, "")
}
if name.Valid && len(name.String) > 0 {
@@ -75,7 +75,11 @@ func (repo *CollectionRepository) Get(collectionID int64) (ente.Collection, erro
c.EncryptedName = encryptedName.String
c.NameDecryptionNonce = nameDecryptionNonce.String
}
urlMap, err := repo.CollectionLinkRepo.GetCollectionToActivePublicURLMap(context.Background(), []int64{collectionID})
app := ente.Photos
if len(c.App) > 0 && ente.App(c.App).IsValid() {
app = ente.App(c.App)
}
urlMap, err := repo.CollectionLinkRepo.GetCollectionToActivePublicURLMap(context.Background(), []int64{collectionID}, app)
if err != nil {
return ente.Collection{}, stacktrace.Propagate(err, "failed to get publicURL info")
}
@@ -175,7 +179,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: repo.CollectionLinkRepo.GetAlbumUrl(pctToken.String),
URL: repo.CollectionLinkRepo.GetAlbumUrl(app, pctToken.String),
DeviceLimit: int(pctDeviceLimit.Int32),
ValidTill: pctValidTill.Int64,
EnableDownload: pctEnableDownload.Bool,

View File

@@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/spf13/viper"
"github.com/ente-io/museum/ente"
"github.com/ente-io/stacktrace"
@@ -14,8 +15,9 @@ import (
// CollectionLinkRepo defines the methods for inserting, updating and
// retrieving entities related to public collections
type CollectionLinkRepo struct {
DB *sql.DB
albumHost string
DB *sql.DB
albumHost string
lockerHost string
}
// NewCollectionLinkRepository ..
@@ -23,13 +25,21 @@ func NewCollectionLinkRepository(db *sql.DB, albumHost string) *CollectionLinkRe
if albumHost == "" {
albumHost = "https://albums.ente.io"
}
lockerHost := viper.GetString("apps.public-locker")
if lockerHost == "" {
lockerHost = "https://locker.ente.io"
}
return &CollectionLinkRepo{
DB: db,
albumHost: albumHost,
DB: db,
albumHost: albumHost,
lockerHost: lockerHost,
}
}
func (pcr *CollectionLinkRepo) GetAlbumUrl(token string) string {
func (pcr *CollectionLinkRepo) GetAlbumUrl(app ente.App, token string) string {
if app == ente.Locker {
return fmt.Sprintf("%s/?t=%s", pcr.lockerHost, token)
}
return fmt.Sprintf("%s/?t=%s", pcr.albumHost, token)
}
@@ -57,7 +67,7 @@ func (pcr *CollectionLinkRepo) DisableSharing(ctx context.Context, cID int64) er
// GetCollectionToActivePublicURLMap will return map of collectionID to PublicURLs which are not disabled yet.
// Note: The url could be expired or deviceLimit is already reached
func (pcr *CollectionLinkRepo) GetCollectionToActivePublicURLMap(ctx context.Context, collectionIDs []int64) (map[int64][]ente.PublicURL, error) {
func (pcr *CollectionLinkRepo) GetCollectionToActivePublicURLMap(ctx context.Context, collectionIDs []int64, app ente.App) (map[int64][]ente.PublicURL, error) {
rows, err := pcr.DB.QueryContext(ctx, `SELECT collection_id, access_token, valid_till, device_limit, enable_download, enable_collect, enable_join, pw_nonce, mem_limit, ops_limit FROM
public_collection_tokens WHERE collection_id = ANY($1) and is_disabled = FALSE`,
pq.Array(collectionIDs))
@@ -78,7 +88,7 @@ func (pcr *CollectionLinkRepo) GetCollectionToActivePublicURLMap(ctx context.Con
if err = rows.Scan(&collectionID, &accessToken, &publicUrl.ValidTill, &publicUrl.DeviceLimit, &publicUrl.EnableDownload, &publicUrl.EnableCollect, &publicUrl.EnableJoin, &nonce, &memLimit, &opsLimit); err != nil {
return nil, stacktrace.Propagate(err, "")
}
publicUrl.URL = pcr.GetAlbumUrl(accessToken)
publicUrl.URL = pcr.GetAlbumUrl(app, accessToken)
if nonce != nil {
publicUrl.Nonce = nonce
publicUrl.MemLimit = memLimit