github.com/edermi/gophish_mods@v0.7.0/controllers/static.go (about)

     1  package controllers
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  )
     7  
     8  // UnindexedFileSystem is an implementation of a standard http.FileSystem
     9  // without the ability to list files in the directory.
    10  // This implementation is largely inspired by
    11  // https://www.alexedwards.net/blog/disable-http-fileserver-directory-listings
    12  type UnindexedFileSystem struct {
    13  	fs http.FileSystem
    14  }
    15  
    16  // Open returns a file from the static directory. If the requested path ends
    17  // with a slash, there is a check for an index.html file. If none exists, then
    18  // an error is returned.
    19  func (ufs UnindexedFileSystem) Open(name string) (http.File, error) {
    20  	f, err := ufs.fs.Open(name)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	s, err := f.Stat()
    26  	if s.IsDir() {
    27  		index := strings.TrimSuffix(name, "/") + "/index.html"
    28  		indexFile, err := ufs.fs.Open(index)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		return indexFile, nil
    33  	}
    34  	return f, nil
    35  }