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
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-02-26 21:06:48 +10:30
|
|
|
"image"
|
|
|
|
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
|
|
|
|
"github.com/fogleman/gg"
|
2017-02-22 21:13:07 +10:30
|
|
|
"github.com/pborman/getopt"
|
2017-07-27 22:04:47 +09:30
|
|
|
"github.com/skratchdot/open-golang/open"
|
2017-02-26 21:06:48 +10:30
|
|
|
"golang.org/x/image/font/inconsolata"
|
2017-07-26 14:24:02 +09:30
|
|
|
|
2020-03-20 05:49:12 +10:30
|
|
|
"github.com/tardisx/discord-auto-upload/web"
|
2017-02-20 21:16:44 +10:30
|
|
|
)
|
|
|
|
|
2017-07-26 22:44:56 +09:30
|
|
|
const currentVersion = "0.7"
|
2017-02-21 12:24:14 +10:30
|
|
|
|
2020-03-20 05:49:12 +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-23 12:55:10 +10:30
|
|
|
// Config for the application
|
|
|
|
type Config struct {
|
2017-02-28 21:32:18 +10:30
|
|
|
webhookURL string
|
|
|
|
path string
|
|
|
|
watch int
|
|
|
|
username string
|
|
|
|
noWatermark bool
|
2017-02-28 22:50:03 +10:30
|
|
|
exclude string
|
2017-02-23 12:55:10 +10:30
|
|
|
}
|
2017-02-20 21:54:16 +10:30
|
|
|
|
2017-02-20 21:16:44 +10:30
|
|
|
func main() {
|
2017-02-22 21:13:07 +10:30
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
config := parseOptions()
|
|
|
|
checkPath(config.path)
|
2017-07-27 12:18:02 +09:30
|
|
|
wconfig := web.Init()
|
|
|
|
go processWebChanges(wconfig)
|
2017-07-26 14:24:02 +09:30
|
|
|
|
2017-07-27 22:04:47 +09:30
|
|
|
log.Print("Opening web browser")
|
|
|
|
open.Start("http://localhost:9090")
|
|
|
|
|
2017-07-26 22:40:21 +09:30
|
|
|
checkUpdates()
|
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
log.Print("Waiting for images to appear in ", config.path)
|
2017-02-22 21:13:07 +10:30
|
|
|
// wander the path, forever
|
|
|
|
for {
|
2017-02-23 12:55:10 +10:30
|
|
|
err := filepath.Walk(config.path,
|
|
|
|
func(path string, f os.FileInfo, err error) error { return checkFile(path, f, err, config) })
|
2017-02-22 21:13:07 +10:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not watch path", err)
|
|
|
|
}
|
|
|
|
lastCheck = newLastCheck
|
2020-03-26 11:40:35 +10:30
|
|
|
log.Print("sleeping before next check");
|
2017-02-23 12:55:10 +10:30
|
|
|
time.Sleep(time.Duration(config.watch) * time.Second)
|
2017-02-22 21:13:07 +10:30
|
|
|
}
|
2017-02-20 21:16:44 +10:30
|
|
|
}
|
|
|
|
|
2017-07-27 12:18:02 +09:30
|
|
|
func processWebChanges(wc web.DAUWebServer) {
|
|
|
|
for {
|
|
|
|
change := <-wc.ConfigChange
|
|
|
|
log.Print(change)
|
|
|
|
log.Print("Got a change!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 21:13:07 +10:30
|
|
|
func checkPath(path string) {
|
|
|
|
src, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if !src.IsDir() {
|
|
|
|
log.Fatal(path, " is not a directory")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-02-21 17:10:00 +10:30
|
|
|
}
|
|
|
|
|
2017-02-22 21:13:07 +10:30
|
|
|
func checkUpdates() {
|
|
|
|
|
|
|
|
type GithubRelease struct {
|
|
|
|
HTMLURL string
|
|
|
|
TagName string
|
|
|
|
Name string
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-07-26 22:44:56 +09:30
|
|
|
log.Print("WARNING: Update check failed: ", err)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentVersion < latest.TagName {
|
|
|
|
fmt.Printf("You are currently on version %s, but version %s is available\n", currentVersion, latest.TagName)
|
|
|
|
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")
|
2017-02-22 21:13:07 +10:30
|
|
|
}
|
2017-02-21 11:15:12 +10:30
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
func parseOptions() Config {
|
2017-02-20 21:16:44 +10:30
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
var newConfig Config
|
2017-02-22 21:13:07 +10:30
|
|
|
// Declare the flags to be used
|
|
|
|
webhookFlag := getopt.StringLong("webhook", 'w', "", "discord webhook URL")
|
|
|
|
pathFlag := getopt.StringLong("directory", 'd', "", "directory to scan, optional, defaults to current directory")
|
|
|
|
watchFlag := getopt.Int16Long("watch", 's', 10, "time between scans")
|
|
|
|
usernameFlag := getopt.StringLong("username", 'u', "", "username for the bot upload")
|
2017-02-28 22:50:03 +10:30
|
|
|
excludeFlag := getopt.StringLong("exclude", 'x', "", "exclude files containing this string")
|
2017-02-28 22:10:53 +10:30
|
|
|
noWatermarkFlag := getopt.BoolLong("no-watermark", 'n', "do not put a watermark on images before uploading")
|
2017-02-22 21:13:07 +10:30
|
|
|
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")
|
|
|
|
fmt.Printf("Version: %s\n", currentVersion)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2017-02-21 12:24:14 +10:30
|
|
|
|
2017-02-22 21:13:07 +10:30
|
|
|
if !getopt.IsSet("directory") {
|
|
|
|
*pathFlag = "./"
|
|
|
|
log.Println("Defaulting to current directory")
|
|
|
|
}
|
2017-02-21 12:24:14 +10:30
|
|
|
|
2017-02-22 21:13:07 +10:30
|
|
|
if !getopt.IsSet("webhook") {
|
|
|
|
log.Fatal("ERROR: You must specify a --webhook URL")
|
|
|
|
}
|
2017-02-21 12:24:14 +10:30
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
newConfig.path = *pathFlag
|
|
|
|
newConfig.webhookURL = *webhookFlag
|
|
|
|
newConfig.watch = int(*watchFlag)
|
|
|
|
newConfig.username = *usernameFlag
|
2017-02-28 21:32:18 +10:30
|
|
|
newConfig.noWatermark = *noWatermarkFlag
|
2017-02-28 22:50:03 +10:30
|
|
|
newConfig.exclude = *excludeFlag
|
2017-02-23 12:55:10 +10:30
|
|
|
|
|
|
|
return newConfig
|
2017-02-20 21:16:44 +10:30
|
|
|
}
|
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
func checkFile(path string, f os.FileInfo, err error, config Config) error {
|
2017-02-20 21:16:44 +10:30
|
|
|
|
2017-02-22 21:13:07 +10:30
|
|
|
if f.ModTime().After(lastCheck) && f.Mode().IsRegular() {
|
2017-02-20 21:16:44 +10:30
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
if fileEligible(config, path) {
|
2017-02-22 21:13:07 +10:30
|
|
|
// process file
|
2017-02-23 12:55:10 +10:30
|
|
|
processFile(config, 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
|
|
|
}
|
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
func fileEligible(config Config, file string) bool {
|
2017-02-28 22:50:03 +10:30
|
|
|
|
|
|
|
if config.exclude != "" && strings.Contains(file, config.exclude) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
func processFile(config Config, file string) {
|
2017-02-26 21:06:48 +10:30
|
|
|
|
2017-02-28 21:32:18 +10:30
|
|
|
if !config.noWatermark {
|
2017-02-28 22:07:57 +10:30
|
|
|
log.Print("Copying to temp location and watermarking ", file)
|
2017-02-28 21:32:18 +10:30
|
|
|
file = mungeFile(file)
|
|
|
|
}
|
2017-02-26 21:06:48 +10:30
|
|
|
|
2017-02-22 21:13:07 +10:30
|
|
|
log.Print("Uploading ", file)
|
|
|
|
|
|
|
|
extraParams := map[string]string{}
|
|
|
|
|
2017-02-23 12:55:10 +10:30
|
|
|
if config.username != "" {
|
|
|
|
extraParams["username"] = config.username
|
2017-02-22 21:13:07 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
type DiscordAPIResponseAttachment struct {
|
|
|
|
URL string
|
|
|
|
ProxyURL string
|
|
|
|
Size int
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DiscordAPIResponse struct {
|
|
|
|
Attachments []DiscordAPIResponseAttachment
|
|
|
|
ID int64 `json:",string"`
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:07:57 +10:30
|
|
|
var retriesRemaining = 5
|
|
|
|
for retriesRemaining > 0 {
|
2020-03-26 11:40:35 +10:30
|
|
|
|
2017-02-28 22:07:57 +10:30
|
|
|
request, err := newfileUploadRequest(config.webhookURL, extraParams, "file", file)
|
2017-02-22 21:13:07 +10:30
|
|
|
if err != nil {
|
2017-02-28 22:07:57 +10:30
|
|
|
log.Fatal(err)
|
2017-02-22 21:13:07 +10:30
|
|
|
}
|
2017-02-28 22:07:57 +10:30
|
|
|
start := time.Now()
|
|
|
|
client := &http.Client{Timeout: time.Second * 30}
|
|
|
|
resp, err := client.Do(request)
|
2017-02-22 21:13:07 +10:30
|
|
|
if err != nil {
|
2017-02-28 22:07:57 +10:30
|
|
|
log.Print("Error performing request:", err)
|
|
|
|
retriesRemaining--
|
|
|
|
sleepForRetries(retriesRemaining)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
log.Print("Bad response from server:", resp.StatusCode)
|
2020-03-26 11:40:35 +10:30
|
|
|
if b, err := ioutil.ReadAll(resp.Body); err == nil {
|
|
|
|
log.Print("Body:", string(b))
|
|
|
|
}
|
2017-02-28 22:07:57 +10:30
|
|
|
retriesRemaining--
|
|
|
|
sleepForRetries(retriesRemaining)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("could not deal with body: ", err)
|
|
|
|
retriesRemaining--
|
|
|
|
sleepForRetries(retriesRemaining)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
var res DiscordAPIResponse
|
|
|
|
err = json.Unmarshal(resBody, &res)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print("could not parse JSON: ", err)
|
|
|
|
fmt.Println("Response was:", string(resBody[:]))
|
|
|
|
retriesRemaining--
|
|
|
|
sleepForRetries(retriesRemaining)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(res.Attachments) < 1 {
|
|
|
|
log.Print("bad response - no attachments?")
|
|
|
|
retriesRemaining--
|
|
|
|
sleepForRetries(retriesRemaining)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var a = res.Attachments[0]
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
rate := float64(a.Size) / elapsed.Seconds() / 1024.0
|
|
|
|
|
|
|
|
log.Printf("Uploaded to %s %dx%d", a.URL, a.Width, a.Height)
|
|
|
|
log.Printf("id: %d, %d bytes transferred in %.2f seconds (%.2f KiB/s)", res.ID, a.Size, elapsed.Seconds(), rate)
|
|
|
|
break
|
2017-02-22 21:13:07 +10:30
|
|
|
}
|
2017-02-28 22:07:57 +10:30
|
|
|
}
|
2017-02-22 21:13:07 +10:30
|
|
|
|
2017-02-28 22:07:57 +10:30
|
|
|
if !config.noWatermark {
|
|
|
|
log.Print("Removing temporary file ", file)
|
|
|
|
os.Remove(file)
|
|
|
|
}
|
2017-02-28 21:32:18 +10:30
|
|
|
|
2017-02-28 22:07:57 +10:30
|
|
|
if retriesRemaining == 0 {
|
|
|
|
log.Fatal("Failed to upload, even after retries")
|
2017-02-22 21:13:07 +10:30
|
|
|
}
|
2017-02-28 22:07:57 +10:30
|
|
|
}
|
2017-02-21 11:15:12 +10:30
|
|
|
|
2017-02-28 22:07:57 +10:30
|
|
|
func sleepForRetries(retry int) {
|
|
|
|
if retry == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
retryTime := (6-retry)*(6-retry) + 6
|
|
|
|
log.Printf("Will retry in %d seconds (%d remaining attempts)", retryTime, retry)
|
|
|
|
// time.Sleep(time.Duration(retryTime) * time.Second)
|
2017-02-20 21:54:16 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
|
2017-02-22 21:13:07 +10:30
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(part, file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not copy: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range params {
|
|
|
|
_ = writer.WriteField(key, val)
|
|
|
|
}
|
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", uri, body)
|
|
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
return req, err
|
2017-02-20 21:16:44 +10:30
|
|
|
}
|
2017-02-26 21:06:48 +10:30
|
|
|
|
2017-02-28 21:32:18 +10:30
|
|
|
func mungeFile(path string) string {
|
2017-02-26 21:06:48 +10:30
|
|
|
|
|
|
|
reader, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
im, _, err := image.Decode(reader)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
bounds := im.Bounds()
|
|
|
|
// var S float64 = float64(bounds.Max.X)
|
|
|
|
|
|
|
|
dc := gg.NewContext(bounds.Max.X, bounds.Max.Y)
|
|
|
|
dc.Clear()
|
|
|
|
dc.SetRGB(0, 0, 0)
|
|
|
|
|
|
|
|
dc.SetFontFace(inconsolata.Regular8x16)
|
|
|
|
|
|
|
|
dc.DrawImage(im, 0, 0)
|
|
|
|
|
|
|
|
dc.DrawRoundedRectangle(0, float64(bounds.Max.Y-18.0), 320, float64(bounds.Max.Y), 0)
|
|
|
|
dc.SetRGB(0, 0, 0)
|
|
|
|
dc.Fill()
|
|
|
|
|
|
|
|
dc.SetRGB(1, 1, 1)
|
|
|
|
|
|
|
|
dc.DrawString("github.com/tardisx/discord-auto-upload", 5.0, float64(bounds.Max.Y)-5.0)
|
|
|
|
|
2017-02-28 21:32:18 +10:30
|
|
|
tempfile, err := ioutil.TempFile("", "dau")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
tempfile.Close()
|
|
|
|
os.Remove(tempfile.Name())
|
|
|
|
actualName := tempfile.Name() + ".png"
|
|
|
|
|
|
|
|
dc.SavePNG(actualName)
|
|
|
|
return actualName
|
2017-02-26 21:06:48 +10:30
|
|
|
}
|