feat(dep/fileindex): enable fileindex in file managing

This commit is contained in:
hexxa 2022-07-21 22:25:38 +08:00 committed by Hexxa
parent 28b7113d27
commit dff79ed87f
7 changed files with 111 additions and 44 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/ihexxa/quickshare/src/idgen"
"github.com/ihexxa/quickshare/src/iolimiter"
"github.com/ihexxa/quickshare/src/kvstore"
"github.com/ihexxa/quickshare/src/search/fileindex"
"github.com/ihexxa/quickshare/src/worker"
)
@ -38,6 +39,7 @@ type Deps struct {
workers worker.IWorkerPool
boltStore *boltstore.BoltStore
cron cron.ICron
fileIndex fileindex.IFileIndex
}
func NewDeps(cfg gocfg.ICfg) *Deps {
@ -139,3 +141,11 @@ func (deps *Deps) Cron() cron.ICron {
func (deps *Deps) SetCron(cronImp cron.ICron) {
deps.cron = cronImp
}
func (deps *Deps) FileIndex() fileindex.IFileIndex {
return deps.fileIndex
}
func (deps *Deps) SetIFileIndex(index fileindex.IFileIndex) {
deps.fileIndex = index
}

View file

@ -17,6 +17,7 @@ import (
"github.com/ihexxa/gocfg"
"github.com/ihexxa/multipart"
"github.com/ihexxa/fsearch"
"github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/depidx"
@ -210,6 +211,12 @@ func (h *FileHandlers) Create(c *gin.Context) {
return
}
err = h.deps.FileIndex().AddPath(fsFilePath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(q.Resp(200))
return
}
@ -290,6 +297,12 @@ func (h *FileHandlers) Delete(c *gin.Context) {
txErr = err
return
}
err = h.deps.FileIndex().DelPath(filePath)
if err != nil && !errors.Is(err, fsearch.ErrNotFound) {
txErr = err
return
}
})
if txErr != nil {
@ -364,6 +377,11 @@ func (h *FileHandlers) Mkdir(c *gin.Context) {
return
}
err = h.deps.FileIndex().AddPath(dirPath)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(q.Resp(200))
}
@ -421,6 +439,18 @@ func (h *FileHandlers) Move(c *gin.Context) {
return
}
newPathBase := filepath.Base(newPath)
err = h.deps.FileIndex().AddPath(newPathBase)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.FileIndex().MovePath(oldPath, newPathBase)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(q.Resp(200))
}
@ -527,6 +557,12 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
txErr, statusCode = err, 500
return
}
err = h.deps.FileIndex().AddPath(fsFilePath)
if err != nil {
txErr, statusCode = err, 500
return
}
}
if txErr != nil {

View file

@ -12,16 +12,26 @@ import (
"github.com/ihexxa/quickshare/src/fs"
)
type IFileIndex interface {
Search(keyword string) ([]string, error)
AddPath(pathname string) error
DelPath(pathname string) error
RenamePath(pathname, newName string) error
MovePath(pathname, dstParentPath string) error
WriteTo(pathname string) error
ReadFrom(pathname string) error
}
type FileTreeIndex struct {
fs fs.ISimpleFS
index *fsearch.FSearch
}
func NewFileTreeIndex(fs fs.ISimpleFS) *FileTreeIndex {
func NewFileTreeIndex(fs fs.ISimpleFS, pathSeparator string, maxResultSize int) *FileTreeIndex {
return &FileTreeIndex{
fs: fs,
// TODO: support max result size config
index: fsearch.New("/", 1024),
index: fsearch.New(pathSeparator, maxResultSize),
}
}

View file

@ -48,6 +48,7 @@ type ServerCfg struct {
WriteTimeout int `json:"writeTimeout" yaml:"writeTimeout"`
MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
PublicPath string `json:"publicPath" yaml:"publicPath"`
SearchResultLimit int `json:"searchResultLimit" yaml:"searchResultLimit"`
}
type WorkerPoolCfg struct {
@ -112,6 +113,7 @@ func DefaultConfigStruct() *Config {
WriteTimeout: 1000 * 3600 * 24, // 1 day
MaxHeaderBytes: 512,
PublicPath: "static/public",
SearchResultLimit: 16,
},
Workers: &WorkerPoolCfg{
QueueSize: 1024,

View file

@ -133,6 +133,7 @@ func TestLoadCfg(t *testing.T) {
WriteTimeout: 1,
MaxHeaderBytes: 1,
PublicPath: "1",
SearchResultLimit: 16,
},
Workers: &WorkerPoolCfg{
QueueSize: 1,
@ -199,6 +200,7 @@ func TestLoadCfg(t *testing.T) {
WriteTimeout: 4,
MaxHeaderBytes: 4,
PublicPath: "4",
SearchResultLimit: 16,
},
Workers: &WorkerPoolCfg{
QueueSize: 4,
@ -267,6 +269,7 @@ func TestLoadCfg(t *testing.T) {
WriteTimeout: 4,
MaxHeaderBytes: 4,
PublicPath: "4",
SearchResultLimit: 16,
},
Workers: &WorkerPoolCfg{
QueueSize: 4,
@ -335,6 +338,7 @@ func TestLoadCfg(t *testing.T) {
WriteTimeout: 4,
MaxHeaderBytes: 4,
PublicPath: "4",
SearchResultLimit: 16,
},
Workers: &WorkerPoolCfg{
QueueSize: 4,

View file

@ -38,6 +38,7 @@ import (
"github.com/ihexxa/quickshare/src/iolimiter"
"github.com/ihexxa/quickshare/src/kvstore"
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
"github.com/ihexxa/quickshare/src/search/fileindex"
"github.com/ihexxa/quickshare/src/worker/localworker"
qsstatic "github.com/ihexxa/quickshare/static"
)
@ -140,6 +141,9 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
filesystem := local.NewLocalFS(rootPath, 0660, opensLimit, openTTL, readerTTL, ider)
jwtEncDec := jwt.NewJWTEncDec(secret)
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit)
dbPath := cfg.GrabString("Db.DbPath")
dbDir := filepath.Dir(dbPath)
if err := filesystem.MkdirAll(dbDir); err != nil {
@ -196,6 +200,7 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
deps.SetID(ider)
deps.SetLog(logger)
deps.SetLimiter(limiter)
deps.SetIFileIndex(fileIndex)
queueSize := cfg.GrabInt("Workers.QueueSize")
sleepCyc := cfg.GrabInt("Workers.SleepCyc")

Binary file not shown.