fs.go 605 B

12345678910111213141516171819202122232425262728293031
  1. package httptoo
  2. import (
  3. "net/http"
  4. "os"
  5. )
  6. // Wraps a http.FileSystem, disabling directory listings, per the commonly
  7. // requested feature at https://groups.google.com/forum/#!topic/golang-
  8. // nuts/bStLPdIVM6w .
  9. type JustFilesFilesystem struct {
  10. Fs http.FileSystem
  11. }
  12. func (fs JustFilesFilesystem) Open(name string) (http.File, error) {
  13. f, err := fs.Fs.Open(name)
  14. if err != nil {
  15. return nil, err
  16. }
  17. d, err := f.Stat()
  18. if err != nil {
  19. f.Close()
  20. return nil, err
  21. }
  22. if d.IsDir() {
  23. f.Close()
  24. // This triggers http.FileServer to show a 404.
  25. return nil, os.ErrNotExist
  26. }
  27. return f, nil
  28. }