@@ -3,16 +3,15 @@ package api
33import (
44 "encoding/xml"
55 "fmt"
6- "github.com/rs/zerolog/log "
6+ "io "
77 "meteor-server/pkg/core"
88 "meteor-server/pkg/db"
99 "net/http"
10- "os"
11- "strconv"
12- "strings"
1310)
1411
1512type mavenMetadata struct {
13+ ArtifactId string `xml:"artifactId"`
14+
1615 Versioning struct {
1716 SnapshotVersions struct {
1817 List []mavenMetadataSnapshotVersion `xml:"snapshotVersion"`
@@ -26,23 +25,19 @@ type mavenMetadataSnapshotVersion struct {
2625}
2726
2827func DownloadHandler (w http.ResponseWriter , r * http.Request ) {
29- devBuild := r .URL .Query ().Get ("devBuild" )
30-
31- if devBuild != "" {
32- version := core .GetConfig ().DevBuildVersion
33-
34- if devBuild == "latest" {
35- devBuild = db .GetGlobal ().DevBuild
36- }
28+ version , build := GetLatestVersion ()
3729
38- w .Header ().Set ("Content-Disposition" , fmt .Sprintf ("attachment; filename=meteor-client-%s-%s.jar" , version , devBuild ))
39- http .ServeFile (w , r , fmt .Sprintf ("data/dev_builds/meteor-client-%s-%s.jar" , version , devBuild ))
40- } else {
41- version := core .GetConfig ().Version
42- url := fmt .Sprintf ("https://maven.meteordev.org/releases/meteordevelopment/meteor-client/%s/meteor-client-%s.jar" , version , version )
43- http .Redirect (w , r , url , http .StatusPermanentRedirect )
30+ if v := r .URL .Query ().Get ("version" ); v != "" {
31+ version = v
32+ build = GetVersionBuild (v )
4433 }
4534
35+ downloadFromMaven (
36+ w , r ,
37+ "https://maven.meteordev.org/snapshots/meteordevelopment/meteor-client/" + version + "-SNAPSHOT" ,
38+ fmt .Sprintf ("meteor-client-%s-%d.jar" , version , build ),
39+ )
40+
4641 db .IncrementDownloads ()
4742}
4843
@@ -53,8 +48,16 @@ func DownloadBaritoneHandler(w http.ResponseWriter, r *http.Request) {
5348 version = core .GetConfig ().BaritoneMcVersion
5449 }
5550
51+ downloadFromMaven (
52+ w , r ,
53+ "https://maven.meteordev.org/snapshots/meteordevelopment/baritone/" + version + "-SNAPSHOT" ,
54+ fmt .Sprintf ("baritone-meteor-%s.jar" , version ),
55+ )
56+ }
57+
58+ func downloadFromMaven (w http.ResponseWriter , r * http.Request , url string , filename string ) {
5659 // Get maven version
57- res , err := http .Get (fmt . Sprintf ( "https:// maven.meteordev.org/snapshots/meteordevelopment/baritone/%s-SNAPSHOT/maven- metadata.xml", version ) )
60+ res , err := http .Get (url + "/ maven- metadata.xml" )
5861 if err != nil {
5962 core .JsonError (w , "Failed to get maven version." )
6063 return
@@ -75,80 +78,33 @@ func DownloadBaritoneHandler(w http.ResponseWriter, r *http.Request) {
7578 return
7679 }
7780
78- // Redirect
81+ // Get file url
82+ fileUrl := ""
83+
7984 for _ , snapshotVersion := range metadata .Versioning .SnapshotVersions .List {
8085 if snapshotVersion .Extension == "jar" {
81- http . Redirect ( w , r , fmt .Sprintf ("https://maven.meteordev.org/snapshots/meteordevelopment/baritone/ %s-SNAPSHOT/baritone- %s.jar" , version , snapshotVersion . Value ), http . StatusPermanentRedirect )
82- return
86+ fileUrl = fmt .Sprintf ("%s/ %s-%s.jar" , url , metadata . ArtifactId , snapshotVersion . Value )
87+ break
8388 }
8489 }
8590
86- core .JsonError (w , "Failed to find jar file." )
87- }
88-
89- func UploadDevBuildHandler (w http.ResponseWriter , r * http.Request ) {
90- // Validate file
91- formFile , header , err := r .FormFile ("file" )
92- if err != nil {
93- core .JsonError (w , "Invalid file." )
94- return
95- }
96-
97- if ! strings .HasSuffix (header .Filename , ".jar" ) {
98- core .JsonError (w , "File needs to be a JAR." )
99- return
100- }
101-
102- // Save file
103- _ = os .Mkdir ("data/dev_builds" , 0755 )
104-
105- devBuild := header .Filename [strings .LastIndex (header .Filename , "-" )+ 1 : len (header .Filename )- 4 ]
106- devBuildNum , _ := strconv .Atoi (devBuild )
107-
108- currDevBuild , _ := strconv .Atoi (db .GetGlobal ().DevBuild )
109-
110- if currDevBuild < devBuildNum {
111- db .SetDevBuild (devBuild )
91+ if fileUrl == "" {
92+ core .JsonError (w , "Failed to find jar file." )
11293 }
11394
114- file , err := os .Create ("data/dev_builds/meteor-client-" + core .GetConfig ().DevBuildVersion + "-" + devBuild + ".jar" )
95+ // Server file
96+ res , err = http .Get (fileUrl )
11597 if err != nil {
116- core .JsonError (w , "Server error. Failed to create file." )
98+ core .JsonError (w , "Failed to get file from maven ." )
11799 return
118100 }
119101
120- core .DownloadFile (formFile , file , w )
121-
122- // Delete old file if needed
123- files , _ := os .ReadDir ("data/dev_builds" )
124-
125- if len (files ) > core .GetConfig ().MaxDevBuilds {
126- oldestBuild := 6666
127- oldest := ""
128-
129- for _ , file := range files {
130- s := strings .TrimSuffix (file .Name (), ".jar" )
131- build , _ := strconv .Atoi (s [strings .LastIndex (s , "-" )+ 1 :])
132-
133- if build < oldestBuild {
134- oldestBuild = build
135- oldest = file .Name ()
136- }
137- }
138-
139- if oldest != "" {
140- _ = os .Remove ("data/dev_builds/" + oldest )
141- }
142- }
102+ //goland:noinspection GoUnhandledErrorResult
103+ defer res .Body .Close ()
143104
144- // Response
145- core .Json (w , core.J {
146- "version" : core .GetConfig ().DevBuildVersion ,
147- "number" : devBuild ,
148- })
105+ w .Header ().Set ("Content-Disposition" , "attachment; filename=" + filename )
106+ w .Header ().Set ("Content-Type" , res .Header .Get ("Content-Type" ))
107+ w .Header ().Set ("Content-Length" , res .Header .Get ("Content-Length" ))
149108
150- err = formFile .Close ()
151- if err != nil {
152- log .Error ().Msg ("Error closing input file from updateDevBuild" )
153- }
109+ _ , _ = io .Copy (w , res .Body )
154110}
0 commit comments