(t *template.Template, pathInFs string, filesystem fs.FS, res http.ResponseWriter)
| 321 | } |
| 322 | |
| 323 | func 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. |
no test coverage detected
searching dependent graphs…