This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
238 lines (185 loc) · 5.02 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
//go:generate goversioninfo -icon=streamable.ico -manifest=sts.exe.manifest
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"syscall"
"fyne.io/fyne/app"
"fyne.io/fyne/dialog"
"fyne.io/fyne/widget"
"golang.org/x/crypto/ssh/terminal"
)
var httpClient = &http.Client{}
type conf struct {
Email string `json:"email"`
Password string `json:"password"`
}
func main() {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("Error getting user home directory: %s", err)
os.Exit(1)
}
confPath := fmt.Sprintf("%s\\.stsconf", homeDir)
conf := &conf{}
if _, err := os.Stat(confPath); err == nil {
b, _ := ioutil.ReadFile(confPath)
json.Unmarshal(b, conf)
}
app := app.New()
w := app.NewWindow("STS")
w.SetFixedSize(true)
if len(os.Args) > 1 {
uploaded := false
filename := widget.NewEntry()
filename.SetText(os.Args[1])
filename.Disable()
title := widget.NewEntry()
videoURL := widget.NewEntry()
videoURL.Disable()
form := &widget.Form{
OnSubmit: func() {
if len(conf.Email) == 0 || len(conf.Password) == 0 {
dialog.NewInformation("", "Missing Streamable credentials.", w).Show()
return
}
if uploaded {
dialog.NewInformation("", "Video has already been uploaded.", w).Show()
return
}
url, err := uploadVideo(httpClient, conf.Email, conf.Password, filename.Text, title.Text)
if err != nil {
dialog.NewInformation("", "Error uploading video", w).Show()
return
}
uploaded = true
videoURL.SetText(url)
videoURL.Enable()
},
}
form.Append("Filename", filename)
form.Append("Title", title)
form.Append("Video URL", videoURL)
w.Canvas().Focus(title)
w.SetContent(widget.NewVBox(
form,
))
w.ShowAndRun()
return
}
email := widget.NewEntry()
email.SetPlaceHolder("[email protected]")
password := widget.NewPasswordEntry()
password.SetPlaceHolder("Password")
form := &widget.Form{
OnSubmit: func() {
if len(email.Text) == 0 || len(password.Text) == 0 {
return
}
err = checkAuth(httpClient, email.Text, password.Text)
if err != nil {
dialog.NewInformation("", "Invalid credentials.", w).Show()
return
}
conf.Email = email.Text
conf.Password = password.Text
b, err := json.Marshal(conf)
if err != nil {
dialog.NewInformation("", "Error marshaling JSON", w).Show()
return
}
err = ioutil.WriteFile(confPath, b, 0600)
if err != nil {
dialog.NewInformation("", "Error writing configuration file", w).Show()
return
}
dialog.NewInformation("", "STS is now configured.", w).Show()
},
}
form.Append("Email", email)
form.Append("Password", password)
w.Canvas().Focus(email)
w.SetContent(widget.NewVBox(
widget.NewLabel("Enter your Streamable email and password and click Submit."),
form,
))
w.ShowAndRun()
}
func userInput(label string, password bool) string {
var out string
fmt.Printf("%s: ", label)
if password {
password, _ := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println("")
out = string(password)
} else {
reader := bufio.NewReader(os.Stdin)
out, _ = reader.ReadString('\n')
}
return strings.TrimSpace(out)
}
func checkAuth(httpClient *http.Client, username, password string) error {
req, err := http.NewRequest("POST", "https://api.streamable.com/upload", nil)
if err != nil {
return err
}
req.Header.Add("User-Agent", "send-to-streamable/1.0")
req.SetBasicAuth(username, password)
res, err := httpClient.Do(req)
if err != nil {
return err
}
if res.StatusCode == 401 {
return errors.New("Invalid credentials")
}
return nil
}
func uploadVideo(httpClient *http.Client, username, password, filename, title string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(file.Name()))
if err != nil {
return "", err
}
io.Copy(part, file)
writer.Close()
req, err := http.NewRequest("POST", fmt.Sprintf("https://api.streamable.com/upload?title=%s", url.QueryEscape(title)), body)
if err != nil {
return "", err
}
req.Header.Add("User-Agent", "send-to-stremable/1.0")
req.SetBasicAuth(username, password)
req.Header.Add("Content-Type", writer.FormDataContentType())
res, err := httpClient.Do(req)
if err != nil {
return "", err
}
if res.StatusCode != 200 {
return "", errors.New("Streamable responded with an error")
}
b, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
type uploadRes struct {
Shortcode string `json:"shortcode"`
Status int `json:"status"`
}
uRes := &uploadRes{}
json.Unmarshal(b, &uRes)
return fmt.Sprintf("https://streamable.com/%s", uRes.Shortcode), nil
}