-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (58 loc) · 1.77 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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/gen2brain/beeep"
"golang.design/x/clipboard"
)
var (
watchPath string
r2BucketDomain string
r2BucketName string
r2AccountId string
r2AccessKeyId string
r2AccessKeySecret string
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
watchPath = strings.TrimSpace(os.Getenv("CLOUDSHARE_WATCH_PATH"))
r2BucketDomain = strings.TrimSpace(os.Getenv("R2_BUCKET_DOMAIN"))
r2BucketName = strings.TrimSpace(os.Getenv("R2_BUCKET_NAME"))
r2AccountId = strings.TrimSpace(os.Getenv("R2_ACCOUNT_ID"))
r2AccessKeyId = strings.TrimSpace(os.Getenv("R2_ACCESS_KEY_ID"))
r2AccessKeySecret = strings.TrimSpace(os.Getenv("R2_ACCESS_KEY_SECRET"))
if r2BucketDomain == "" || r2BucketName == "" || r2AccountId == "" ||
r2AccessKeyId == "" || r2AccessKeySecret == "" || watchPath == "" {
log.Fatalf("Missing required environment variables\n")
}
clipboard.Init()
}
func main() {
storageClient := NewStorageClient()
watcher := NewWatcher(watchPath)
defer watcher.Close()
// On file create handler
onCreate := func(filepath string) error {
url, err := storageClient.UploadFile(filepath)
if err != nil {
return fmt.Errorf("failed to upload file '%s': %v", filepath, err)
}
log.Printf("Uploaded file '%s' to '%s'\n", filepath, url)
clipboard.Write(clipboard.FmtText, []byte(url))
if err := beeep.Notify("Link copied to clipboard", url, ""); err != nil {
return fmt.Errorf("failed to send notification %v", err)
}
return nil
}
// Start watch loop
go watchLoop(watcher, onCreate)
log.Printf("Started watching directory '%s'\n", watchPath)
// Start web server for managing uploads
server := Server{
Port: "80",
StorageClient: storageClient,
}
server.Start()
}