-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadfolder.go
159 lines (131 loc) · 3.12 KB
/
uploadfolder.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
package main
import (
"fmt"
"github.com/atotto/clipboard"
"gopkg.in/fsnotify.v1"
"log"
"os"
"time"
)
type FileReadyChecker struct {
DelayTickChan map[string]*time.Ticker
LastFileDetails map[string]*FileDetails
Debug bool
S3pal *S3pal
}
type FileDetails struct {
Name string
Size int64
Readable bool
}
func getFileDetails(path string) *FileDetails {
f, err := os.Open(path)
result := &FileDetails{
Name: path,
Readable: false,
}
if err == nil {
var stat os.FileInfo
stat, err = f.Stat()
if err == nil {
result.Readable = true
result.Size = stat.Size()
}
f.Close()
}
return result
}
func (o *FileReadyChecker) checkFile(path string) {
if val, ok := o.DelayTickChan[path]; ok {
val.Stop()
}
fwConfig := o.S3pal.Config.FolderWatchUpload
o.LastFileDetails[path] = getFileDetails(path)
//log.Println("Size now:", *o.LastFileDetails[path])
o.DelayTickChan[path] = time.NewTicker(2 * time.Second)
go func() {
for {
select {
case <-o.DelayTickChan[path].C:
now := getFileDetails(path)
if now.Readable && now.Size == o.LastFileDetails[path].Size {
o.DelayTickChan[path].Stop()
newFilename, err := o.S3pal.uploadPathOrURL(path, fwConfig.Prefix)
if err == nil {
if fwConfig.AutoDeleteFile {
fmt.Printf("\nAuto deleting '%v'...", path)
err = os.Remove(path)
if err != nil {
fmt.Printf("Error! Not removed.")
} else {
fmt.Printf("Done.")
}
}
if fwConfig.AutoClipboard {
var toCopy string
if len(fwConfig.AutoClipboardPrefix) > 0 {
toCopy = fwConfig.AutoClipboardPrefix + newFilename
} else {
toCopy = o.S3pal.makeUrl(newFilename)
}
clipboard.WriteAll(toCopy)
fmt.Printf("\nAdded '%v' to your clipboard\n\n", toCopy)
}
} else {
fmt.Printf("\nError uploading '%v'\n\n", path)
}
} else {
o.LastFileDetails[path] = now
}
}
}
}()
}
func (o *FileReadyChecker) startWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Rename != fsnotify.Rename {
o.checkFile(event.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
err = watcher.Add(o.S3pal.Config.FolderWatchUpload.Path)
fmt.Printf("\nLooking for new files in '%v'...\n", o.S3pal.Config.FolderWatchUpload.Path)
if err != nil {
log.Fatal(err)
}
<-done
}
func (s *S3pal) startDropFolder() {
path := s.Config.FolderWatchUpload.Path
if len(path) == 0 {
fmt.Printf("\nNot Running! No Path defined in config or command line.\n\n")
return
}
fileInfo, err := os.Stat(path)
if err != nil {
fmt.Printf("\nNot Running! Could not find folder '%v'\n\n", path)
return
}
if !fileInfo.IsDir() {
fmt.Printf("\nNot Running! '%v' is NOT a folder.\n\n", path)
return
}
o := FileReadyChecker{
DelayTickChan: map[string]*time.Ticker{},
LastFileDetails: map[string]*FileDetails{},
S3pal: s,
}
o.startWatcher()
}