This commit is contained in:
Justin Hawkins 2021-01-31 18:48:48 +10:30
parent 46a0f5a187
commit e1f5afa788
2 changed files with 42 additions and 45 deletions

5
dau.go
View File

@ -27,14 +27,13 @@ import (
// "github.com/skratchdot/open-golang/open" // "github.com/skratchdot/open-golang/open"
"golang.org/x/image/font/inconsolata" "golang.org/x/image/font/inconsolata"
"github.com/tardisx/discord-auto-upload/web"
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
"github.com/tardisx/discord-auto-upload/web"
) )
var lastCheck = time.Now() var lastCheck = time.Now()
var newLastCheck = time.Now() var newLastCheck = time.Now()
func main() { func main() {
parseOptions() parseOptions()
@ -55,7 +54,7 @@ func main() {
log.Fatal("could not watch path", err) log.Fatal("could not watch path", err)
} }
lastCheck = newLastCheck lastCheck = newLastCheck
log.Print("sleeping before next check"); log.Print("sleeping before next check")
time.Sleep(time.Duration(config.Config.Watch) * time.Second) time.Sleep(time.Duration(config.Config.Watch) * time.Second)
} }
} }

View File

@ -3,17 +3,16 @@ package web
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/tardisx/discord-auto-upload/assets"
"log" "log"
"net/http" "net/http"
"github.com/tardisx/discord-auto-upload/assets" // "strings"
// "strings"
"regexp"
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
"mime"
"os" "os"
"path/filepath" "path/filepath"
"mime" "regexp"
"text/template" "text/template"
) )
// DAUWebServer - stuff for the web server // DAUWebServer - stuff for the web server
@ -39,7 +38,7 @@ func getStatic(w http.ResponseWriter, r *http.Request) {
sanitized_path := re.ReplaceAll([]byte(path), []byte("_")) sanitized_path := re.ReplaceAll([]byte(path), []byte("_"))
if string(sanitized_path) == "" { if string(sanitized_path) == "" {
sanitized_path = []byte("index.html"); sanitized_path = []byte("index.html")
} }
data, err := assets.Asset(string(sanitized_path)) data, err := assets.Asset(string(sanitized_path))
@ -48,7 +47,6 @@ func getStatic(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, err) fmt.Fprintln(w, err)
} }
extension := filepath.Ext(string(sanitized_path)) extension := filepath.Ext(string(sanitized_path))
// is this a HTML file? if so wrap it in the template // is this a HTML file? if so wrap it in the template
@ -71,7 +69,7 @@ func getStatic(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", mime.TypeByExtension(extension)) w.Header().Set("Content-Type", mime.TypeByExtension(extension))
w.Write(data) w.Write(data)
// //
} }
// TODO there should be locks around all these config accesses // TODO there should be locks around all these config accesses
@ -79,7 +77,7 @@ func getSetWebhook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if r.Method == "GET" { if r.Method == "GET" {
getResponse := valueStringResponse{ Success: true, Value: config.Config.WebHookURL } getResponse := valueStringResponse{Success: true, Value: config.Config.WebHookURL}
// I can't see any way this will fail // I can't see any way this will fail
js, _ := json.Marshal(getResponse) js, _ := json.Marshal(getResponse)
@ -90,7 +88,7 @@ func getSetWebhook(w http.ResponseWriter, r *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
config.Config.WebHookURL = r.PostForm.Get("value") config.Config.WebHookURL = r.PostForm.Get("value")
postResponse := valueStringResponse{ Success: true, Value: config.Config.WebHookURL } postResponse := valueStringResponse{Success: true, Value: config.Config.WebHookURL}
js, _ := json.Marshal(postResponse) js, _ := json.Marshal(postResponse)
w.Write(js) w.Write(js)
@ -102,7 +100,7 @@ func getSetDirectory(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if r.Method == "GET" { if r.Method == "GET" {
getResponse := valueStringResponse{ Success: true, Value: config.Config.Path } getResponse := valueStringResponse{Success: true, Value: config.Config.Path}
// I can't see any way this will fail // I can't see any way this will fail
js, _ := json.Marshal(getResponse) js, _ := json.Marshal(getResponse)
@ -118,20 +116,20 @@ func getSetDirectory(w http.ResponseWriter, r *http.Request) {
stat, err := os.Stat(newPath) stat, err := os.Stat(newPath)
if os.IsNotExist(err) { if os.IsNotExist(err) {
// not exist // not exist
response := errorResponse{ Success: false, Error: fmt.Sprintf("Path: %s - does not exist", newPath) } response := errorResponse{Success: false, Error: fmt.Sprintf("Path: %s - does not exist", newPath)}
js, _ := json.Marshal(response) js, _ := json.Marshal(response)
w.Write(js) w.Write(js)
return; return
} else if ! stat.IsDir() { } else if !stat.IsDir() {
// not a directory // not a directory
response := errorResponse{ Success: false, Error: fmt.Sprintf("Path: %s - is not a directory", newPath) } response := errorResponse{Success: false, Error: fmt.Sprintf("Path: %s - is not a directory", newPath)}
js, _ := json.Marshal(response) js, _ := json.Marshal(response)
w.Write(js) w.Write(js)
return; return
} }
config.Config.Path = newPath config.Config.Path = newPath
postResponse := valueStringResponse{ Success: true, Value: config.Config.Path } postResponse := valueStringResponse{Success: true, Value: config.Config.Path}
js, _ := json.Marshal(postResponse) js, _ := json.Marshal(postResponse)
w.Write(js) w.Write(js)