Files
Matheus Sampaio Queiroga 13cf2fe783
All checks were successful
Find and Upload Minecraft Server versions / build (push) Successful in 1m35s
Update parse
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2024-06-13 17:53:00 -03:00

191 lines
4.7 KiB
Go

package internal
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"sirherobrine23.org/minecraft-server/bedrockfetch/internal/promise"
"sirherobrine23.org/minecraft-server/bedrockfetch/internal/regex"
"sirherobrine23.org/minecraft-server/bedrockfetch/internal/request"
"sirherobrine23.org/minecraft-server/bedrockfetch/internal/tea"
)
var fileMatch = regex.MustCompile(`(?m)bin-(?P<Platform>win|linux)-?(?P<isPreview>preview)?\/bedrock-server-(?P<Version>[0-9\.]+)\.zip$`)
var rootSave, _ = filepath.Abs("./dists")
func (versions Versions) Parse(links []string) error {
releases := map[string]*tea.GiteaRelease{}
prm := promise.Create()
for _, urlString := range links {
// Skip if not match string
if !fileMatch.MatchString(urlString) {
continue
}
// Make map from group
match := fileMatch.FindAllGroups(urlString)
if match == nil {
continue
}
version, platform, preview := match["Version"], match["Platform"], false
if str, ok := match["isPreview"]; ok && str != "" {
preview = true
}
if len(platform) == 0 {
fmt.Printf("Cannont detect platform (%q), ignoring...\n", platform)
continue
} else if platform == "win" {
platform = "windows"
}
serverPath := filepath.Join(rootSave, fmt.Sprintf("%s-%s.zip", platform, version))
serverPathTgz := strings.Replace(serverPath, ".zip", ".tgz", 1)
// Create Version if not exists
if _, ok := versions[version]; !ok {
versions[version] = Version{
IsPreview: preview,
Platforms: map[string]VersionPlatform{},
}
}
if _, ok := versions[version].Platforms[fmt.Sprintf("%s/amd64", platform)]; ok {
fmt.Printf("Ignoring upload to %q, ared released in sirherobrine23.org, skiping ...\n", filepath.Base(serverPathTgz))
continue
}
// Zip file
req := request.RequestOptions{HttpError: true, Url: urlString}
// Add to promise
prm.Add(func() error {
var (
sumZip string
err error
)
fmt.Printf("Downloading %q ...\n", urlString)
if sumZip, err = req.FileSHA1(serverPath); err != nil {
return err
}
fmt.Printf("Converting %q to %q\n", filepath.Base(serverPath), filepath.Base(serverPathTgz))
zipFiles, err := zip.OpenReader(serverPath)
if err != nil {
return err
}
defer zipFiles.Close()
tgzFile, err := os.Create(serverPathTgz)
if err != nil {
return err
}
sumTz := sha1.New()
gz := gzip.NewWriter(io.MultiWriter(tgzFile, sumTz))
tz := tar.NewWriter(gz)
defer tz.Close()
defer gz.Close()
defer tgzFile.Close()
var bedrockRelease time.Time
for _, file := range zipFiles.File {
info := file.FileInfo()
// Write tar header
if err = tz.WriteHeader(&tar.Header{Name: file.Name, Size: info.Size(), Typeflag: byte(info.Mode().Type()), Mode: int64(info.Mode()), ModTime: file.Modified, ChangeTime: file.Modified}); err != nil {
return err
}
if strings.HasPrefix(file.Name, "bedrock_server") {
bedrockRelease = file.Modified
}
// Ignore if is directory
if !info.IsDir() {
// Open file
read, err := file.Open()
if err != nil {
return err
}
// Copy data to tar file
_, err = io.Copy(tz, read)
read.Close()
if err != nil {
return err
}
}
}
// End convert zip to tar
tz.Close() // End tar file
gz.Close() // End gzip file
tgzFile.Close() // End file write
sum := hex.EncodeToString(sumTz.Sum(nil))
// Ignore upload file to Sirherobrine23.org
if len(tea.Token) == 0 {
fmt.Println("Ignoring upload file! skiping")
return nil
}
var teaRelease *tea.GiteaRelease
if teaRelease, err = tea.CreateReleasee(version, "", preview, true, false); err != nil {
return err
}
releases[version] = teaRelease
// Open local file
if tgzFile, err = os.Open(serverPathTgz); err != nil {
return err
}
defer tgzFile.Close()
// Upload
fmt.Printf("Uploading %q ...\n", filepath.Base(serverPathTgz))
fileInfo, err := teaRelease.Upload(filepath.Base(serverPathTgz), tgzFile)
if err != nil {
return err
}
fmt.Printf("Done uploading %q\n", filepath.Base(serverPathTgz))
// Add to versions
versions[version].Platforms[fmt.Sprintf("%s/amd64", platform)] = VersionPlatform{
ZipFile: urlString,
ZipSHA1: sumZip,
TarFile: fileInfo.FileUrl,
TarSHA1: sum,
ReleaseDate: bedrockRelease,
}
return nil
})
}
if err := prm.Wait(); err != nil {
return err
}
for _, kr := range releases {
fmt.Printf("Updating %q release ... ", kr.TagName)
kr.IsDraft = false
if err := kr.Update(); err != nil {
fmt.Println()
return err
}
fmt.Println("done")
}
return nil
}