-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmd_upload.go
47 lines (43 loc) · 1010 Bytes
/
cmd_upload.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
package cmd
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
"time"
)
const (
Upload_API = "https://asciinema.org/api/asciicasts"
)
func (r *Runner) Upload() (resp string, err error) {
file, err := os.Open(r.FilePath)
if err != nil {
return "", err
}
defer file.Close()
buf := &bytes.Buffer{}
writer := multipart.NewWriter(buf)
filePart, err := writer.CreateFormFile("asciicast", "ascii.cast")
_, err = io.Copy(filePart, file)
writer.Close()
req, _ := http.NewRequest("POST", Upload_API, buf)
req.SetBasicAuth("goAsciinema", cfg.ApiToken())
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Add("User-Agent", "goAsciinema/1.0.0")
client := &http.Client{
Timeout: time.Second * 600,
}
rsp, err := client.Do(req)
if err != nil {
return "", err
}
body := &bytes.Buffer{}
_, err = body.ReadFrom(rsp.Body)
if err != nil {
return "", err
}
rsp.Body.Close()
resp = body.String()
return resp, err
}