Skip to content

Commit

Permalink
refactor: list files by parentId explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Jun 7, 2024
1 parent 94ef73d commit 7e5e3ad
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
49 changes: 49 additions & 0 deletions internal/database/migrations/20240607114052_modify_functions.sql
Original file line number Diff line number Diff line change
@@ -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
25 changes: 12 additions & 13 deletions pkg/services/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

var (
parent *models.File
err error
)
var parentID string

if fquery.Path != "" {
parent, err = fs.getFileFromPath(fquery.Path, userId)
if fquery.Path != "" && fquery.ParentID == "" {
parent, err := fs.getFileFromPath(fquery.Path, userId)
if err != nil {
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)
setOrderFilter(query, fquery)

if fquery.Op == "list" {
filter := &models.File{UserID: userId, Status: "active"}
query.Order("type DESC").Order(getOrder(fquery)).Where("parent_id = ?", parent.Id).
Model(filter).Where(&filter)
filter := &models.File{UserID: userId, Status: "active", ParentID: parentID}
query.Order("type DESC").Order(getOrder(fquery)).Model(filter).Where(&filter)

} else if fquery.Op == "find" {
if !fquery.DeepSearch && parent != nil && (fquery.Name != "" || fquery.Query != "") {
query.Where("parent_id = ?", parent.Id)
if !fquery.DeepSearch && parentID != "" && (fquery.Name != "" || fquery.Query != "") {
query.Where("parent_id = ?", parentID)
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",
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").
Joins("inner join subdirs ON f.parent_id = subdirs.id")))}}}}).Where("files.id in (select id from subdirs)")
fquery.Path = ""
Expand Down

0 comments on commit 7e5e3ad

Please sign in to comment.