File: fs.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (31 lines) | stat: -rw-r--r-- 605 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package httptoo

import (
	"net/http"
	"os"
)

// Wraps a http.FileSystem, disabling directory listings, per the commonly
// requested feature at https://groups.google.com/forum/#!topic/golang-
// nuts/bStLPdIVM6w .
type JustFilesFilesystem struct {
	Fs http.FileSystem
}

func (fs JustFilesFilesystem) Open(name string) (http.File, error) {
	f, err := fs.Fs.Open(name)
	if err != nil {
		return nil, err
	}
	d, err := f.Stat()
	if err != nil {
		f.Close()
		return nil, err
	}
	if d.IsDir() {
		f.Close()
		// This triggers http.FileServer to show a 404.
		return nil, os.ErrNotExist
	}
	return f, nil
}