-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
184 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package model | ||
|
||
type Meta struct { | ||
ID uint `json:"id" gorm:"primaryKey"` | ||
Path string `json:"path" gorm:"unique" binding:"required"` | ||
Password string `json:"password"` | ||
Hide string `json:"hide"` | ||
Upload bool `json:"upload"` | ||
OnlyShows string `json:"only_shows"` | ||
Readme string `json:"readme"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package store | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrMetaNotFound = errors.New("meta not found") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package store | ||
|
||
import ( | ||
"github.com/Xhofe/go-cache" | ||
"github.com/alist-org/alist/v3/internal/model" | ||
"github.com/alist-org/alist/v3/pkg/singleflight" | ||
"github.com/alist-org/alist/v3/pkg/utils" | ||
"github.com/pkg/errors" | ||
"gorm.io/gorm" | ||
stdpath "path" | ||
) | ||
|
||
var metaCache = cache.NewMemCache(cache.WithShards[*model.Meta](2)) | ||
|
||
// metaG maybe not needed | ||
var metaG singleflight.Group[*model.Meta] | ||
|
||
func GetNearestMeta(path string) (*model.Meta, error) { | ||
path = utils.StandardizationPath(path) | ||
meta, err := GetMetaByPath(path) | ||
if err == nil { | ||
return meta, nil | ||
} | ||
if errors.Cause(err) != gorm.ErrRecordNotFound { | ||
return nil, err | ||
} | ||
if path == "/" { | ||
return nil, errors.WithStack(ErrMetaNotFound) | ||
} | ||
return GetNearestMeta(stdpath.Dir(path)) | ||
} | ||
|
||
func GetMetaByPath(path string) (*model.Meta, error) { | ||
meta, ok := metaCache.Get(path) | ||
if ok { | ||
return meta, nil | ||
} | ||
meta, err, _ := metaG.Do(path, func() (*model.Meta, error) { | ||
meta := model.Meta{Path: path} | ||
if err := db.Where(meta).First(&meta).Error; err != nil { | ||
return nil, errors.Wrapf(err, "failed select meta") | ||
} | ||
metaCache.Set(path, &meta) | ||
return &meta, nil | ||
}) | ||
return meta, err | ||
} | ||
|
||
func GetMetaById(id uint) (*model.Meta, error) { | ||
var u model.Meta | ||
if err := db.First(&u, id).Error; err != nil { | ||
return nil, errors.Wrapf(err, "failed get old meta") | ||
} | ||
return &u, nil | ||
} | ||
|
||
func CreateMeta(u *model.Meta) error { | ||
return errors.WithStack(db.Create(u).Error) | ||
} | ||
|
||
func UpdateMeta(u *model.Meta) error { | ||
old, err := GetMetaById(u.ID) | ||
if err != nil { | ||
return err | ||
} | ||
metaCache.Del(old.Path) | ||
return errors.WithStack(db.Save(u).Error) | ||
} | ||
|
||
func GetMetas(pageIndex, pageSize int) ([]model.Meta, int64, error) { | ||
metaDB := db.Model(&model.Meta{}) | ||
var count int64 | ||
if err := metaDB.Count(&count).Error; err != nil { | ||
return nil, 0, errors.Wrapf(err, "failed get metas count") | ||
} | ||
var metas []model.Meta | ||
if err := metaDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&metas).Error; err != nil { | ||
return nil, 0, errors.Wrapf(err, "failed get find metas") | ||
} | ||
return metas, count, nil | ||
} | ||
|
||
func DeleteMetaById(id uint) error { | ||
old, err := GetMetaById(id) | ||
if err != nil { | ||
return err | ||
} | ||
metaCache.Del(old.Path) | ||
return errors.WithStack(db.Delete(&model.Meta{}, id).Error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package store | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/internal/model" | ||
"github.com/pkg/errors" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"testing" | ||
) | ||
|
||
func init() { | ||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{}) | ||
if err != nil { | ||
panic("failed to connect database") | ||
} | ||
Init(db) | ||
} | ||
|
||
func TestCreateMeta(t *testing.T) { | ||
metas := []model.Meta{ | ||
{Path: "/"}, | ||
{Path: "/a"}, | ||
{Path: "/a/b"}, | ||
{Path: "/a/b/c"}, | ||
} | ||
for _, meta := range metas { | ||
err := CreateMeta(&meta) | ||
if err != nil { | ||
t.Errorf("failed to create meta: %+v", err) | ||
} | ||
} | ||
} | ||
|
||
func TestUpdateMeta(t *testing.T) { | ||
meta := model.Meta{ID: 1, Path: "/b"} | ||
err := UpdateMeta(&meta) | ||
if err != nil { | ||
t.Errorf("failed to update meta: %+v", err) | ||
} | ||
} | ||
|
||
func TestGetNearestMeta1(t *testing.T) { | ||
meta, err := GetNearestMeta("/b/c/d") | ||
if err != nil { | ||
t.Errorf("failed to get nearest meta: %+v", err) | ||
} | ||
if meta.Path != "/b" { | ||
t.Errorf("unexpected meta: %+v", meta) | ||
} | ||
} | ||
|
||
func TestGetNearestMeta2(t *testing.T) { | ||
meta, err := GetNearestMeta("/c/d/e") | ||
if errors.Cause(err) != ErrMetaNotFound { | ||
t.Errorf("unexpected error: %+v", err) | ||
t.Errorf("unexpected meta: %+v", meta) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters