-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
127 lines (111 loc) · 2.59 KB
/
http.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package fs
import (
"errors"
"fmt"
vmhttp "github.com/vlorc/lua-vm/net/http"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
type HttpFile struct {
fd io.ReadCloser
url string
length string
modify string
}
type HttpFileInfo struct {
name string
length int64
modify time.Time
}
type HttpFileFactory struct {
root string
driver *vmhttp.HTTPFactory
}
func (f *HttpFileFactory) Open(file string, args ...int) (FileDriver, error) {
resp, err := f.driver.Get(f.root + file)
if nil != err {
return nil, err
}
if 200 != resp.StatusCode {
return nil, fmt.Errorf("Can't open http file code: %d", resp.StatusCode)
}
return &HttpFile{
fd: resp.Body,
url: file,
length: resp.Header.Get("Content-Length"),
modify: resp.Header.Get("Last-Modified"),
}, nil
}
func (f *HttpFileFactory) Remove(file string) error {
resp, err := f.driver.Delete(f.root + file)
if nil == err {
defer resp.Body.Close()
if 200 != resp.StatusCode {
err = fmt.Errorf("Can't open http file code: %d", resp.StatusCode)
}
}
return err
}
func (*HttpFileFactory) Rename(src, dst string) error {
return errors.New("Can't support rename method")
}
func (f *HttpFileFactory) Exist(file string) bool {
resp, err := f.driver.Head(f.root + file)
if nil == err {
defer resp.Body.Close()
if 200 != resp.StatusCode {
err = fmt.Errorf("Can't exist http file code: %d", resp.StatusCode)
}
}
return true
}
func (*HttpFileFactory) Mkdir(string, int) error {
return errors.New("Can't support mkdir method")
}
func (*HttpFileFactory) Walk(root string, callback filepath.WalkFunc) error {
return errors.New("Can't support walk method")
}
func (f *HttpFile) Write(b []byte) (int, error) {
return 0, errors.New("Can't support write method")
}
func (f *HttpFile) Read(b []byte) (int, error) {
return f.fd.Read(b)
}
func (f *HttpFile) Close() error {
return f.fd.Close()
}
func (f *HttpFile) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("Can't support seek method")
}
func (f *HttpFile) Stat() (os.FileInfo, error) {
_, filename := filepath.Split(f.url)
length, _ := strconv.Atoi(f.length)
last, _ := time.Parse(http.TimeFormat, f.modify)
return &HttpFileInfo{
name: filename,
length: int64(length),
modify: last,
}, nil
}
func (f *HttpFileInfo) Name() string {
return f.name
}
func (f *HttpFileInfo) Size() int64 {
return f.length
}
func (f *HttpFileInfo) Mode() os.FileMode {
return 0
}
func (f *HttpFileInfo) ModTime() time.Time {
return f.modify
}
func (f *HttpFileInfo) IsDir() bool {
return false
}
func (f *HttpFileInfo) Sys() interface{} {
return nil
}