feat: enable subdirectory search

This commit is contained in:
divyam234 2024-06-01 04:44:47 +05:30
parent 5a66bf38f2
commit 6ee7c82a2b
7 changed files with 26 additions and 13 deletions

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -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)
}

View file

@ -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

View file

@ -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

View file

@ -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"`

View file

@ -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{}