2021-01-31 23:12:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-02-01 17:39:19 +00:00
|
|
|
// Since the gin-static middleware uses a version of http.Filesystem with an extra Exists() func, we extend it here.
|
|
|
|
|
2021-01-31 23:12:50 +00:00
|
|
|
type httpFS struct {
|
2021-02-01 17:39:19 +00:00
|
|
|
hfs http.FileSystem // Created by converting fs.FS using http.FS()
|
2021-01-31 23:12:50 +00:00
|
|
|
fs fs.FS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f httpFS) Open(name string) (http.File, error) {
|
2021-02-02 15:44:30 +00:00
|
|
|
return f.hfs.Open("web" + name)
|
2021-01-31 23:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f httpFS) Exists(prefix string, filepath string) bool {
|
|
|
|
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
|
2021-02-02 15:44:30 +00:00
|
|
|
stats, err := fs.Stat(f.fs, "web/"+p)
|
2021-01-31 23:12:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if stats.IsDir() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|