Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: server id support #6834

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
14 changes: 11 additions & 3 deletions internal/db/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/model/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions internal/op/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions server/handles/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -89,15 +90,17 @@ 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
// if token is empty, return guest user
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 != "" {
Expand Down
Loading