2017-02-20 21:16:44 +10:30
package main
2020-03-20 05:49:12 +10:30
//go:generate go-bindata -pkg assets -o assets/static.go -prefix data/ data
2017-02-20 21:16:44 +10:30
import (
2017-02-22 21:13:07 +10:30
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
2017-02-26 21:06:48 +10:30
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-02-22 21:13:07 +10:30
"github.com/pborman/getopt"
2021-06-02 23:42:29 +09:30
2021-01-31 17:53:32 +10:30
// "github.com/skratchdot/open-golang/open"
2017-07-26 14:24:02 +09:30
2021-01-31 17:53:32 +10:30
"github.com/tardisx/discord-auto-upload/config"
2021-06-03 19:36:48 +09:30
daulog "github.com/tardisx/discord-auto-upload/log"
2021-06-07 21:13:57 +09:30
"github.com/tardisx/discord-auto-upload/uploads"
2021-01-31 18:48:48 +10:30
"github.com/tardisx/discord-auto-upload/web"
2017-02-20 21:16:44 +10:30
)
2021-01-31 18:48:48 +10:30
var lastCheck = time . Now ( )
2017-02-22 21:13:07 +10:30
var newLastCheck = time . Now ( )
2017-02-21 12:24:14 +10:30
2017-02-20 21:16:44 +10:30
func main ( ) {
2017-02-22 21:13:07 +10:30
2021-01-31 17:53:32 +10:30
parseOptions ( )
2017-07-26 14:24:02 +09:30
2021-01-31 17:53:32 +10:30
// log.Print("Opening web browser")
// open.Start("http://localhost:9090")
2021-06-02 23:42:29 +09:30
web . StartWebServer ( )
2017-07-27 22:04:47 +09:30
2017-07-26 22:40:21 +09:30
checkUpdates ( )
2021-06-03 19:36:48 +09:30
daulog . SendLog ( fmt . Sprintf ( "Waiting for images to appear in %s" , config . Config . Path ) , daulog . LogTypeInfo )
2017-02-22 21:13:07 +10:30
// wander the path, forever
for {
2021-02-07 11:42:13 +10:30
if checkPath ( config . Config . Path ) {
err := filepath . Walk ( config . Config . Path ,
func ( path string , f os . FileInfo , err error ) error { return checkFile ( path , f , err ) } )
if err != nil {
log . Fatal ( "could not watch path" , err )
}
lastCheck = newLastCheck
2017-02-22 21:13:07 +10:30
}
2021-06-03 19:36:48 +09:30
daulog . SendLog ( fmt . Sprintf ( "sleeping for %ds before next check of %s" , config . Config . Watch , config . Config . Path ) , daulog . LogTypeDebug )
2021-01-31 17:53:32 +10:30
time . Sleep ( time . Duration ( config . Config . Watch ) * time . Second )
2017-07-27 12:18:02 +09:30
}
}
2021-02-07 11:42:13 +10:30
func checkPath ( path string ) bool {
2017-02-22 21:13:07 +10:30
src , err := os . Stat ( path )
if err != nil {
2021-02-07 22:06:19 +10:30
log . Printf ( "Problem with path '%s': %s" , path , err )
2021-02-07 11:42:13 +10:30
return false
2017-02-22 21:13:07 +10:30
}
if ! src . IsDir ( ) {
2021-02-07 22:06:19 +10:30
log . Printf ( "Problem with path '%s': is not a directory" , 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
2017-02-21 17:10:00 +10:30
}
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
}
2021-06-03 19:36:48 +09:30
daulog . SendLog ( "checking for new version" , daulog . LogTypeInfo )
2021-06-02 23:42:29 +09:30
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 {
2021-06-03 19:36:48 +09:30
daulog . SendLog ( fmt . Sprintf ( "WARNING: Update check failed: %v" , err ) , daulog . LogTypeError )
2017-07-26 22:44:56 +09:30
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 )
}
2021-01-31 17:53:32 +10:30
if config . CurrentVersion < latest . TagName {
fmt . Printf ( "You are currently on version %s, but version %s is available\n" , config . CurrentVersion , latest . TagName )
2017-02-22 21:13:07 +10:30
fmt . Println ( "----------- Release Info -----------" )
fmt . Println ( latest . Body )
fmt . Println ( "------------------------------------" )
2017-02-28 22:50:03 +10:30
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
}
2017-02-21 11:15:12 +10:30
}
2021-01-31 17:53:32 +10:30
func parseOptions ( ) {
2017-02-20 21:16:44 +10:30
2017-02-22 21:13:07 +10:30
// Declare the flags to be used
helpFlag := getopt . BoolLong ( "help" , 'h' , "help" )
versionFlag := getopt . BoolLong ( "version" , 'v' , "show version" )
getopt . SetParameters ( "" )
2017-02-20 21:16:44 +10:30
2017-02-22 21:13:07 +10:30
getopt . Parse ( )
2017-02-20 21:16:44 +10:30
2017-02-22 21:13:07 +10:30
if * helpFlag {
getopt . PrintUsage ( os . Stderr )
os . Exit ( 0 )
}
2017-02-21 12:28:26 +10:30
2017-02-22 21:13:07 +10:30
if * versionFlag {
fmt . Println ( "dau - https://github.com/tardisx/discord-auto-upload" )
2021-01-31 17:53:32 +10:30
fmt . Printf ( "Version: %s\n" , config . CurrentVersion )
2017-02-22 21:13:07 +10:30
os . Exit ( 0 )
}
2017-02-21 12:24:14 +10:30
2021-02-07 11:42:13 +10:30
// grab the config
config . LoadOrInit ( )
2017-02-20 21:16:44 +10:30
}
2021-01-31 17:53:32 +10:30
func checkFile ( path string , f os . FileInfo , err error ) error {
2017-02-22 21:13:07 +10:30
if f . ModTime ( ) . After ( lastCheck ) && f . Mode ( ) . IsRegular ( ) {
2017-02-20 21:16:44 +10:30
2021-01-31 17:53:32 +10:30
if fileEligible ( path ) {
2017-02-22 21:13:07 +10:30
// process file
2021-01-31 17:53:32 +10:30
processFile ( path )
2017-02-22 21:13:07 +10:30
}
2017-02-20 21:16:44 +10:30
2017-02-22 21:13:07 +10:30
if newLastCheck . Before ( f . ModTime ( ) ) {
newLastCheck = f . ModTime ( )
}
}
2017-02-20 21:16:44 +10:30
2017-02-22 21:13:07 +10:30
return nil
2017-02-20 21:16:44 +10:30
}
2021-01-31 17:53:32 +10:30
func fileEligible ( file string ) bool {
2017-02-28 22:50:03 +10:30
2021-01-31 17:53:32 +10:30
if config . Config . Exclude != "" && strings . Contains ( file , config . Config . Exclude ) {
2017-02-28 22:50:03 +10:30
return false
}
2017-02-22 21:13:07 +10:30
extension := strings . ToLower ( filepath . Ext ( file ) )
if extension == ".png" || extension == ".jpg" || extension == ".gif" {
return true
}
2017-02-28 22:50:03 +10:30
2017-02-22 21:13:07 +10:30
return false
2017-02-20 21:16:44 +10:30
}
2021-01-31 17:53:32 +10:30
func processFile ( file string ) {
2017-02-26 21:06:48 +10:30
2021-06-07 21:13:57 +09:30
daulog . SendLog ( "Sending to uploader" , daulog . LogTypeInfo )
uploads . AddFile ( file )
2017-02-26 21:06:48 +10:30
}