Add semver parsing to stop overwriting of newer packages

This commit is contained in:
Tyler 2017-06-11 03:52:32 -04:00
parent 463c1751a9
commit 2fa86f850c
1 changed files with 23 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/blang/semver"
) )
func uploadHandler(config Conf) http.Handler { func uploadHandler(config Conf) http.Handler {
@ -35,6 +36,14 @@ func uploadHandler(config Conf) http.Handler {
return return
} }
force := false
forceStr := r.URL.Query().Get("force")
if forceStr != "" && forceStr == "true" {
force = true
}
reader, err := r.MultipartReader() reader, err := r.MultipartReader()
if err != nil { if err != nil {
@ -109,9 +118,22 @@ func uploadHandler(config Conf) http.Handler {
} }
if p, exists := packages[f.Info.Package]; exists { if p, exists := packages[f.Info.Package]; exists {
v1, err1 := semver.Parse(p.Info.Version)
v2, err2 := semver.Parse(f.Info.Version)
if err1 == nil && err2 == nil && v1.Compare(v2) > 0 && !force {
// Don't replace newer package
httpErrorf(w, "version in old package is greater than new: %s, %s - override with \"force\"", p.Info.Version, f.Info.Version)
return
}
// Archive old file // Archive old file
log.Println("Replacing", p.Name, "with", f.Name) log.Println("Replacing", p.Name, "with", f.Name)
os.Remove(p.Path)
if err := os.Remove(p.Path); err != nil {
httpErrorf(w, "Unable to remove old package: %s", err)
return
}
} }
packages[f.Info.Package] = f packages[f.Info.Package] = f