Refactor folder creation logic to reload directory from backend and normalize parent path
This commit is contained in:
@@ -264,15 +264,22 @@ func (db *DB) GetOrgFiles(ctx context.Context, orgID uuid.UUID, path string, q s
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
// Basic search and pagination. Returns files under the given path (including nested).
|
||||
// Basic search and pagination. Returns only direct children of the given path.
|
||||
// For root ("/"), we want files where path doesn't contain "/" after the first character.
|
||||
// For subdirs, we want files where path starts with parent but has no additional "/" after parent.
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT id, org_id::text, user_id::text, name, path, type, size, last_modified, created_at
|
||||
FROM files
|
||||
WHERE org_id = $1 AND path != $2 AND path LIKE $2 || '/%'
|
||||
AND ($4 = '' OR name ILIKE '%' || $4 || '%')
|
||||
WHERE org_id = $1
|
||||
AND path != $2
|
||||
AND (
|
||||
($2 = '/' AND path LIKE '/%' AND path NOT LIKE '/%/%')
|
||||
OR ($2 != '/' AND path LIKE $2 || '/%' AND path NOT LIKE $2 || '/%/%')
|
||||
)
|
||||
AND ($3 = '' OR name ILIKE '%' || $3 || '%')
|
||||
ORDER BY name
|
||||
LIMIT $5 OFFSET $6
|
||||
`, orgID, path, path, q, pageSize, offset)
|
||||
LIMIT $4 OFFSET $5
|
||||
`, orgID, path, q, pageSize, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -309,11 +316,17 @@ func (db *DB) GetUserFiles(ctx context.Context, userID uuid.UUID, path string, q
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
// Return only direct children of the given path
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT id, org_id::text, user_id::text, name, path, type, size, last_modified, created_at
|
||||
FROM files
|
||||
WHERE user_id = $1 AND path LIKE $2 || '%'
|
||||
AND ($3 = '' OR name ILIKE '%' || $3 || '%')
|
||||
WHERE user_id = $1
|
||||
AND path != $2
|
||||
AND (
|
||||
($2 = '/' AND path LIKE '/%' AND path NOT LIKE '/%/%')
|
||||
OR ($2 != '/' AND path LIKE $2 || '/%' AND path NOT LIKE $2 || '/%/%')
|
||||
)
|
||||
AND ($3 = '' OR name ILIKE '%' || $3 || '%')
|
||||
ORDER BY name
|
||||
LIMIT $4 OFFSET $5
|
||||
`, userID, path, q, pageSize, offset)
|
||||
|
||||
@@ -27,6 +27,7 @@ func CORS(next http.Handler) http.Handler {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
w.Header().Set("Access-Control-Max-Age", "3600")
|
||||
|
||||
if r.Method == http.MethodOptions {
|
||||
|
||||
Reference in New Issue
Block a user