diff --git a/internal/conf/config.go b/internal/conf/config.go index 742aab1f265..83124f05996 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -71,6 +71,7 @@ type S3 struct { } type Config struct { + ServerId string `json:"server_id" env:"SERVER_ID"` Force bool `json:"force" env:"FORCE"` SiteURL string `json:"site_url" env:"SITE_URL"` Cdn string `json:"cdn" env:"CDN"` diff --git a/internal/db/storage.go b/internal/db/storage.go index d4e0730f064..a1bc918045b 100644 --- a/internal/db/storage.go +++ b/internal/db/storage.go @@ -4,6 +4,7 @@ import ( "fmt" "sort" + "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/model" "github.com/pkg/errors" ) @@ -66,8 +67,15 @@ func GetEnabledStorages() ([]model.Storage, error) { if err := db.Where(fmt.Sprintf("%s = ?", columnName("disabled")), false).Find(&storages).Error; err != nil { return nil, errors.WithStack(err) } - sort.Slice(storages, func(i, j int) bool { - return storages[i].Order < storages[j].Order + serverStorages := make([]model.Storage, 0) + for _, v := range storages { + if v.Driver == "Local" && len(conf.Conf.ServerId) > 0 && conf.Conf.ServerId != v.ServerId { + continue + } + serverStorages = append(serverStorages, v) + } + sort.Slice(serverStorages, func(i, j int) bool { + return serverStorages[i].Order < serverStorages[j].Order }) - return storages, nil + return serverStorages, nil } diff --git a/internal/model/storage.go b/internal/model/storage.go index 14bcf45f6e2..f3d49c01e15 100644 --- a/internal/model/storage.go +++ b/internal/model/storage.go @@ -4,6 +4,7 @@ import "time" type Storage struct { ID uint `json:"id" gorm:"primaryKey"` // unique key + ServerId string `json:"server_id"` // only effective if driver is "Local" MountPath string `json:"mount_path" gorm:"unique" binding:"required"` // must be standardized Order int `json:"order"` // use to sort Driver string `json:"driver"` // driver used diff --git a/internal/op/storage.go b/internal/op/storage.go index 2f0831c452e..dd9b72a43aa 100644 --- a/internal/op/storage.go +++ b/internal/op/storage.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/model" @@ -51,6 +52,10 @@ func CreateStorage(ctx context.Context, storage model.Storage) (uint, error) { return 0, errors.WithMessage(err, "failed get driver new") } storageDriver := driverNew() + //if server_id is not empty,set the local storage server id + if driverName == "Local" && len(conf.Conf.ServerId) > 0 { + storage.ServerId = conf.Conf.ServerId + } // insert storage to database err = db.CreateStorage(&storage) if err != nil { @@ -165,6 +170,9 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error { } storage.Modified = time.Now() storage.MountPath = utils.FixAndCleanPath(storage.MountPath) + if storage.Driver == "Local" && len(conf.Conf.ServerId) > 0 { + storage.ServerId = conf.Conf.ServerId + } err = db.UpdateStorage(&storage) if err != nil { return errors.WithMessage(err, "failed update storage in database") diff --git a/server/handles/auth.go b/server/handles/auth.go index 209bdd3a2b8..c0c503106e8 100644 --- a/server/handles/auth.go +++ b/server/handles/auth.go @@ -7,6 +7,7 @@ import ( "time" "github.com/Xhofe/go-cache" + "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/op" "github.com/alist-org/alist/v3/server/common" @@ -89,7 +90,8 @@ func loginHash(c *gin.Context, req *LoginReq) { type UserResp struct { model.User - Otp bool `json:"otp"` + Otp bool `json:"otp"` + ServerId string `json:"server_id"` } // CurrentUser get current user by token @@ -97,7 +99,8 @@ type UserResp struct { func CurrentUser(c *gin.Context) { user := c.MustGet("user").(*model.User) userResp := UserResp{ - User: *user, + User: *user, + ServerId: conf.Conf.ServerId, } userResp.Password = "" if userResp.OtpSecret != "" {