Skip to content

Commit

Permalink
Update download endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Jan 8, 2025
1 parent 25238f7 commit a465335
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 98 deletions.
15 changes: 1 addition & 14 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
{
"port": 3001,
"debug": false,
"version": "0.5.9",
"dev_build_version": "0.6.0",
"mc_version": "1.21.3",
"dev_build_mc_version": "1.21.4",
"baritone_mc_version": "1.21.4",
"max_dev_builds": 10,
"changelog": [
"Minecraft 1.21.3",
"Small refactor of some Better Tooltips settings",
"Refactor Sprint",
"Add Hide Hud to Zoom",
"Add Hide Buttons to Auto Reconnect",
"Bunch of fixes all around"
]
"baritone_mc_version": "1.21.4"
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
go.mongodb.org/mongo-driver v1.5.1
golang.org/x/crypto v0.1.0
golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
120 changes: 38 additions & 82 deletions pkg/web/api/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package api
import (
"encoding/xml"
"fmt"
"github.com/rs/zerolog/log"
"io"
"meteor-server/pkg/core"
"meteor-server/pkg/db"
"net/http"
"os"
"strconv"
"strings"
)

type mavenMetadata struct {
ArtifactId string `xml:"artifactId"`

Versioning struct {
SnapshotVersions struct {
List []mavenMetadataSnapshotVersion `xml:"snapshotVersion"`
Expand All @@ -26,23 +25,19 @@ type mavenMetadataSnapshotVersion struct {
}

func DownloadHandler(w http.ResponseWriter, r *http.Request) {
devBuild := r.URL.Query().Get("devBuild")

if devBuild != "" {
version := core.GetConfig().DevBuildVersion

if devBuild == "latest" {
devBuild = db.GetGlobal().DevBuild
}
version, build := GetLatestVersion()

w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=meteor-client-%s-%s.jar", version, devBuild))
http.ServeFile(w, r, fmt.Sprintf("data/dev_builds/meteor-client-%s-%s.jar", version, devBuild))
} else {
version := core.GetConfig().Version
url := fmt.Sprintf("https://maven.meteordev.org/releases/meteordevelopment/meteor-client/%s/meteor-client-%s.jar", version, version)
http.Redirect(w, r, url, http.StatusPermanentRedirect)
if v := r.URL.Query().Get("version"); v != "" {
version = v
build = GetVersionBuild(v)
}

downloadFromMaven(
w, r,
"https://maven.meteordev.org/snapshots/meteordevelopment/meteor-client/"+version+"-SNAPSHOT",
fmt.Sprintf("meteor-client-%s-%d.jar", version, build),
)

db.IncrementDownloads()
}

Expand All @@ -53,8 +48,16 @@ func DownloadBaritoneHandler(w http.ResponseWriter, r *http.Request) {
version = core.GetConfig().BaritoneMcVersion
}

downloadFromMaven(
w, r,
"https://maven.meteordev.org/snapshots/meteordevelopment/baritone/"+version+"-SNAPSHOT",
fmt.Sprintf("baritone-meteor-%s.jar", version),
)
}

func downloadFromMaven(w http.ResponseWriter, r *http.Request, url string, filename string) {
// Get maven version
res, err := http.Get(fmt.Sprintf("https://maven.meteordev.org/snapshots/meteordevelopment/baritone/%s-SNAPSHOT/maven-metadata.xml", version))
res, err := http.Get(url + "/maven-metadata.xml")
if err != nil {
core.JsonError(w, "Failed to get maven version.")
return
Expand All @@ -75,80 +78,33 @@ func DownloadBaritoneHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Redirect
// Get file url
fileUrl := ""

for _, snapshotVersion := range metadata.Versioning.SnapshotVersions.List {
if snapshotVersion.Extension == "jar" {
http.Redirect(w, r, fmt.Sprintf("https://maven.meteordev.org/snapshots/meteordevelopment/baritone/%s-SNAPSHOT/baritone-%s.jar", version, snapshotVersion.Value), http.StatusPermanentRedirect)
return
fileUrl = fmt.Sprintf("%s/%s-%s.jar", url, metadata.ArtifactId, snapshotVersion.Value)
break
}
}

core.JsonError(w, "Failed to find jar file.")
}

func UploadDevBuildHandler(w http.ResponseWriter, r *http.Request) {
// Validate file
formFile, header, err := r.FormFile("file")
if err != nil {
core.JsonError(w, "Invalid file.")
return
}

if !strings.HasSuffix(header.Filename, ".jar") {
core.JsonError(w, "File needs to be a JAR.")
return
}

// Save file
_ = os.Mkdir("data/dev_builds", 0755)

devBuild := header.Filename[strings.LastIndex(header.Filename, "-")+1 : len(header.Filename)-4]
devBuildNum, _ := strconv.Atoi(devBuild)

currDevBuild, _ := strconv.Atoi(db.GetGlobal().DevBuild)

if currDevBuild < devBuildNum {
db.SetDevBuild(devBuild)
if fileUrl == "" {
core.JsonError(w, "Failed to find jar file.")
}

file, err := os.Create("data/dev_builds/meteor-client-" + core.GetConfig().DevBuildVersion + "-" + devBuild + ".jar")
// Server file
res, err = http.Get(fileUrl)
if err != nil {
core.JsonError(w, "Server error. Failed to create file.")
core.JsonError(w, "Failed to get file from maven.")
return
}

core.DownloadFile(formFile, file, w)

// Delete old file if needed
files, _ := os.ReadDir("data/dev_builds")

if len(files) > core.GetConfig().MaxDevBuilds {
oldestBuild := 6666
oldest := ""

for _, file := range files {
s := strings.TrimSuffix(file.Name(), ".jar")
build, _ := strconv.Atoi(s[strings.LastIndex(s, "-")+1:])

if build < oldestBuild {
oldestBuild = build
oldest = file.Name()
}
}

if oldest != "" {
_ = os.Remove("data/dev_builds/" + oldest)
}
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()

// Response
core.Json(w, core.J{
"version": core.GetConfig().DevBuildVersion,
"number": devBuild,
})
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
w.Header().Set("Content-Type", res.Header.Get("Content-Type"))
w.Header().Set("Content-Length", res.Header.Get("Content-Length"))

err = formFile.Close()
if err != nil {
log.Error().Msg("Error closing input file from updateDevBuild")
}
_, _ = io.Copy(w, res.Body)
}
25 changes: 24 additions & 1 deletion pkg/web/api/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"golang.org/x/mod/semver"
"io"
"meteor-server/pkg/core"
"meteor-server/pkg/db"
Expand All @@ -31,7 +32,7 @@ type Stats struct {
var builds map[string]int

func InitStats() {
t := time.NewTicker(time.Minute)
t := time.NewTicker(10 * time.Minute)

go func() {
for {
Expand Down Expand Up @@ -74,6 +75,28 @@ func RecheckMavenHandler(w http.ResponseWriter, _ *http.Request) {
core.Json(w, struct{}{})
}

func GetLatestVersion() (string, int) {
latest := "0.0.0"
build := 0

for version, number := range builds {
if semver.Compare("v"+version, "v"+latest) == 1 {
latest = version
build = number
}
}

return latest, build
}

func GetVersionBuild(version string) int {
if build, ok := builds[version]; ok {
return build
}

return 0
}

// Maven

type MavenSnapshotVersion struct {
Expand Down
1 change: 0 additions & 1 deletion pkg/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func Main() {
r.Get("/metrics", api.MetricsHandler)
r.Get("/capeowners", api.CapeOwnersHandler)

r.Post("/uploadDevBuild", auth.TokenAuth(api.UploadDevBuildHandler))
r.Post("/recheckMaven", auth.TokenAuth(api.RecheckMavenHandler))

// /api/account
Expand Down

0 comments on commit a465335

Please sign in to comment.