2019-08-30 00:05:59 +02:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2019-2020 Musing Studio LLC.
|
2019-08-30 00:05:59 +02:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
2020-02-15 18:58:45 +01:00
|
|
|
"github.com/writeas/web-core/log"
|
2023-07-10 11:55:04 +02:00
|
|
|
"io"
|
2019-08-30 00:05:59 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// updatesCacheTime is the default interval between cache updates for new
|
|
|
|
// software versions
|
2019-09-09 19:24:29 +02:00
|
|
|
const defaultUpdatesCacheTime = 12 * time.Hour
|
2019-08-30 00:05:59 +02:00
|
|
|
|
|
|
|
// updatesCache holds data about current and new releases of the writefreely
|
|
|
|
// software
|
|
|
|
type updatesCache struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
frequency time.Duration
|
|
|
|
lastCheck time.Time
|
|
|
|
latestVersion string
|
|
|
|
currentVersion string
|
2020-02-15 18:56:33 +01:00
|
|
|
checkError error
|
2019-08-30 00:05:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckNow asks for the latest released version of writefreely and updates
|
|
|
|
// the cache last checked time. If the version postdates the current 'latest'
|
|
|
|
// the version value is replaced.
|
|
|
|
func (uc *updatesCache) CheckNow() error {
|
2020-02-15 18:58:45 +01:00
|
|
|
if debugging {
|
|
|
|
log.Info("[update check] Checking for update now.")
|
|
|
|
}
|
2019-08-30 00:05:59 +02:00
|
|
|
uc.mu.Lock()
|
|
|
|
defer uc.mu.Unlock()
|
2020-02-15 18:56:33 +01:00
|
|
|
uc.lastCheck = time.Now()
|
2019-09-09 19:24:29 +02:00
|
|
|
latestRemote, err := newVersionCheck()
|
2019-08-30 00:05:59 +02:00
|
|
|
if err != nil {
|
2020-02-15 18:58:45 +01:00
|
|
|
log.Error("[update check] Failed: %v", err)
|
2020-02-15 18:56:33 +01:00
|
|
|
uc.checkError = err
|
2019-08-30 00:05:59 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if CompareSemver(latestRemote, uc.latestVersion) == 1 {
|
|
|
|
uc.latestVersion = latestRemote
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AreAvailable updates the cache if the frequency duration has passed
|
|
|
|
// then returns if the latest release is newer than the current running version.
|
|
|
|
func (uc updatesCache) AreAvailable() bool {
|
|
|
|
if time.Since(uc.lastCheck) > uc.frequency {
|
|
|
|
uc.CheckNow()
|
|
|
|
}
|
|
|
|
return CompareSemver(uc.latestVersion, uc.currentVersion) == 1
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:53:47 +01:00
|
|
|
// AreAvailableNoCheck returns if the latest release is newer than the current
|
|
|
|
// running version.
|
|
|
|
func (uc updatesCache) AreAvailableNoCheck() bool {
|
|
|
|
return CompareSemver(uc.latestVersion, uc.currentVersion) == 1
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:05:59 +02:00
|
|
|
// LatestVersion returns the latest stored version available.
|
|
|
|
func (uc updatesCache) LatestVersion() string {
|
|
|
|
return uc.latestVersion
|
|
|
|
}
|
|
|
|
|
2020-02-15 17:59:47 +01:00
|
|
|
func (uc updatesCache) ReleaseURL() string {
|
|
|
|
return "https://writefreely.org/releases/" + uc.latestVersion
|
|
|
|
}
|
|
|
|
|
2020-02-14 22:13:54 +01:00
|
|
|
// ReleaseNotesURL returns the full URL to the blog.writefreely.org release notes
|
2019-08-30 00:05:59 +02:00
|
|
|
// for the latest version as stored in the cache.
|
2020-02-14 22:13:54 +01:00
|
|
|
func (uc updatesCache) ReleaseNotesURL() string {
|
2020-02-15 18:19:06 +01:00
|
|
|
return wfReleaseNotesURL(uc.latestVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func wfReleaseNotesURL(v string) string {
|
|
|
|
ver := strings.TrimPrefix(v, "v")
|
2019-08-30 00:05:59 +02:00
|
|
|
ver = strings.TrimSuffix(ver, ".0")
|
2019-08-30 01:20:41 +02:00
|
|
|
// hack until go 1.12 in build/travis
|
|
|
|
seg := strings.Split(ver, ".")
|
|
|
|
return "https://blog.writefreely.org/version-" + strings.Join(seg, "-")
|
2019-08-30 00:05:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newUpdatesCache returns an initialized updates cache
|
2019-09-09 19:24:29 +02:00
|
|
|
func newUpdatesCache(expiry time.Duration) *updatesCache {
|
2019-08-30 00:05:59 +02:00
|
|
|
cache := updatesCache{
|
2019-09-09 19:24:29 +02:00
|
|
|
frequency: expiry,
|
2019-08-30 00:05:59 +02:00
|
|
|
currentVersion: "v" + softwareVer,
|
|
|
|
}
|
2020-02-25 20:12:54 +01:00
|
|
|
go cache.CheckNow()
|
2019-08-30 00:05:59 +02:00
|
|
|
return &cache
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitUpdates initializes the updates cache, if the config value is set
|
2019-09-09 19:24:29 +02:00
|
|
|
// It uses the defaultUpdatesCacheTime for the cache expiry
|
2019-08-30 00:05:59 +02:00
|
|
|
func (app *App) InitUpdates() {
|
|
|
|
if app.cfg.App.UpdateChecks {
|
2019-09-09 19:24:29 +02:00
|
|
|
app.updates = newUpdatesCache(defaultUpdatesCacheTime)
|
2019-08-30 00:05:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:24:29 +02:00
|
|
|
func newVersionCheck() (string, error) {
|
2019-08-30 00:05:59 +02:00
|
|
|
res, err := http.Get("https://version.writefreely.org")
|
2020-02-15 18:58:45 +01:00
|
|
|
if debugging {
|
|
|
|
log.Info("[update check] GET https://version.writefreely.org")
|
|
|
|
}
|
2020-02-25 20:12:54 +01:00
|
|
|
// TODO: return error if statusCode != OK
|
2019-08-30 00:05:59 +02:00
|
|
|
if err == nil && res.StatusCode == http.StatusOK {
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2023-07-10 11:55:04 +02:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2019-08-30 00:05:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(body), nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|