From 0f5341835f932a6d4f54a2d156155eceffe5738e Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Wed, 5 Mar 2025 01:06:44 +0100 Subject: [PATCH] Add version check Close #11 --- main.go | 2 ++ src/cli/cli.go | 21 +++++++++++++ src/meta/CheckVersionIsUpToDate.go | 47 ++++++++++++++++++++++++++++++ src/meta/meta.go | 2 +- src/webui/assets/md-uploader.js | 19 +++++++++--- src/webui/serveApiGetSettings.go | 3 +- src/webui/server.go | 20 ++++++++++++- src/webui/translation-json | 2 +- 8 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/meta/CheckVersionIsUpToDate.go diff --git a/main.go b/main.go index 832139a..09e073f 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ func main() { if slices.Contains(os.Args, "--help") { } else if slices.Contains(os.Args, "--version") { cli.ShowVersion(); + } else if slices.Contains(os.Args, "--check-update") { + cli.CheckVersionIsUpToDate(); } else if slices.Contains(os.Args, "--show-config") { cli.ShowCurrentConfig() } else if slices.Contains(os.Args, "--run-manual-setup") { diff --git a/src/cli/cli.go b/src/cli/cli.go index f24dc53..9ea379b 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -376,3 +376,24 @@ func ListLocalMedia() { func ShowVersion() { fmt.Println(meta.GetVersion()) } + +// Prints, if there is a current version. +func CheckVersionIsUpToDate() { + + output, err := meta.CheckVersionIsUpToDate() + if err != nil { + fmt.Println("Failed to get RSS stream for most recent entry") + fmt.Print(err) + panic("Aborting") + } + + // Empty output has no title + if output.Title == "" { + fmt.Println("Your version is up to date") + } else { + fmt.Println("There is a more recent version, version: " + output.Title) + fmt.Println("\nReleased: " + output.PubDate) + fmt.Println("\nGet it at " + output.Link) + } + +} diff --git a/src/meta/CheckVersionIsUpToDate.go b/src/meta/CheckVersionIsUpToDate.go new file mode 100644 index 0000000..612f59a --- /dev/null +++ b/src/meta/CheckVersionIsUpToDate.go @@ -0,0 +1,47 @@ +package meta + +import ( + "net/http" + "encoding/xml" +) + +// Very reduced item, as only title, link and pubDate are useful for a version check. +type Item struct { + Title string `xml:"title"` + Link string `xml:"link"` + PubDate string `xml:"pubDate"` +} + +type Channel struct { + Items []Item `xml:"item"` +} + +type Rss struct { + Channel Channel `xml:"channel"` +} + +// Checks if the current version is up to date or not. If there is a newer / different +// version. +func CheckVersionIsUpToDate() (Item, error) { + + resp, err := http.Get("https://gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/releases.rss") + if err != nil { + return Item{}, err + } + defer resp.Body.Close() + + rss := Rss{} + + decoder := xml.NewDecoder(resp.Body) + decodeErr := decoder.Decode(&rss) + if decodeErr != nil { + return Item{}, err + } + + newestItem := rss.Channel.Items[0]; + if (newestItem.Title == GetVersion() || newestItem.Title == "v" + GetVersion()) { + return Item{}, nil + } + return newestItem, nil + +} diff --git a/src/meta/meta.go b/src/meta/meta.go index d0a719a..7a867f9 100644 --- a/src/meta/meta.go +++ b/src/meta/meta.go @@ -2,5 +2,5 @@ package meta // Returns current version number. func GetVersion() string { - return "0.1" + return "0.1.1" } diff --git a/src/webui/assets/md-uploader.js b/src/webui/assets/md-uploader.js index 6e6a8b8..c9aebc1 100644 --- a/src/webui/assets/md-uploader.js +++ b/src/webui/assets/md-uploader.js @@ -351,7 +351,7 @@ class App { // Handle next auto upload const nextAutoUpload = document.createElement("span"); - const startTimeStr = "Next scheduled check: "; + const startTimeStr = this.tls.next_scheduled_check + ": "; this.actionsArea.appendChild(nextAutoUpload); const options1 = { @@ -365,10 +365,21 @@ class App { const dateTimeFormat1 = new Intl.DateTimeFormat(this.lang, options1); const nextUploadTime = Date.parse(this.config.next_auto_upload); const now = new Date(); - const diffMs = nextUploadTime - now; - const diffMins = Math.round((diffMs % 86400000) / 60000); // minutes - nextAutoUpload.textContent = dateTimeFormat1.format(nextUploadTime) + " (in " + diffMins + " mins)"; + setInterval(function() { + const diffMs = nextUploadTime - now; + const diffMins = Math.round((diffMs % 86400000) / 60000); // minutes + nextAutoUpload.textContent = startTimeStr + dateTimeFormat1.format(nextUploadTime) + " (in " + diffMins + " mins)"; + }, 60); + + // Version + const vSpan = document.createElement("a"); + vSpan.textContent = this.tls.version + " " + this.config.version; + if (this.config.update_available === true) { + vSpan.style.color = "red"; + vSpan.href = "https://gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/releases"; + } + this.actionsArea.appendChild(vSpan) } diff --git a/src/webui/serveApiGetSettings.go b/src/webui/serveApiGetSettings.go index c3309a8..cdbfc59 100644 --- a/src/webui/serveApiGetSettings.go +++ b/src/webui/serveApiGetSettings.go @@ -13,6 +13,7 @@ type getSettingsApiResponse struct { SetupRequired bool `json:"setup_required"` Settings configloader.MDWebDavUploaderConfig `json:"settings"` NextAutoUpload string `json:"next_auto_upload"` + UpdateAvailable bool `json:"update_available"` } // Generates the API output for listing current settings. @@ -27,7 +28,7 @@ func serveApiGetSettings(w http.ResponseWriter, r *http.Request) { } setHeadersForJson(w) - output := getSettingsApiResponse{SetupRequired: setupRequired, Settings: config, Version: meta.GetVersion(), NextAutoUpload: nextAutoUpOut} + output := getSettingsApiResponse{SetupRequired: setupRequired, Settings: config, Version: meta.GetVersion(), NextAutoUpload: nextAutoUpOut, UpdateAvailable: updateAvailable} outputJson, encodeErr := json.Marshal(output) if encodeErr != nil { diff --git a/src/webui/server.go b/src/webui/server.go index 278a320..239782f 100644 --- a/src/webui/server.go +++ b/src/webui/server.go @@ -10,12 +10,14 @@ import ( "github.com/go-co-op/gocron/v2" "gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/src/cli" "gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/src/configloader" + "gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/src/meta" ) var config configloader.MDWebDavUploaderConfig -var setupRequired bool +var setupRequired bool var scheduler gocron.Scheduler var scheduledUpload gocron.Job +var updateAvailable bool // Opens the application in the default browser. func openInBrowser(port string) { @@ -69,9 +71,25 @@ func scheduleUploads() { } +// Checks if a new update is available. If that can be confirmed, set updateAvailable +// to true. +func checkUpdateAvailable() { + + output, err := meta.CheckVersionIsUpToDate() + // If the RSS stream could not be fetched or parsed, don't say anything + if err != nil || output.Title == "" { + updateAvailable = false + } else { + updateAvailable = true + } + +} + // Sets up the server and manages routing. func Run() { + checkUpdateAvailable() + config, setupRequired, _ = configloader.LoadFromFile("") // Bind callable URLs diff --git a/src/webui/translation-json b/src/webui/translation-json index 6803016..89deadc 160000 --- a/src/webui/translation-json +++ b/src/webui/translation-json @@ -1 +1 @@ -Subproject commit 6803016b709dcc0bf92ddfc29c6bf7bd9bd29cf7 +Subproject commit 89deadcad02169b1d5bcb73b11a37b10e45ada2d