-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (134 loc) · 3.2 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
package main
import (
"flag"
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"github.com/atotto/clipboard"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/fsnotify/fsnotify"
"github.com/joho/godotenv"
"github.com/martinlindhe/notify"
)
const AppVersion = "1.0.6"
var (
s3Bucket = ""
s3Host = ""
awsRegion = ""
awsProfile = ""
awsConfigFile = ""
watchDirectory = ""
s3Uploader = &s3manager.Uploader{}
)
func initConfig() {
user, _ := user.Current()
directory := user.HomeDir
err := godotenv.Load(directory + "/.config/screenshot-uploader")
if err != nil {
panic("Error loading ~/.config/screenshot-uploader file")
}
s3Bucket = os.Getenv("S3_BUCKET")
s3Host = os.Getenv("S3_HOST")
awsRegion = os.Getenv("AWS_REGION")
awsProfile = os.Getenv("AWS_PROFILE")
awsConfigFile = os.Getenv("AWS_CONFIG_FILE")
watchDirectory = os.Getenv("SCREENSHOT_LOCATION")
fmt.Printf("Watching for changes in %s\n", watchDirectory)
creds := credentials.NewSharedCredentials(awsConfigFile, awsProfile)
config := aws.Config{
Region: aws.String(awsRegion),
Credentials: creds,
}
session := session.New(&config)
s3Uploader = s3manager.NewUploader(session)
}
func uploadScreenshot(filePath string) {
ext := filepath.Ext(filePath)
if ext != ".png" && ext != ".jpg" && ext != ".mov" {
return
}
mimeType := ""
switch ext {
case ".png":
mimeType = "image/png"
case ".jpg":
mimeType = "image/jpeg"
case ".mov":
mimeType = "video/quicktime"
}
s3Key := RandomString(5) + ext
fmt.Println("s3 key: " + s3Key)
file, _ := os.Open(filePath)
fmt.Println("Uploading file to S3...")
result, err := s3Uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s3Bucket),
Key: aws.String(s3Key),
Body: file,
ACL: aws.String("public-read"),
ContentType: aws.String(mimeType),
})
file.Close()
if err != nil {
fmt.Println("s3 upload error", err)
return
}
fmt.Printf("Successfully uploaded %s to %s\n", filePath, result.Location)
url := result.Location
if s3Host != "" {
url = s3Host + "/" + s3Key
}
sendNotification(url)
}
func sendNotification(url string) {
clipboard.WriteAll(url)
notify.Notify("Screenshot Uploaded!", "", "Linked copied to clipboard", "")
}
func watchForChanges() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer watcher.Close()
if err := watcher.Add(watchDirectory); err != nil {
fmt.Println(err)
os.Exit(1)
}
for {
select {
case event := <-watcher.Events:
// fmt.Printf("event: %v\n", event)
switch event.Op {
case fsnotify.Write, fsnotify.Create:
if path.Base(event.Name)[:1] == "." {
continue
}
if path.Base(event.Name)[:1] == ".." {
continue
}
uploadScreenshot(event.Name)
}
case err := <-watcher.Errors:
// if err == nil {
// panic("unexpected nil err")
// }
// return
panic(err)
}
}
}
func main() {
version := flag.Bool("v", false, "prints current version")
flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
initConfig()
watchForChanges()
}