mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-09-13 01:44:41 +08:00
refactor: list files by parentId explicitly
This commit is contained in:
parent
94ef73d62c
commit
7e5e3adf5f
2 changed files with 61 additions and 13 deletions
|
@ -0,0 +1,49 @@
|
||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE OR REPLACE FUNCTION teldrive.get_file_from_path(full_path text,u_id bigint)
|
||||||
|
RETURNS setof teldrive.files AS $$
|
||||||
|
DECLARE
|
||||||
|
target_id text;
|
||||||
|
begin
|
||||||
|
|
||||||
|
IF full_path = '/' then
|
||||||
|
RETURN QUERY select * from teldrive.files as root where root.parent_id = 'root' and root.user_id = u_id;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
WITH RECURSIVE dir_hierarchy AS (
|
||||||
|
SELECT
|
||||||
|
root.id,
|
||||||
|
root.name,
|
||||||
|
root.parent_id,
|
||||||
|
0 AS depth,
|
||||||
|
'' as path
|
||||||
|
FROM
|
||||||
|
teldrive.files as root
|
||||||
|
WHERE
|
||||||
|
root.parent_id = 'root' AND root.user_id = u_id
|
||||||
|
|
||||||
|
UNION ALL
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
f.id,
|
||||||
|
f.name,
|
||||||
|
f.parent_id,
|
||||||
|
dh.depth + 1 AS depth,
|
||||||
|
dh.path || '/' || f.name
|
||||||
|
FROM
|
||||||
|
teldrive.files f
|
||||||
|
JOIN
|
||||||
|
dir_hierarchy dh ON dh.id = f.parent_id
|
||||||
|
WHERE f.type = 'folder' AND f.user_id = u_id
|
||||||
|
)
|
||||||
|
|
||||||
|
SELECT id into target_id FROM dir_hierarchy dh
|
||||||
|
WHERE dh.path = full_path
|
||||||
|
ORDER BY dh.depth DESC
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
RETURN QUERY select * from teldrive.files where id=target_id;
|
||||||
|
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
-- +goose StatementEnd
|
|
@ -155,33 +155,32 @@ func (fs *FileService) GetFileByID(id string) (*schemas.FileOutFull, *types.AppE
|
||||||
|
|
||||||
func (fs *FileService) ListFiles(userId int64, fquery *schemas.FileQuery) (*schemas.FileResponse, *types.AppError) {
|
func (fs *FileService) ListFiles(userId int64, fquery *schemas.FileQuery) (*schemas.FileResponse, *types.AppError) {
|
||||||
|
|
||||||
var (
|
var parentID string
|
||||||
parent *models.File
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if fquery.Path != "" {
|
if fquery.Path != "" && fquery.ParentID == "" {
|
||||||
parent, err = fs.getFileFromPath(fquery.Path, userId)
|
parent, err := fs.getFileFromPath(fquery.Path, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &types.AppError{Error: err, Code: http.StatusNotFound}
|
return nil, &types.AppError{Error: err, Code: http.StatusNotFound}
|
||||||
}
|
}
|
||||||
|
parentID = parent.Id
|
||||||
|
} else if fquery.ParentID != "" {
|
||||||
|
parentID = fquery.ParentID
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fs.db.Limit(fquery.PerPage)
|
query := fs.db.Limit(fquery.PerPage)
|
||||||
setOrderFilter(query, fquery)
|
setOrderFilter(query, fquery)
|
||||||
|
|
||||||
if fquery.Op == "list" {
|
if fquery.Op == "list" {
|
||||||
filter := &models.File{UserID: userId, Status: "active"}
|
filter := &models.File{UserID: userId, Status: "active", ParentID: parentID}
|
||||||
query.Order("type DESC").Order(getOrder(fquery)).Where("parent_id = ?", parent.Id).
|
query.Order("type DESC").Order(getOrder(fquery)).Model(filter).Where(&filter)
|
||||||
Model(filter).Where(&filter)
|
|
||||||
|
|
||||||
} else if fquery.Op == "find" {
|
} else if fquery.Op == "find" {
|
||||||
if !fquery.DeepSearch && parent != nil && (fquery.Name != "" || fquery.Query != "") {
|
if !fquery.DeepSearch && parentID != "" && (fquery.Name != "" || fquery.Query != "") {
|
||||||
query.Where("parent_id = ?", parent.Id)
|
query.Where("parent_id = ?", parentID)
|
||||||
fquery.Path = ""
|
fquery.Path = ""
|
||||||
} else if fquery.DeepSearch && parent != nil && fquery.Query != "" {
|
} else if fquery.DeepSearch && parentID != "" && fquery.Query != "" {
|
||||||
query = fs.db.Clauses(exclause.With{Recursive: true, CTEs: []exclause.CTE{{Name: "subdirs",
|
query = fs.db.Clauses(exclause.With{Recursive: true, CTEs: []exclause.CTE{{Name: "subdirs",
|
||||||
Subquery: exclause.Subquery{DB: fs.db.Model(&models.File{Id: parent.Id}).Select("id", "parent_id").Clauses(exclause.NewUnion("ALL ?",
|
Subquery: exclause.Subquery{DB: fs.db.Model(&models.File{Id: parentID}).Select("id", "parent_id").Clauses(exclause.NewUnion("ALL ?",
|
||||||
fs.db.Table("teldrive.files as f").Select("f.id", "f.parent_id").
|
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)")
|
Joins("inner join subdirs ON f.parent_id = subdirs.id")))}}}}).Where("files.id in (select id from subdirs)")
|
||||||
fquery.Path = ""
|
fquery.Path = ""
|
||||||
|
|
Loading…
Add table
Reference in a new issue