...

Source file src/github.com/gin-gonic/gin/fs.go

Documentation: github.com/gin-gonic/gin

     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  	fs := http.Dir(root)
    26  	if listDirectory {
    27  		return fs
    28  	}
    29  	return &onlyFilesFS{fs}
    30  }
    31  
    32  // Open conforms to http.Filesystem.
    33  func (fs onlyFilesFS) Open(name string) (http.File, error) {
    34  	f, err := fs.fs.Open(name)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return neuteredReaddirFile{f}, nil
    39  }
    40  
    41  // Readdir overrides the http.File default implementation.
    42  func (f neuteredReaddirFile) Readdir(_ int) ([]os.FileInfo, error) {
    43  	// this disables directory listing
    44  	return nil, nil
    45  }
    46  

View as plain text