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-01 20:25:20 +00:00
|
|
|
return f.hfs.Open("data/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-01 20:25:20 +00:00
|
|
|
stats, err := fs.Stat(f.fs, "data/web/"+p)
|
2021-01-31 23:12:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if stats.IsDir() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|