-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdemia.go
142 lines (113 loc) · 2.73 KB
/
updemia.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/atotto/clipboard"
"github.com/howeyc/fsnotify"
"github.com/mitchellh/go-homedir"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
)
const defaultDestinationPath string = "/updemia"
const apiPostEndpoint string = "http://www.updemia.com/api/v1/post?userhash="
type UpdemiaResponse struct {
Key string
Url string
}
func watchUploadFolder() {
saveNotificationLogo()
log.Println("watch directory")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
done := make(chan bool)
go func() {
for {
select {
case ev := <-watcher.Event:
if ev.IsCreate() {
log.Println("file is create:")
sendFile(getNewFilePath(ev))
}
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
dest := getDestinationPath()
err = watcher.Watch(dest)
if err != nil {
log.Fatal(err)
}
<-done
watcher.Close()
}
func getDestinationPath() string {
usrDir, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
var destination_path bytes.Buffer
destination_path.WriteString(usrDir)
destination_path.WriteString(defaultDestinationPath)
os.MkdirAll(destination_path.String(), os.ModePerm)
log.Printf("Send to destination_path: %+v", destination_path.String())
return destination_path.String()
}
func newfileUploadRequest(uri string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}
func sendFile(filename string) {
urlStr := fmt.Sprintf("%s%s", apiPostEndpoint, updemiaUser.Hash)
request, err := newfileUploadRequest(urlStr, "file", filename)
if err == nil {
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
notifyUserFail()
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var response UpdemiaResponse
json.Unmarshal(body, &response)
err = clipboard.WriteAll(response.Url)
notifyUserSuccess(response.Url)
log.Printf("New upload: %+v", response.Url)
}
}
func getNewFilePath(ev *fsnotify.FileEvent) string {
strEvent := fmt.Sprintf("%s", ev)
endingIndex := strings.Index(strEvent, "\":")
filename := strEvent[1:endingIndex]
beginningIndex := strings.LastIndex(filename, "/") + 1
if string(filename[beginningIndex]) != "." {
return filename
}
return ""
}