From 6b1867f35f9c3a1d49c49dd3ee14bebdae41bae0 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sun, 7 Feb 2021 08:10:06 +1030 Subject: [PATCH] Support noWatermark config --- data/config.html | 30 ++++++++++++++++++++++++++++-- web/server.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/data/config.html b/data/config.html index fe485c4..2bbddc5 100644 --- a/data/config.html +++ b/data/config.html @@ -55,7 +55,24 @@
- + +
+
+ +
+ + + +
+
+
+ Do not watermark images +
+
+
+ + +
@@ -78,7 +95,13 @@ $(document).ready(function() { $.ajax({ method: 'get', url: '/rest/config/'+key}) .done(function(data) { - $(".config-item[data-key='"+key+"']").find('.rest-field').val(data.Value); + var this_el = $(".config-item[data-key='"+key+"']").find('.rest-field'); + if (this_el.hasClass('rest-field-boolean')) { + this_el.prop('checked', data.Value); + } + else { + this_el.val(data.Value); + } }); }); @@ -86,6 +109,9 @@ $(document).ready(function() { $('.config-item button').on('click', function(e,f) { key = $(this).parents('.config-item').data('key'); val = $(this).parents('.config-item').find('.rest-field').val(); + if ($(this).parents('.config-item').find('.rest-field-boolean')) { + val = $(this).parents('.config-item').find('.rest-field').prop('checked') ? 1 : 0; + } $.post('/rest/config/'+key, { value: val }) .done(function(d) { if (d.Success) { diff --git a/web/server.go b/web/server.go index 551b773..8c4e06f 100644 --- a/web/server.go +++ b/web/server.go @@ -25,6 +25,11 @@ type valueStringResponse struct { Value string `json: 'value'` } +type valueBooleanResponse struct { + Success bool `json: 'success'` + Value bool `json: 'value'` +} + type errorResponse struct { Success bool `json: 'success'` Error string `json: 'error'` @@ -159,6 +164,43 @@ func getSetWatch(w http.ResponseWriter, r *http.Request) { } } +func getSetNoWatermark(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + if r.Method == "GET" { + getResponse := valueBooleanResponse{Success: true, Value: config.Config.NoWatermark} + + // I can't see any way this will fail + js, _ := json.Marshal(getResponse) + w.Write(js) + } else if r.Method == "POST" { + err := r.ParseForm() + if err != nil { + log.Fatal(err) + } + + v := r.PostForm.Get("value") + + if v != "0" && v != "1" { + response := errorResponse{Success: false, Error: fmt.Sprintf("Bad value for nowatermark: %v", err)} + js, _ := json.Marshal(response) + w.Write(js) + return + } + + if v == "0" { + config.Config.NoWatermark = false + } else { + config.Config.NoWatermark = true + } + postResponse := valueBooleanResponse{Success: true, Value: config.Config.NoWatermark} + + js, _ := json.Marshal(postResponse) + w.Write(js) + } +} + + func getSetDirectory(w http.ResponseWriter, r *http.Request) { log.Print("ok") w.Header().Set("Content-Type", "application/json") @@ -205,6 +247,7 @@ func StartWebServer() { http.HandleFunc("/rest/config/webhook", getSetWebhook) http.HandleFunc("/rest/config/username", getSetUsername) http.HandleFunc("/rest/config/watch", getSetWatch) + http.HandleFunc("/rest/config/nowatermark", getSetNoWatermark) http.HandleFunc("/rest/config/directory", getSetDirectory) log.Print("Starting web server on http://localhost:9090")