forked from keybase/client
/
httprootfs.go
52 lines (40 loc) · 1 KB
/
httprootfs.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2019 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libfs
import (
"net/http"
"os"
"strings"
"github.com/pkg/errors"
billy "gopkg.in/src-d/go-billy.v4"
)
type httpRootFileSystem struct {
rfs *RootFS
}
var _ http.FileSystem = httpRootFileSystem{}
type fileOnly struct {
billy.File
rfs *RootFS
}
// Readdir implements the http.File interface.
func (fo fileOnly) Readdir(count int) ([]os.FileInfo, error) {
return nil, errors.New("not supported")
}
// Stat implements the http.File interface.
func (fo fileOnly) Stat() (os.FileInfo, error) {
return fo.rfs.Stat(fo.File.Name())
}
func (hrfs httpRootFileSystem) Open(filename string) (entry http.File, err error) {
defer func() {
if err != nil {
err = translateErr(err)
}
}()
filename = strings.TrimPrefix(filename, "/")
f, err := hrfs.rfs.Open(filename)
if err != nil {
return nil, err
}
return fileOnly{File: f, rfs: hrfs.rfs}, nil
}