1
// Copyright 2017 Manu Martinez-Almeida.  All rights reserved.
2
// Use of this source code is governed by a MIT style
3
// license that can be found in the LICENSE file.
4

5
package gin
6

7
import (
8
	"net/http"
9
	"os"
10
)
11

12
type onlyFilesFS struct {
13
	fs http.FileSystem
14
}
15

16
type neuteredReaddirFile struct {
17
	http.File
18
}
19

20
// Dir returns a http.Filesystem that can be used by http.FileServer(). It is used internally
21
// in router.Static().
22
// if listDirectory == true, then it works the same as http.Dir() otherwise it returns
23
// a filesystem that prevents http.FileServer() to list the directory files.
24
func Dir(root string, listDirectory bool) http.FileSystem {
25 14
	fs := http.Dir(root)
26 14
	if listDirectory {
27 14
		return fs
28
	}
29 14
	return &onlyFilesFS{fs}
30
}
31

32
// Open conforms to http.Filesystem.
33
func (fs onlyFilesFS) Open(name string) (http.File, error) {
34 14
	f, err := fs.fs.Open(name)
35 14
	if err != nil {
36 14
		return nil, err
37
	}
38 14
	return neuteredReaddirFile{f}, nil
39
}
40

41
// Readdir overrides the http.File default implementation.
42
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
43
	// this disables directory listing
44 14
	return nil, nil
45
}

Read our documentation on viewing source code .

Loading