...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package afero
15
16 import (
17 "errors"
18 "net/http"
19 "os"
20 "path"
21 "path/filepath"
22 "strings"
23 "time"
24 )
25
26 type httpDir struct {
27 basePath string
28 fs HttpFs
29 }
30
31 func (d httpDir) Open(name string) (http.File, error) {
32 if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||
33 strings.Contains(name, "\x00") {
34 return nil, errors.New("http: invalid character in file path")
35 }
36 dir := string(d.basePath)
37 if dir == "" {
38 dir = "."
39 }
40
41 f, err := d.fs.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
42 if err != nil {
43 return nil, err
44 }
45 return f, nil
46 }
47
48 type HttpFs struct {
49 source Fs
50 }
51
52 func NewHttpFs(source Fs) *HttpFs {
53 return &HttpFs{source: source}
54 }
55
56 func (h HttpFs) Dir(s string) *httpDir {
57 return &httpDir{basePath: s, fs: h}
58 }
59
60 func (h HttpFs) Name() string { return "h HttpFs" }
61
62 func (h HttpFs) Create(name string) (File, error) {
63 return h.source.Create(name)
64 }
65
66 func (h HttpFs) Chmod(name string, mode os.FileMode) error {
67 return h.source.Chmod(name, mode)
68 }
69
70 func (h HttpFs) Chown(name string, uid, gid int) error {
71 return h.source.Chown(name, uid, gid)
72 }
73
74 func (h HttpFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
75 return h.source.Chtimes(name, atime, mtime)
76 }
77
78 func (h HttpFs) Mkdir(name string, perm os.FileMode) error {
79 return h.source.Mkdir(name, perm)
80 }
81
82 func (h HttpFs) MkdirAll(path string, perm os.FileMode) error {
83 return h.source.MkdirAll(path, perm)
84 }
85
86 func (h HttpFs) Open(name string) (http.File, error) {
87 f, err := h.source.Open(name)
88 if err == nil {
89 if httpfile, ok := f.(http.File); ok {
90 return httpfile, nil
91 }
92 }
93 return nil, err
94 }
95
96 func (h HttpFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
97 return h.source.OpenFile(name, flag, perm)
98 }
99
100 func (h HttpFs) Remove(name string) error {
101 return h.source.Remove(name)
102 }
103
104 func (h HttpFs) RemoveAll(path string) error {
105 return h.source.RemoveAll(path)
106 }
107
108 func (h HttpFs) Rename(oldname, newname string) error {
109 return h.source.Rename(oldname, newname)
110 }
111
112 func (h HttpFs) Stat(name string) (os.FileInfo, error) {
113 return h.source.Stat(name)
114 }
115
View as plain text