-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
82 lines (76 loc) · 1.47 KB
/
fs.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
package httpfile
import (
"fmt"
"io"
"net/http"
"os"
)
type FS struct {
Client *http.Client
}
func (fs *FS) Delete(urlStr string) (err error) {
req, err := http.NewRequest("DELETE", urlStr, nil)
if err != nil {
return
}
resp, err := fs.Client.Do(req)
if err != nil {
return
}
resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
err = ErrNotFound
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("response: %s", resp.Status)
}
return
}
func (fs *FS) GetLength(url string) (ret int64, err error) {
resp, err := fs.Client.Head(url)
if err != nil {
return
}
resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
err = ErrNotFound
return
}
return instanceLength(resp)
}
func (fs *FS) OpenSectionReader(url string, off, n int64) (ret io.ReadCloser, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", off, off+n-1))
resp, err := fs.Client.Do(req)
if err != nil {
return
}
if resp.StatusCode == http.StatusNotFound {
err = ErrNotFound
resp.Body.Close()
return
}
if resp.StatusCode != http.StatusPartialContent {
err = fmt.Errorf("bad response status: %s", resp.Status)
resp.Body.Close()
return
}
ret = resp.Body
return
}
func (fs *FS) Open(url string, flags int) (ret *File, err error) {
ret = &File{
url: url,
flags: flags,
length: -1,
fs: fs,
}
if flags&os.O_CREATE == 0 {
err = ret.headLength()
}
return
}