MCPcopy Index your code
hub / github.com/labstack/echo / listDir

Function listDir

middleware/static.go:323–356  ·  view source on GitHub ↗
(t *template.Template, pathInFs string, filesystem fs.FS, res http.ResponseWriter)

Source from the content-addressed store, hash-verified

321}
322
323func listDir(t *template.Template, pathInFs string, filesystem fs.FS, res http.ResponseWriter) error {
324 files, err := fs.ReadDir(filesystem, pathInFs)
325 if err != nil {
326 return fmt.Errorf("static middleware failed to read directory for listing: %w", err)
327 }
328
329 // Create directory index
330 res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
331 data := struct {
332 Name string
333 Files []any
334 }{
335 Name: pathInFs,
336 }
337
338 for _, f := range files {
339 var size int64
340 if !f.IsDir() {
341 info, err := f.Info()
342 if err != nil {
343 return fmt.Errorf("static middleware failed to get file info for listing: %w", err)
344 }
345 size = info.Size()
346 }
347
348 data.Files = append(data.Files, struct {
349 Name string
350 Dir bool
351 Size string
352 }{f.Name(), f.IsDir(), format(size)})
353 }
354
355 return t.Execute(res, data)
356}
357
358// format formats bytes integer to human readable string.
359// For example, 31323 bytes will return 30.59KB.

Callers 1

ToMiddlewareMethod · 0.85

Calls 3

formatFunction · 0.85
SetMethod · 0.80
HeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…