From e69c71cb72e12cf1633f00d2b916dd201335326a Mon Sep 17 00:00:00 2001 From: heavyscientist Date: Mon, 17 Feb 2025 16:26:50 -0800 Subject: [PATCH 1/4] added download_links file + strengthened filename uniqueness --- pkg/rustmaps/download.go | 41 +++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/rustmaps/download.go b/pkg/rustmaps/download.go index 2e9f89e..bdcce8d 100644 --- a/pkg/rustmaps/download.go +++ b/pkg/rustmaps/download.go @@ -1,6 +1,7 @@ package rustmaps import ( + "encoding/json" "fmt" "io" "net/http" @@ -12,6 +13,13 @@ import ( "go.uber.org/zap" ) +type DownloadLinks struct { + MapURL string `json:"map_url"` + ImageURL string `json:"image_url"` + ImageIconURL string `json:"image_icon_url"` + ThumbnailURL string `json:"thumbnail_url"` +} + func (g *Generator) OverrideDownloadsDir(log *zap.Logger, dir string) { g.downloadsDir = dir if err := os.MkdirAll(dir, 0755); err != nil { @@ -89,11 +97,34 @@ func (g *Generator) Download(log *zap.Logger, version string) error { log.Error("Error creating downloads directory", zap.Error(err)) return err } - mapTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s.map", m.Seed, m.Size, m.MapID)) - imageTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s.png", m.Seed, m.Size, m.MapID)) - imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_icons.png", m.Seed, m.Size, m.MapID)) - thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_thumbnail.png", m.Seed, m.Size, m.MapID)) - fmt.Printf("Download URL: %s\n", status.Data.DownloadURL) + savedConfig := m.SavedConfig + if savedConfig == "" { + savedConfig = "procedural" + } + mapTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s.map", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) + imageTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) + imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_icons.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) + thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_thumbnail.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) + downloadLinksTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_download_links.json", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) + // create a json file next to the rest that contains the download urls + log.Info("Downloading assets", zap.String("seed", m.Seed), zap.String("map_id", m.MapID)) + links := DownloadLinks{ + MapURL: status.Data.DownloadURL, + ImageURL: status.Data.ImageURL, + ImageIconURL: status.Data.ImageIconURL, + ThumbnailURL: status.Data.ThumbnailURL, + } + downloadLinksData, err := json.MarshalIndent(links, "", " ") + if err != nil { + log.Error("Error marshalling JSON", zap.Error(err)) + return err + } + log.Info("Writing download links", zap.String("target", downloadLinksTarget)) + if err := os.WriteFile(downloadLinksTarget, downloadLinksData, 0644); err != nil { + log.Error("Error writing JSON file", zap.Error(err)) + return err + } + if err := g.DownloadFile(log, status.Data.DownloadURL, mapTarget); err != nil { log.Error("Error downloading map", zap.String("seed", m.Seed), zap.Error(err)) return err From ddf8a6257244bae3e39ab2c3b5d53c40fb72f44a Mon Sep 17 00:00:00 2001 From: heavyscientist Date: Mon, 17 Feb 2025 16:45:37 -0800 Subject: [PATCH 2/4] added test when reading dir for config --- pkg/rustmaps/config_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/rustmaps/config_test.go b/pkg/rustmaps/config_test.go index f6fe78c..e4b1c01 100644 --- a/pkg/rustmaps/config_test.go +++ b/pkg/rustmaps/config_test.go @@ -32,6 +32,16 @@ func TestGenerator_LoadConfig(t *testing.T) { }), wantErr: true, }, + { + name: "Test LoadConfig fail tmp", + generator: NewMockedGenerator(t, &Generator{ + configPath: "/tmp/", + config: types.Config{}, + maps: []*types.Map{}, + rmcli: &api.RustMapsClient{}, + }), + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From e9e452df09d32178da69273448e7d6e966c0770b Mon Sep 17 00:00:00 2001 From: heavyscientist Date: Mon, 17 Feb 2025 16:55:50 -0800 Subject: [PATCH 3/4] added more tests for config.go --- pkg/rustmaps/config_test.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/rustmaps/config_test.go b/pkg/rustmaps/config_test.go index e4b1c01..07efdb9 100644 --- a/pkg/rustmaps/config_test.go +++ b/pkg/rustmaps/config_test.go @@ -22,6 +22,16 @@ func TestGenerator_LoadConfig(t *testing.T) { }), wantErr: false, }, + { + name: "Test LoadConfig with file", + generator: NewMockedGenerator(t, &Generator{ + configPath: "/etc/hosts", + config: types.Config{}, + maps: []*types.Map{}, + rmcli: &api.RustMapsClient{}, + }), + wantErr: true, + }, { name: "Test LoadConfig fail", generator: NewMockedGenerator(t, &Generator{ @@ -58,7 +68,25 @@ func TestGenerator_SaveConfig(t *testing.T) { generator *Generator wantErr bool }{ - // TODO: Add test cases. + { + name: "Test SaveConfig", + generator: NewMockedGenerator(t, &Generator{ + config: types.Config{}, + maps: []*types.Map{}, + rmcli: &api.RustMapsClient{}, + }), + wantErr: false, + }, + { + name: "Test SaveConfig fail", + generator: NewMockedGenerator(t, &Generator{ + configPath: "/tmp/asdfjasdlf/sadf432323/./.23", + config: types.Config{}, + maps: []*types.Map{}, + rmcli: &api.RustMapsClient{}, + }), + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 68a16479693038b8e39bec0329cb40f4fd647012 Mon Sep 17 00:00:00 2001 From: heavyscientist Date: Mon, 17 Feb 2025 17:00:26 -0800 Subject: [PATCH 4/4] cleanup path prefix --- pkg/rustmaps/download.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/rustmaps/download.go b/pkg/rustmaps/download.go index bdcce8d..948a317 100644 --- a/pkg/rustmaps/download.go +++ b/pkg/rustmaps/download.go @@ -101,11 +101,12 @@ func (g *Generator) Download(log *zap.Logger, version string) error { if savedConfig == "" { savedConfig = "procedural" } - mapTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s.map", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) - imageTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) - imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_icons.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) - thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_thumbnail.png", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) - downloadLinksTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_%d_%s_%t_%s_download_links.json", m.Seed, m.Size, savedConfig, m.Staging, m.MapID)) + prefix := fmt.Sprintf("%s_%d_%s_%t_%s", m.Seed, m.Size, savedConfig, m.Staging, m.MapID) + mapTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s.map", prefix)) + imageTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s.png", prefix)) + imageWithIconsTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_icons.png", prefix)) + thumbnailTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_thumbnail.png", prefix)) + downloadLinksTarget := filepath.Join(downloadsDir, fmt.Sprintf("%s_download_links.json", prefix)) // create a json file next to the rest that contains the download urls log.Info("Downloading assets", zap.String("seed", m.Seed), zap.String("map_id", m.MapID)) links := DownloadLinks{