-
Notifications
You must be signed in to change notification settings - Fork 2
/
piping_duplex.go
42 lines (40 loc) · 1.15 KB
/
piping_duplex.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
package piping_duplex
import (
"github.com/nwtgck/go-piping-duplex/util"
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
)
func DuplexReader(server string, uploadPath string, downloadPath string, r io.Reader) (io.Reader, <-chan error, error) {
uploadFinishErrCh := make(chan error)
postUrl, err := util.UrlJoin(server, uploadPath)
if err != nil {
return nil, uploadFinishErrCh, err
}
// TODO: hard code
contentType := "application/octet-stream"
postRes, err := http.Post(postUrl, contentType, r)
if err != nil {
return nil, uploadFinishErrCh, err
}
if postRes.StatusCode != 200 {
return nil, uploadFinishErrCh, errors.Errorf("GET is not 200 status: %d", postRes.StatusCode)
}
go func() {
_, err := io.Copy(ioutil.Discard, postRes.Body)
uploadFinishErrCh <- err
}()
getUrl, err := util.UrlJoin(server, downloadPath)
if err != nil {
return nil, uploadFinishErrCh, err
}
getRes, err := http.Get(getUrl)
if err != nil {
return nil, uploadFinishErrCh, err
}
if getRes.StatusCode != 200 {
return nil, uploadFinishErrCh, errors.Errorf("POST is not 200 status: %d", postRes.StatusCode)
}
return getRes.Body, uploadFinishErrCh, nil
}