(w http.ResponseWriter, r *http.Request)
| 433 | } |
| 434 | |
| 435 | func (s *Server) browseDirectories(w http.ResponseWriter, r *http.Request) { |
| 436 | requested := strings.TrimSpace(r.URL.Query().Get("path")) |
| 437 | target := requested |
| 438 | if target == "" { |
| 439 | target = s.defaultBrowsePath() |
| 440 | } |
| 441 | target, err := s.ensureBrowsePathAllowed(target) |
| 442 | if err != nil { |
| 443 | writeError(w, err) |
| 444 | return |
| 445 | } |
| 446 | |
| 447 | dirEntries, err := os.ReadDir(target) |
| 448 | if err != nil { |
| 449 | writeError(w, err) |
| 450 | return |
| 451 | } |
| 452 | |
| 453 | entries := make([]directoryBrowseEntry, 0, len(dirEntries)) |
| 454 | for _, entry := range dirEntries { |
| 455 | childPath := filepath.Join(target, entry.Name()) |
| 456 | childInfo, err := os.Stat(childPath) |
| 457 | if err != nil || !childInfo.IsDir() { |
| 458 | continue |
| 459 | } |
| 460 | if _, err := s.ensureBrowsePathAllowed(childPath); err != nil { |
| 461 | continue |
| 462 | } |
| 463 | entries = append(entries, directoryBrowseEntry{ |
| 464 | Name: entry.Name(), |
| 465 | Path: childPath, |
| 466 | }) |
| 467 | } |
| 468 | sort.Slice(entries, func(i, j int) bool { |
| 469 | left := strings.ToLower(entries[i].Name) |
| 470 | right := strings.ToLower(entries[j].Name) |
| 471 | if left == right { |
| 472 | return entries[i].Name < entries[j].Name |
| 473 | } |
| 474 | return left < right |
| 475 | }) |
| 476 | |
| 477 | parentPath := filepath.Dir(target) |
| 478 | var parent *string |
| 479 | if parentPath != "" && parentPath != target { |
| 480 | if allowedParent, err := s.ensureBrowsePathAllowed(parentPath); err == nil { |
| 481 | parent = &allowedParent |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | writeJSON(w, http.StatusOK, directoryBrowseResult{ |
| 486 | CurrentPath: target, |
| 487 | ParentPath: parent, |
| 488 | Entries: entries, |
| 489 | Shortcuts: s.browseShortcuts(), |
| 490 | }) |
| 491 | } |
| 492 |
nothing calls this directly
no test coverage detected