-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif2cast.go
71 lines (63 loc) · 1.6 KB
/
gif2cast.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
package gif2cast
import (
"bytes"
"image"
"image/gif"
"io"
"mime/multipart"
"net/http"
"github.com/mkishere/gif2cast/asciicast"
"github.com/mkishere/gif2cast/imgutil"
)
const (
endpoint = "https://asciinema.org/api/asciicasts"
)
// Gif2Cast helps with image conversion(resize) and controlling the virtual screen
type Gif2Cast struct {
oriImg *gif.GIF
cw, ch int
rgbImg []*image.RGBA
title string
}
// NewGif2Cast creates a new Gif2Cast object
func NewGif2Cast(gif *gif.GIF, w, h int, title string) *Gif2Cast {
return &Gif2Cast{
oriImg: gif,
cw: w,
ch: h,
title: title,
}
}
// Write writes file content to writer
func (gc *Gif2Cast) Write(w io.Writer) (err error) {
gc.rgbImg, err = imgutil.Resize(gc.oriImg, gc.cw, gc.ch*2)
if err != nil {
return err
}
vs := asciicast.NewVirtualScreen(gc.cw, gc.ch, gc.title)
for i := range gc.rgbImg {
vs.WriteFrame(gc.rgbImg[i], gc.oriImg.Delay[i])
}
return vs.Write(w)
}
// Upload the written file to asciinema server
func (gc *Gif2Cast) Upload(apiKey string) (string, error) {
buf := &bytes.Buffer{}
writer := multipart.NewWriter(buf)
filePart, _ := writer.CreateFormFile("asciicast", "ascii.cast")
_ = gc.Write(filePart)
writer.Close()
req, _ := http.NewRequest("POST", endpoint, buf)
req.SetBasicAuth("gif2cast", apiKey)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Add("User-Agent", "gif2cast/1.0.0")
client := &http.Client{}
rsp, err := client.Do(req)
if err != nil {
return "", err
}
body := &bytes.Buffer{}
_, err = body.ReadFrom(rsp.Body)
rsp.Body.Close()
return string(body.Bytes()), err
}