how to avoid sending dotfile or files in a dot folder on a http response

how to avoid sending dotfile or files in a dot folder on a http response

1 Like

Check if the filename starts with a dot and return an error?

2 Likes

http.FileServer expects http.FileSystem as it’s argument. Create one structure and implement Open(name string) (http.File, error) which the one behaviour http.FileSystem has and filter files based on your needs.

type PublicDir struct{
    dir http.Dir
    AllowedExtensions []string
}
func (d PublicDir) Open(fileName string) (http.File, error) {
    // check extension of the file and do what you need to do
}
public := http.FileServer(PublicDir{http.Dir("public/")})
http.Handle("/", http.StripPrefix("/", public))
2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.