-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
84 lines (65 loc) · 1.82 KB
/
file.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
package api
import (
"io"
"os"
"path"
"fmt"
"encoding/json"
"github.com/h2object/rpc"
"github.com/h2object/content-type"
)
func (h2o *Client) Delete(l Logger, auth Auth, dest_uri string) error {
URL := rpc.BuildHttpURL(h2o.addr, dest_uri, nil)
h2o.Lock()
defer h2o.Unlock()
h2o.conn.Prepare(auth)
defer h2o.conn.Prepare(nil)
return h2o.conn.Delete(l, URL, nil)
}
func (h2o *Client) Download(l Logger, auth Auth, dest_uri string) (io.ReadCloser, int64, error) {
URL := rpc.BuildHttpURL(h2o.addr, dest_uri, nil)
h2o.Lock()
defer h2o.Unlock()
h2o.conn.Prepare(auth)
defer h2o.conn.Prepare(nil)
resp, err := h2o.conn.GetResponse(l, URL)
if err != nil {
return nil, 0, err
}
if resp.StatusCode/100 != 2 {
if resp.ContentLength != 0 {
var ret1 rpc.ErrorRet
if err := json.NewDecoder(resp.Body).Decode(&ret1); err != nil {
return nil, 0, err
}
return nil, 0, fmt.Errorf("code: %d, reason: %s", resp.StatusCode, ret1.Error)
}
return nil, 0, fmt.Errorf("code: %d, reason: failed", resp.StatusCode)
}
return resp.Body, resp.ContentLength, nil
}
func (h2o *Client) UploadFile(l Logger, auth Auth, dest_uri string, file string) error {
fd, err := os.Open(file)
if err != nil {
return err
}
defer fd.Close()
st, err := fd.Stat()
if err != nil {
return err
}
_, filename := path.Split(file)
contentType := content_type.DefaultContentTypeHelper.ContentTypeByFilename(filename)
return h2o.Upload(l, auth, dest_uri, contentType, fd, st.Size())
}
func (h2o *Client) Upload(l Logger, auth Auth, dest_uri string, contentType string, rd io.Reader, sz int64) error {
URL := rpc.BuildHttpURL(h2o.addr, dest_uri, nil)
h2o.Lock()
defer h2o.Unlock()
h2o.conn.Prepare(auth)
defer h2o.conn.Prepare(nil)
if err := h2o.conn.Put(l, URL, contentType, rd, sz, nil); err != nil {
return err
}
return nil
}