forked from CloudyKit/jet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
35 lines (29 loc) · 823 Bytes
/
loader.go
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
package httpfs
import (
"errors"
"io"
"net/http"
"github.com/iesreza/jet/v7"
)
type httpFileSystemLoader struct {
fs http.FileSystem
}
// NewLoader returns an initialized loader serving the passed http.FileSystem.
func NewLoader(fs http.FileSystem) (jet.Loader, error) {
if fs == nil {
return nil, errors.New("httpfs: nil http.Filesystem passed to NewLoader")
}
return &httpFileSystemLoader{fs: fs}, nil
}
// Open implements Loader.Open() on top of an http.FileSystem.
func (l *httpFileSystemLoader) Open(name string) (io.ReadCloser, error) {
return l.fs.Open(name)
}
// Exists implements Loader.Exists() on top of an http.FileSystem by trying to open the file.
func (l *httpFileSystemLoader) Exists(name string) bool {
if f, err := l.Open(name); err == nil {
f.Close()
return true
}
return false
}