podman
45 строк · 1.1 Кб
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
5package gin
6
7import (
8"net/http"
9"os"
10)
11
12type onlyFilesFS struct {
13fs http.FileSystem
14}
15
16type neuteredReaddirFile struct {
17http.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.
24func Dir(root string, listDirectory bool) http.FileSystem {
25fs := http.Dir(root)
26if listDirectory {
27return fs
28}
29return &onlyFilesFS{fs}
30}
31
32// Open conforms to http.Filesystem.
33func (fs onlyFilesFS) Open(name string) (http.File, error) {
34f, err := fs.fs.Open(name)
35if err != nil {
36return nil, err
37}
38return neuteredReaddirFile{f}, nil
39}
40
41// Readdir overrides the http.File default implementation.
42func (f neuteredReaddirFile) Readdir(_ int) ([]os.FileInfo, error) {
43// this disables directory listing
44return nil, nil
45}
46