From 6ee7c82a2bd251ba1bd32ad3d718feee26b27fc3 Mon Sep 17 00:00:00 2001 From: divyam234 <47589864+divyam234@users.noreply.github.com> Date: Sat, 1 Jun 2024 04:44:47 +0530 Subject: [PATCH] feat: enable subdirectory search --- go.mod | 1 + go.sum | 2 ++ internal/database/database.go | 2 ++ .../20240531194813_modify_constraint.sql | 5 ----- .../20240531194913_modify_constraint.sql | 6 +++++ pkg/schemas/file.go | 1 + pkg/services/file.go | 22 ++++++++++++------- 7 files changed, 26 insertions(+), 13 deletions(-) delete mode 100644 internal/database/migrations/20240531194813_modify_constraint.sql create mode 100644 internal/database/migrations/20240531194913_modify_constraint.sql diff --git a/go.mod b/go.mod index 2c3967d..ea877ca 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( ) require ( + github.com/WinterYukky/gorm-extra-clause-plugin v0.2.1 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect diff --git a/go.sum b/go.sum index 13a829d..ada7677 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/WinterYukky/gorm-extra-clause-plugin v0.2.1 h1:G0e4eFRrh3WdM1I3EKKidV2yF5J09uRIJlKYxt6zNR4= +github.com/WinterYukky/gorm-extra-clause-plugin v0.2.1/go.mod h1:qAN5KRJJTCM49X2wUHZAVB3rfvO8A8L0ISd/uB1WM5s= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= diff --git a/internal/database/database.go b/internal/database/database.go index f28f844..36a3028 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -6,6 +6,7 @@ import ( "github.com/divyam234/teldrive/internal/config" "github.com/divyam234/teldrive/pkg/logging" + extraClausePlugin "github.com/WinterYukky/gorm-extra-clause-plugin" "go.uber.org/zap/zapcore" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -37,6 +38,7 @@ func NewDatabase(cfg *config.Config) (*gorm.DB, error) { logging.DefaultLogger().Warnf("failed to open database: %v", err) time.Sleep(500 * time.Millisecond) } + db.Use(extraClausePlugin.New()) if err != nil { logging.DefaultLogger().Fatalf("database: %v", err) } diff --git a/internal/database/migrations/20240531194813_modify_constraint.sql b/internal/database/migrations/20240531194813_modify_constraint.sql deleted file mode 100644 index f50b489..0000000 --- a/internal/database/migrations/20240531194813_modify_constraint.sql +++ /dev/null @@ -1,5 +0,0 @@ --- +goose Up --- +goose StatementBegin -ALTER TABLE teldrive.uploads DROP CONSTRAINT IF EXISTS uploads_pk; -ALTER TABLE teldrive.uploads ADD CONSTRAINT uploads_pk PRIMARY KEY (part_id,channel_id); --- +goose StatementEnd \ No newline at end of file diff --git a/internal/database/migrations/20240531194913_modify_constraint.sql b/internal/database/migrations/20240531194913_modify_constraint.sql new file mode 100644 index 0000000..9511628 --- /dev/null +++ b/internal/database/migrations/20240531194913_modify_constraint.sql @@ -0,0 +1,6 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE teldrive.uploads DROP CONSTRAINT IF EXISTS uploads_pk; +ALTER TABLE teldrive.uploads DROP CONSTRAINT IF EXISTS uploads_pkey; +ALTER TABLE teldrive.uploads ADD CONSTRAINT uploads_pkey PRIMARY KEY (part_id,channel_id); +-- +goose StatementEnd \ No newline at end of file diff --git a/pkg/schemas/file.go b/pkg/schemas/file.go index 98986be..5644c84 100644 --- a/pkg/schemas/file.go +++ b/pkg/schemas/file.go @@ -15,6 +15,7 @@ type FileQuery struct { Type string `form:"type"` Path string `form:"path"` Op string `form:"op"` + DeepSearch bool `form:"deepSearch"` Starred *bool `form:"starred"` ParentID string `form:"parentId"` Category string `form:"category"` diff --git a/pkg/services/file.go b/pkg/services/file.go index a2c6219..7f273bb 100644 --- a/pkg/services/file.go +++ b/pkg/services/file.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/WinterYukky/gorm-extra-clause-plugin/exclause" "github.com/divyam234/teldrive/internal/cache" "github.com/divyam234/teldrive/internal/category" "github.com/divyam234/teldrive/internal/config" @@ -188,21 +189,23 @@ func (fs *FileService) ListFiles(userId int64, fquery *schemas.FileQuery) (*sche } query := fs.db.Limit(fquery.PerPage) - - filter := &models.File{UserID: userId, Status: "active"} - setOrderFilter(query, fquery) if fquery.Op == "list" { - + filter := &models.File{UserID: userId, Status: "active"} query.Order("type DESC").Order(getOrder(fquery)).Where("parent_id = ?", pathId). Model(filter).Where(&filter) } else if fquery.Op == "find" { - - if fquery.Path != "" && (fquery.Name != "" || fquery.Query != "") { + if !fquery.DeepSearch && pathId != "" && (fquery.Name != "" || fquery.Query != "") { query.Where("parent_id = ?", pathId) fquery.Path = "" + } else if fquery.DeepSearch && pathId != "" && fquery.Query != "" { + query = fs.db.Clauses(exclause.With{Recursive: true, CTEs: []exclause.CTE{{Name: "subdirs", + Subquery: exclause.Subquery{DB: fs.db.Model(&models.File{Id: pathId}).Select("id", "parent_id").Clauses(exclause.NewUnion("ALL ?", + fs.db.Table("teldrive.files as f").Select("f.id", "f.parent_id"). + Joins("inner join subdirs ON f.parent_id = subdirs.id")))}}}}).Where("files.id in (select id from subdirs)") + fquery.Path = "" } if fquery.UpdatedAt != "" { @@ -258,6 +261,7 @@ func (fs *FileService) ListFiles(userId int64, fquery *schemas.FileQuery) (*sche } + filter := &models.File{UserID: userId, Status: "active"} filter.Name = fquery.Name filter.ParentID = fquery.ParentID filter.Path = fquery.Path @@ -269,10 +273,12 @@ func (fs *FileService) ListFiles(userId int64, fquery *schemas.FileQuery) (*sche query.Order("type DESC").Order(getOrder(fquery)). Model(&filter).Where(&filter) + query.Limit(fquery.PerPage) + setOrderFilter(query, fquery) } - if fquery.Path == "" { - query.Select("*,(select path from teldrive.files as f where f.id = files.parent_id) as parent_path") + if fquery.Path == "" || fquery.DeepSearch { + query.Select("*,(select path from teldrive.files as ff where ff.id = files.parent_id) as parent_path") } files := []schemas.FileOut{}