feat(dep/fileindex): enable fileindex in file managing
This commit is contained in:
parent
28b7113d27
commit
dff79ed87f
7 changed files with 111 additions and 44 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/ihexxa/quickshare/src/idgen"
|
"github.com/ihexxa/quickshare/src/idgen"
|
||||||
"github.com/ihexxa/quickshare/src/iolimiter"
|
"github.com/ihexxa/quickshare/src/iolimiter"
|
||||||
"github.com/ihexxa/quickshare/src/kvstore"
|
"github.com/ihexxa/quickshare/src/kvstore"
|
||||||
|
"github.com/ihexxa/quickshare/src/search/fileindex"
|
||||||
"github.com/ihexxa/quickshare/src/worker"
|
"github.com/ihexxa/quickshare/src/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ type Deps struct {
|
||||||
workers worker.IWorkerPool
|
workers worker.IWorkerPool
|
||||||
boltStore *boltstore.BoltStore
|
boltStore *boltstore.BoltStore
|
||||||
cron cron.ICron
|
cron cron.ICron
|
||||||
|
fileIndex fileindex.IFileIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDeps(cfg gocfg.ICfg) *Deps {
|
func NewDeps(cfg gocfg.ICfg) *Deps {
|
||||||
|
@ -139,3 +141,11 @@ func (deps *Deps) Cron() cron.ICron {
|
||||||
func (deps *Deps) SetCron(cronImp cron.ICron) {
|
func (deps *Deps) SetCron(cronImp cron.ICron) {
|
||||||
deps.cron = cronImp
|
deps.cron = cronImp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (deps *Deps) FileIndex() fileindex.IFileIndex {
|
||||||
|
return deps.fileIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (deps *Deps) SetIFileIndex(index fileindex.IFileIndex) {
|
||||||
|
deps.fileIndex = index
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"github.com/ihexxa/gocfg"
|
"github.com/ihexxa/gocfg"
|
||||||
"github.com/ihexxa/multipart"
|
"github.com/ihexxa/multipart"
|
||||||
|
|
||||||
|
"github.com/ihexxa/fsearch"
|
||||||
"github.com/ihexxa/quickshare/src/db"
|
"github.com/ihexxa/quickshare/src/db"
|
||||||
"github.com/ihexxa/quickshare/src/db/userstore"
|
"github.com/ihexxa/quickshare/src/db/userstore"
|
||||||
"github.com/ihexxa/quickshare/src/depidx"
|
"github.com/ihexxa/quickshare/src/depidx"
|
||||||
|
@ -210,6 +211,12 @@ func (h *FileHandlers) Create(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = h.deps.FileIndex().AddPath(fsFilePath)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(q.Resp(200))
|
c.JSON(q.Resp(200))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -290,6 +297,12 @@ func (h *FileHandlers) Delete(c *gin.Context) {
|
||||||
txErr = err
|
txErr = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = h.deps.FileIndex().DelPath(filePath)
|
||||||
|
if err != nil && !errors.Is(err, fsearch.ErrNotFound) {
|
||||||
|
txErr = err
|
||||||
|
return
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if txErr != nil {
|
if txErr != nil {
|
||||||
|
@ -364,6 +377,11 @@ func (h *FileHandlers) Mkdir(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = h.deps.FileIndex().AddPath(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(q.ErrResp(c, 500, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
c.JSON(q.Resp(200))
|
c.JSON(q.Resp(200))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,6 +439,18 @@ func (h *FileHandlers) Move(c *gin.Context) {
|
||||||
return
|
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))
|
c.JSON(q.Resp(200))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -527,6 +557,12 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
|
||||||
txErr, statusCode = err, 500
|
txErr, statusCode = err, 500
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = h.deps.FileIndex().AddPath(fsFilePath)
|
||||||
|
if err != nil {
|
||||||
|
txErr, statusCode = err, 500
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if txErr != nil {
|
if txErr != nil {
|
||||||
|
|
|
@ -12,16 +12,26 @@ import (
|
||||||
"github.com/ihexxa/quickshare/src/fs"
|
"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 {
|
type FileTreeIndex struct {
|
||||||
fs fs.ISimpleFS
|
fs fs.ISimpleFS
|
||||||
index *fsearch.FSearch
|
index *fsearch.FSearch
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileTreeIndex(fs fs.ISimpleFS) *FileTreeIndex {
|
func NewFileTreeIndex(fs fs.ISimpleFS, pathSeparator string, maxResultSize int) *FileTreeIndex {
|
||||||
return &FileTreeIndex{
|
return &FileTreeIndex{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
// TODO: support max result size config
|
// TODO: support max result size config
|
||||||
index: fsearch.New("/", 1024),
|
index: fsearch.New(pathSeparator, maxResultSize),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,13 +41,14 @@ type Secrets struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerCfg struct {
|
type ServerCfg struct {
|
||||||
Debug bool `json:"debug" yaml:"debug"`
|
Debug bool `json:"debug" yaml:"debug"`
|
||||||
Host string `json:"host" yaml:"host"`
|
Host string `json:"host" yaml:"host"`
|
||||||
Port int `json:"port" yaml:"port" cfg:"env"`
|
Port int `json:"port" yaml:"port" cfg:"env"`
|
||||||
ReadTimeout int `json:"readTimeout" yaml:"readTimeout"`
|
ReadTimeout int `json:"readTimeout" yaml:"readTimeout"`
|
||||||
WriteTimeout int `json:"writeTimeout" yaml:"writeTimeout"`
|
WriteTimeout int `json:"writeTimeout" yaml:"writeTimeout"`
|
||||||
MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
|
MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"`
|
||||||
PublicPath string `json:"publicPath" yaml:"publicPath"`
|
PublicPath string `json:"publicPath" yaml:"publicPath"`
|
||||||
|
SearchResultLimit int `json:"searchResultLimit" yaml:"searchResultLimit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkerPoolCfg struct {
|
type WorkerPoolCfg struct {
|
||||||
|
@ -105,13 +106,14 @@ func DefaultConfigStruct() *Config {
|
||||||
TokenSecret: "", // it will auto generated if it is left as empty
|
TokenSecret: "", // it will auto generated if it is left as empty
|
||||||
},
|
},
|
||||||
Server: &ServerCfg{
|
Server: &ServerCfg{
|
||||||
Debug: false,
|
Debug: false,
|
||||||
Host: "0.0.0.0",
|
Host: "0.0.0.0",
|
||||||
Port: 8686,
|
Port: 8686,
|
||||||
ReadTimeout: 2000,
|
ReadTimeout: 2000,
|
||||||
WriteTimeout: 1000 * 3600 * 24, // 1 day
|
WriteTimeout: 1000 * 3600 * 24, // 1 day
|
||||||
MaxHeaderBytes: 512,
|
MaxHeaderBytes: 512,
|
||||||
PublicPath: "static/public",
|
PublicPath: "static/public",
|
||||||
|
SearchResultLimit: 16,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 1024,
|
QueueSize: 1024,
|
||||||
|
|
|
@ -126,13 +126,14 @@ func TestLoadCfg(t *testing.T) {
|
||||||
TokenSecret: "1",
|
TokenSecret: "1",
|
||||||
},
|
},
|
||||||
Server: &ServerCfg{
|
Server: &ServerCfg{
|
||||||
Debug: true,
|
Debug: true,
|
||||||
Host: "1",
|
Host: "1",
|
||||||
Port: 1,
|
Port: 1,
|
||||||
ReadTimeout: 1,
|
ReadTimeout: 1,
|
||||||
WriteTimeout: 1,
|
WriteTimeout: 1,
|
||||||
MaxHeaderBytes: 1,
|
MaxHeaderBytes: 1,
|
||||||
PublicPath: "1",
|
PublicPath: "1",
|
||||||
|
SearchResultLimit: 16,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 1,
|
QueueSize: 1,
|
||||||
|
@ -192,13 +193,14 @@ func TestLoadCfg(t *testing.T) {
|
||||||
TokenSecret: "4",
|
TokenSecret: "4",
|
||||||
},
|
},
|
||||||
Server: &ServerCfg{
|
Server: &ServerCfg{
|
||||||
Debug: false,
|
Debug: false,
|
||||||
Host: "4",
|
Host: "4",
|
||||||
Port: 4,
|
Port: 4,
|
||||||
ReadTimeout: 4,
|
ReadTimeout: 4,
|
||||||
WriteTimeout: 4,
|
WriteTimeout: 4,
|
||||||
MaxHeaderBytes: 4,
|
MaxHeaderBytes: 4,
|
||||||
PublicPath: "4",
|
PublicPath: "4",
|
||||||
|
SearchResultLimit: 16,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 4,
|
QueueSize: 4,
|
||||||
|
@ -260,13 +262,14 @@ func TestLoadCfg(t *testing.T) {
|
||||||
TokenSecret: "4",
|
TokenSecret: "4",
|
||||||
},
|
},
|
||||||
Server: &ServerCfg{
|
Server: &ServerCfg{
|
||||||
Debug: false,
|
Debug: false,
|
||||||
Host: "4",
|
Host: "4",
|
||||||
Port: 4,
|
Port: 4,
|
||||||
ReadTimeout: 4,
|
ReadTimeout: 4,
|
||||||
WriteTimeout: 4,
|
WriteTimeout: 4,
|
||||||
MaxHeaderBytes: 4,
|
MaxHeaderBytes: 4,
|
||||||
PublicPath: "4",
|
PublicPath: "4",
|
||||||
|
SearchResultLimit: 16,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 4,
|
QueueSize: 4,
|
||||||
|
@ -328,13 +331,14 @@ func TestLoadCfg(t *testing.T) {
|
||||||
TokenSecret: "4",
|
TokenSecret: "4",
|
||||||
},
|
},
|
||||||
Server: &ServerCfg{
|
Server: &ServerCfg{
|
||||||
Debug: false,
|
Debug: false,
|
||||||
Host: "4",
|
Host: "4",
|
||||||
Port: 4,
|
Port: 4,
|
||||||
ReadTimeout: 4,
|
ReadTimeout: 4,
|
||||||
WriteTimeout: 4,
|
WriteTimeout: 4,
|
||||||
MaxHeaderBytes: 4,
|
MaxHeaderBytes: 4,
|
||||||
PublicPath: "4",
|
PublicPath: "4",
|
||||||
|
SearchResultLimit: 16,
|
||||||
},
|
},
|
||||||
Workers: &WorkerPoolCfg{
|
Workers: &WorkerPoolCfg{
|
||||||
QueueSize: 4,
|
QueueSize: 4,
|
||||||
|
|
|
@ -38,6 +38,7 @@ import (
|
||||||
"github.com/ihexxa/quickshare/src/iolimiter"
|
"github.com/ihexxa/quickshare/src/iolimiter"
|
||||||
"github.com/ihexxa/quickshare/src/kvstore"
|
"github.com/ihexxa/quickshare/src/kvstore"
|
||||||
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
|
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
|
||||||
|
"github.com/ihexxa/quickshare/src/search/fileindex"
|
||||||
"github.com/ihexxa/quickshare/src/worker/localworker"
|
"github.com/ihexxa/quickshare/src/worker/localworker"
|
||||||
qsstatic "github.com/ihexxa/quickshare/static"
|
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)
|
filesystem := local.NewLocalFS(rootPath, 0660, opensLimit, openTTL, readerTTL, ider)
|
||||||
jwtEncDec := jwt.NewJWTEncDec(secret)
|
jwtEncDec := jwt.NewJWTEncDec(secret)
|
||||||
|
|
||||||
|
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
|
||||||
|
fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit)
|
||||||
|
|
||||||
dbPath := cfg.GrabString("Db.DbPath")
|
dbPath := cfg.GrabString("Db.DbPath")
|
||||||
dbDir := filepath.Dir(dbPath)
|
dbDir := filepath.Dir(dbPath)
|
||||||
if err := filesystem.MkdirAll(dbDir); err != nil {
|
if err := filesystem.MkdirAll(dbDir); err != nil {
|
||||||
|
@ -196,6 +200,7 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
|
||||||
deps.SetID(ider)
|
deps.SetID(ider)
|
||||||
deps.SetLog(logger)
|
deps.SetLog(logger)
|
||||||
deps.SetLimiter(limiter)
|
deps.SetLimiter(limiter)
|
||||||
|
deps.SetIFileIndex(fileIndex)
|
||||||
|
|
||||||
queueSize := cfg.GrabInt("Workers.QueueSize")
|
queueSize := cfg.GrabInt("Workers.QueueSize")
|
||||||
sleepCyc := cfg.GrabInt("Workers.SleepCyc")
|
sleepCyc := cfg.GrabInt("Workers.SleepCyc")
|
||||||
|
|
BIN
src/server/testdata/test_quickshare.db
vendored
BIN
src/server/testdata/test_quickshare.db
vendored
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue