diff --git a/cmd/gowo/cmd/upload.go b/cmd/gowo/cmd/upload.go index 7e04f30..11f747d 100644 --- a/cmd/gowo/cmd/upload.go +++ b/cmd/gowo/cmd/upload.go @@ -39,7 +39,7 @@ import ( // uploadCmd represents the upload command var uploadCmd = &cobra.Command{ Use: "upload", - Aliases: []string{"up"}, + Aliases: []string{"up", "whats"}, Short: "Upload files to OwO", Run: func(cmd *cobra.Command, args []string) { cdn := viper.GetString("cdn") diff --git a/cmd/gowo/cmd/watch.go b/cmd/gowo/cmd/watch.go new file mode 100644 index 0000000..0490a63 --- /dev/null +++ b/cmd/gowo/cmd/watch.go @@ -0,0 +1,135 @@ +// MIT License + +// Copyright (c) 2016 @zet4 / @Zeta#2229 / + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +package cmd + +import ( + "bytes" + "context" + "fmt" + "log" + "time" + + "github.com/atotto/clipboard" + "github.com/spf13/cobra" + "github.com/spf13/viper" + owo "github.com/whats-this/owo.go" + + "sync" + + "github.com/fsnotify/fsnotify" +) + +var queue = make(map[string]struct{}) +var queueLock = sync.Mutex{} + +// watchCmd represents the upload command +var watchCmd = &cobra.Command{ + Use: "watch", + Aliases: []string{"w", "this"}, + Short: "Watches directory and uploads new files to OwO", + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + log.Fatal("Need at least one location.") + } + + cdn := viper.GetString("cdn") + + w, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + + for _, arg := range args { + err = w.Add(arg) + if err != nil { + log.Fatal(err) + } + } + + go func() { + ticker := time.NewTicker(time.Second) + for range ticker.C { + var names []string + queueLock.Lock() + for name := range queue { + names = append(names, name) + delete(queue, name) + } + queueLock.Unlock() + + if len(names) == 0 { + continue + } + + files, err := owo.FilesToNamedReaders(names) + if err != nil { + log.Println("[upload]", err) + return + } + response, err := owo.UploadFiles(context.Background(), files) + if err != nil { + log.Println("[upload]", err) + return + } + if !response.Success { + log.Printf("[upload] %d: %s", response.Errorcode, response.Description) + return + } + buf := bytes.Buffer{} + for _, file := range response.Files { + if file.Error { + log.Printf("%d: %s", file.Errorcode, file.Description) + continue + } + fmt.Fprintf(&buf, "%s\n", file.WithCDN(cdn)) + } + if shouldClipboard { + err = clipboard.WriteAll(buf.String()) + if err != nil { + log.Println("[upload]", err) + return + } + log.Printf("Wrote %d URLs to clipboard", len(response.Files)) + } else { + fmt.Print(buf.String()) + } + } + }() + + log.Print("Started watching for events in given destinations.") + for ev := range w.Events { + if ev.Op&fsnotify.Create == fsnotify.Create { + name := ev.Name + go func() { + time.Sleep(2 * time.Second) + queueLock.Lock() + queue[name] = struct{}{} + queueLock.Unlock() + }() + } + } + }, +} + +func init() { + uploadCmd.AddCommand(watchCmd) +}