Skip to content

Commit e69c71c

Browse files
author
heavyscientist
committed
added download_links file + strengthened filename uniqueness
1 parent 5c4c8b7 commit e69c71c

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

pkg/rustmaps/download.go

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rustmaps
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"net/http"
@@ -12,6 +13,13 @@ import (
1213
"go.uber.org/zap"
1314
)
1415

16+
type DownloadLinks struct {
17+
MapURL string `json:"map_url"`
18+
ImageURL string `json:"image_url"`
19+
ImageIconURL string `json:"image_icon_url"`
20+
ThumbnailURL string `json:"thumbnail_url"`
21+
}
22+
1523
func (g *Generator) OverrideDownloadsDir(log *zap.Logger, dir string) {
1624
g.downloadsDir = dir
1725
if err := os.MkdirAll(dir, 0755); err != nil {
@@ -89,11 +97,34 @@ func (g *Generator) Download(log *zap.Logger, version string) error {
8997
log.Error("Error creating downloads directory", zap.Error(err))
9098
return err
9199
}
92-
mapTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s.map", m.Seed, m.Size, m.MapID))
93-
imageTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s.png", m.Seed, m.Size, m.MapID))
94-
imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_icons.png", m.Seed, m.Size, m.MapID))
95-
thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_thumbnail.png", m.Seed, m.Size, m.MapID))
96-
fmt.Printf("Download URL: %s\n", status.Data.DownloadURL)
100+
savedConfig := m.SavedConfig
101+
if savedConfig == "" {
102+
savedConfig = "procedural"
103+
}
104+
mapTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s.map", m.Seed, m.Size, savedConfig, m.Staging, m.MapID))
105+
imageTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID))
106+
imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_icons.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID))
107+
thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_thumbnail.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID))
108+
downloadLinksTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_download_links.json", m.Seed, m.Size, savedConfig, m.Staging, m.MapID))
109+
// create a json file next to the rest that contains the download urls
110+
log.Info("Downloading assets", zap.String("seed", m.Seed), zap.String("map_id", m.MapID))
111+
links := DownloadLinks{
112+
MapURL: status.Data.DownloadURL,
113+
ImageURL: status.Data.ImageURL,
114+
ImageIconURL: status.Data.ImageIconURL,
115+
ThumbnailURL: status.Data.ThumbnailURL,
116+
}
117+
downloadLinksData, err := json.MarshalIndent(links, "", " ")
118+
if err != nil {
119+
log.Error("Error marshalling JSON", zap.Error(err))
120+
return err
121+
}
122+
log.Info("Writing download links", zap.String("target", downloadLinksTarget))
123+
if err := os.WriteFile(downloadLinksTarget, downloadLinksData, 0644); err != nil {
124+
log.Error("Error writing JSON file", zap.Error(err))
125+
return err
126+
}
127+
97128
if err := g.DownloadFile(log, status.Data.DownloadURL, mapTarget); err != nil {
98129
log.Error("Error downloading map", zap.String("seed", m.Seed), zap.Error(err))
99130
return err

0 commit comments

Comments
 (0)