212 lines
5.3 KiB
Go
Raw Normal View History

package main
import (
2017-02-22 21:13:07 +10:30
"encoding/json"
"flag"
2017-02-22 21:13:07 +10:30
"fmt"
"io/fs"
2017-02-22 21:13:07 +10:30
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
// "github.com/skratchdot/open-golang/open"
"github.com/tardisx/discord-auto-upload/config"
daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/tardisx/discord-auto-upload/upload"
// "github.com/tardisx/discord-auto-upload/upload"
"github.com/tardisx/discord-auto-upload/version"
2021-01-31 18:48:48 +10:30
"github.com/tardisx/discord-auto-upload/web"
)
type watch struct {
lastCheck time.Time
newLastCheck time.Time
config config.Watcher
uploader upload.Uploader
}
func main() {
2017-02-22 21:13:07 +10:30
parseOptions()
// grab the config
config := config.DefaultConfigService()
config.LoadOrInit()
// create the uploader
up := upload.Uploader{}
// log.Print("Opening web browser")
// open.Start("http://localhost:9090")
web := web.WebService{Config: *config}
web.StartWebServer()
go func() { checkUpdates() }()
// create the watchers
2017-07-26 22:40:21 +09:30
log.Printf("Conf: %#v", config.Config)
for _, c := range config.Config.Watchers {
log.Printf("Creating watcher for %v", c)
watcher := watch{uploader: up, lastCheck: time.Now(), newLastCheck: time.Now(), config: c}
go watcher.Watch(config.Config.WatchInterval)
}
select {}
}
func (w *watch) Watch(interval int) {
2017-02-22 21:13:07 +10:30
for {
newFiles := w.ProcessNewFiles()
for _, f := range newFiles {
w.uploader.AddFile(f, w.config)
2017-02-22 21:13:07 +10:30
}
// upload them
w.uploader.Upload()
daulog.SendLog(fmt.Sprintf("sleeping for %ds before next check of %s", interval, w.config.Path), daulog.LogTypeDebug)
time.Sleep(time.Duration(interval) * time.Second)
2017-07-27 12:18:02 +09:30
}
}
// ProcessNewFiles returns an array of new files that have appeared since
// the last time ProcessNewFiles was run.
func (w *watch) ProcessNewFiles() []string {
var newFiles []string
// check the path each time around, in case it goes away or something
if w.checkPath() {
// walk the path
err := filepath.WalkDir(w.config.Path,
func(path string, d fs.DirEntry, err error) error {
2021-10-10 12:00:25 +10:30
return w.checkFile(path, &newFiles, w.config.Exclude)
})
if err != nil {
log.Fatal("could not watch path", err)
}
w.lastCheck = w.newLastCheck
}
return newFiles
}
// checkPath makes sure the path exists, and is a directory.
// It logs errors if there are problems, and returns false
func (w *watch) checkPath() bool {
src, err := os.Stat(w.config.Path)
2017-02-22 21:13:07 +10:30
if err != nil {
log.Printf("Problem with path '%s': %s", w.config.Path, err)
2021-02-07 11:42:13 +10:30
return false
2017-02-22 21:13:07 +10:30
}
if !src.IsDir() {
log.Printf("Problem with path '%s': is not a directory", w.config.Path)
2021-02-07 11:42:13 +10:30
return false
2017-02-22 21:13:07 +10:30
}
2021-02-07 11:42:13 +10:30
return true
}
// checkFile checks if a file is eligible, first looking at extension (to
// avoid statting files uselessly) then modification times.
2021-10-10 12:00:25 +10:30
// If the file is eligible, not excluded and new enough to care we add it
// to the passed in array of files
func (w *watch) checkFile(path string, found *[]string, exclusions []string) error {
log.Printf("Considering %s", path)
extension := strings.ToLower(filepath.Ext(path))
if !(extension == ".png" || extension == ".jpg" || extension == ".gif") {
return nil
}
fi, err := os.Stat(path)
if err != nil {
return err
}
if fi.ModTime().After(w.lastCheck) && fi.Mode().IsRegular() {
2021-10-10 12:00:25 +10:30
excluded := false
for _, exclusion := range exclusions {
if strings.Contains(path, exclusion) {
excluded = true
}
}
if !excluded {
*found = append(*found, path)
}
}
if w.newLastCheck.Before(fi.ModTime()) {
w.newLastCheck = fi.ModTime()
}
return nil
}
2017-02-22 21:13:07 +10:30
func checkUpdates() {
type GithubRelease struct {
2021-06-04 10:41:23 +09:30
HTMLURL string `json:"html_url"`
TagName string `json:"tag_name"`
Name string `json:"name"`
Body string `json:"body"`
2017-02-22 21:13:07 +10:30
}
daulog.SendLog("checking for new version", daulog.LogTypeInfo)
2017-02-22 21:13:07 +10:30
client := &http.Client{Timeout: time.Second * 5}
resp, err := client.Get("https://api.github.com/repos/tardisx/discord-auto-upload/releases/latest")
if err != nil {
daulog.SendLog(fmt.Sprintf("WARNING: Update check failed: %v", err), daulog.LogTypeError)
return
2017-02-22 21:13:07 +10:30
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("could not check read update response")
}
var latest GithubRelease
err = json.Unmarshal(body, &latest)
if err != nil {
log.Fatal("could not parse JSON: ", err)
}
// pre v0.11.0 version (ie before semver) did a simple string comparison,
// but since "0.10.0" < "v0.11.0" they should still get prompted to upgrade
// ok
if version.NewVersionAvailable(latest.TagName) {
fmt.Printf("You are currently on version %s, but version %s is available\n", version.CurrentVersion, latest.TagName)
2017-02-22 21:13:07 +10:30
fmt.Println("----------- Release Info -----------")
fmt.Println(latest.Body)
fmt.Println("------------------------------------")
fmt.Println("Upgrade at https://github.com/tardisx/discord-auto-upload/releases/latest")
2021-06-04 10:41:23 +09:30
daulog.SendLog(fmt.Sprintf("New version available: %s - download at https://github.com/tardisx/discord-auto-upload/releases/latest", latest.TagName), daulog.LogTypeInfo)
2017-02-22 21:13:07 +10:30
}
2021-06-17 18:47:59 +09:30
daulog.SendLog("already running latest version", daulog.LogTypeInfo)
}
func parseOptions() {
var versionFlag bool
flag.BoolVar(&versionFlag, "version", false, "show version")
flag.Parse()
if versionFlag {
2017-02-22 21:13:07 +10:30
fmt.Println("dau - https://github.com/tardisx/discord-auto-upload")
fmt.Printf("Version: %s\n", version.CurrentVersion)
2017-02-22 21:13:07 +10:30
os.Exit(0)
}
}