-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathlocal_file.go
More file actions
49 lines (40 loc) · 890 Bytes
/
local_file.go
File metadata and controls
49 lines (40 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package static
import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
)
const INDEX = "index.html"
type localFileSystem struct {
http.FileSystem
root string
indexes bool
}
func LocalFile(root string, indexes bool) *localFileSystem {
return &localFileSystem{
FileSystem: gin.Dir(root, indexes),
root: root,
indexes: indexes,
}
}
func (l *localFileSystem) Exists(prefix string, path string) bool {
// Check if path starts with prefix
p := strings.TrimPrefix(path, prefix)
if len(p) >= len(path) {
return false
}
name := filepath.Join(l.root, p)
stats, err := os.Stat(name)
if err != nil {
return false
}
// If it's a directory and indexes are disabled, check for index file
if stats.IsDir() && !l.indexes {
indexPath := filepath.Join(name, INDEX)
_, err := os.Stat(indexPath)
return err == nil
}
return true
}