fs.go 594 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package gin
  2. import (
  3. "net/http"
  4. "os"
  5. )
  6. type (
  7. onlyfilesFS struct {
  8. fs http.FileSystem
  9. }
  10. neuteredReaddirFile struct {
  11. http.File
  12. }
  13. )
  14. func Dir(root string, listDirectory bool) http.FileSystem {
  15. fs := http.Dir(root)
  16. if listDirectory {
  17. return fs
  18. } else {
  19. return &onlyfilesFS{fs}
  20. }
  21. }
  22. func (fs onlyfilesFS) Open(name string) (http.File, error) {
  23. f, err := fs.fs.Open(name)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return neuteredReaddirFile{f}, nil
  28. }
  29. func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
  30. // this disables directory listing
  31. return nil, nil
  32. }