-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
241 lines (205 loc) · 5.1 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
239
240
241
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strings"
"github.com/0xAX/notificator"
"github.com/atotto/clipboard"
"github.com/fsnotify/fsnotify"
"github.com/lithammer/shortuuid/v3"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
var notify *notificator.Notificator
var watcher *fsnotify.Watcher
var screensPath string
var remoteHost string
var remoteUser string
var sshKeyPath string
var remotePath string
var baseURL string
func main() {
var err error
flags()
// creates a new file watcher
watcher, err = fsnotify.NewWatcher()
if err != nil {
panic(err)
}
defer watcher.Close()
exit := make(chan bool)
go watch()
if err := watcher.Add(screensPath); err != nil {
panic(err)
}
<-exit
}
// flags parses flags
func flags() {
flag.StringVar(&screensPath, "p", "", "Path to where screenshots are saved locally")
flag.StringVar(&remoteHost, "r", "", "Remote host, e.g. example.com:2003 or 43.56.122.31:22")
flag.StringVar(&remoteUser, "ru", "", "Username on remote host")
flag.StringVar(&sshKeyPath, "pk", "", "Private key path")
flag.StringVar(&remotePath, "rp", "", "Path on the remote host")
flag.StringVar(&baseURL, "url", "", "A base URL that points to given screenshot, e.g https://i.slacki.io/")
flag.Parse()
screensPath = strings.TrimRight(screensPath, "/") + "/"
remotePath = strings.TrimRight(remotePath, "/") + "/"
baseURL = strings.TrimRight(baseURL, "/") + "/"
}
func watch() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
upload()
}
if event.Op&fsnotify.Create == fsnotify.Create {
upload()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}
func upload() {
fileExtRegexp, _ := regexp.Compile(".*?\\.(\\w+)$")
fi, err := ioutil.ReadDir(screensPath)
if err != nil {
log.Fatal(err)
}
for _, f := range fi {
fmt.Println(f.Name())
if f.IsDir() {
continue
}
fullPath := screensPath + f.Name()
matches := fileExtRegexp.FindAllStringSubmatch(f.Name(), -1)
if len(matches) > 0 && len(matches[0]) > 1 {
ext := matches[0][1]
if !allowedExtension(ext) {
continue
}
if ext == "mov" {
log.Println("Detected .mov file, converting to mp4")
result := ffmpegTranscode(fullPath, screensPath+"out.mp4")
if result {
// remove the .mov file if successfully transcoded
// next pass will upload the file
os.Remove(fullPath)
continue
}
}
remoteFilename := fmt.Sprintf("%s.%s", shortuuid.New(), ext)
err = uploadObjectToDestination(fullPath, remoteFilename)
if err != nil {
log.Println(err)
continue
}
url := baseURL + remoteFilename
copyToClipboard(url)
showNotification(url)
os.Remove(fullPath)
}
}
}
// showNotification displays a system notification about uploaded screenshot
func showNotification(url string) {
notify = notificator.New(notificator.Options{
AppName: "Skrins",
})
notify.Push("Screenshot uploaded!", url, "", notificator.UR_NORMAL)
}
// copyToClipboard puts a string to clipboards
func copyToClipboard(s string) {
clipboard.WriteAll(s)
}
// allowedExtension determines whether it is allowed to upload a file with that extension
func allowedExtension(ext string) bool {
allowed := []string{"jpg", "jpeg", "png", "gif", "webm", "mp4", "mov", "zip", "tar", "tar.gz", "tar.bz2"}
for _, e := range allowed {
if ext == e {
return true
}
}
return false
}
// ffmpegTranscode transcodes a media file.
func ffmpegTranscode(fileIn, fileOut string) bool {
cmd := exec.Command("/usr/local/bin/ffmpeg", "-i", fileIn, fileOut)
var stderr bytes.Buffer
var stdout bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
log.Println(err)
return false
}
log.Println("[ffmpeg stderr]", stderr.String())
log.Println("[ffmpeg stdout]", stdout.String())
return true
}
// newSFTPClient creates new sFTP client
func newSFTPClient() (*sftp.Client, error) {
key, err := ioutil.ReadFile(sshKeyPath)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: remoteUser,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err := ssh.Dial("tcp", remoteHost, config)
if err != nil {
return nil, err
}
return sftp.NewClient(client)
}
// uploadObjectToDestination uploads file to a remote host
func uploadObjectToDestination(src, dest string) error {
client, err := newSFTPClient()
if err != nil {
return err
}
defer client.Close()
// create destination file
// remotePath is expected to have a trailing slash
dstFile, err := client.OpenFile(remotePath+dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
if err != nil {
return err
}
defer dstFile.Close()
// open local file
srcReader, err := os.Open(src)
if err != nil {
return err
}
// copy source file to destination file
bytes, err := io.Copy(dstFile, srcReader)
if err != nil {
return err
}
log.Printf("Total of %d bytes copied\n", bytes)
return nil
}