From 36b6d806c881ad2ee86d3ec86d0af4e68b977aec Mon Sep 17 00:00:00 2001 From: comp500 Date: Mon, 14 Feb 2022 15:48:54 +0000 Subject: [PATCH] Re-encode URLs from CF and when exporting to MR for RFC3986 compliance --- core/urlutil.go | 14 ++++++++++++++ curseforge/curseforge.go | 14 ++++++++++++-- modrinth/export.go | 9 ++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 core/urlutil.go diff --git a/core/urlutil.go b/core/urlutil.go new file mode 100644 index 0000000..b66f977 --- /dev/null +++ b/core/urlutil.go @@ -0,0 +1,14 @@ +package core + +import ( + "fmt" + "net/url" +) + +func ReencodeURL(u string) (string, error) { + parsed, err := url.Parse(u) + if err != nil { + return "", fmt.Errorf("failed to parse url: %s, %v", u, err) + } + return parsed.String(), nil +} diff --git a/curseforge/curseforge.go b/curseforge/curseforge.go index 4a88f14..3ad39b8 100644 --- a/curseforge/curseforge.go +++ b/curseforge/curseforge.go @@ -174,12 +174,17 @@ func createModFile(modInfo modInfo, fileInfo modFileInfo, index *core.Index) err return err } + u, err := core.ReencodeURL(fileInfo.DownloadURL) + if err != nil { + return err + } + modMeta := core.Mod{ Name: modInfo.Name, FileName: fileInfo.FileName, Side: core.UniversalSide, Download: core.ModDownload{ - URL: fileInfo.DownloadURL, + URL: u, // TODO: murmur2 hashing may be unstable in curse api, calculate the hash manually? // TODO: check if the hash is invalid (e.g. 0) HashFormat: "murmur2", @@ -409,10 +414,15 @@ func (u cfUpdater) DoUpdate(mods []*core.Mod, cachedState []interface{}) error { } } + u, err := core.ReencodeURL(fileInfoData.DownloadURL) + if err != nil { + return err + } + v.FileName = fileInfoData.FileName v.Name = modState.Name v.Download = core.ModDownload{ - URL: fileInfoData.DownloadURL, + URL: u, // TODO: murmur2 hashing may be unstable in curse api, calculate the hash manually? // TODO: check if the hash is invalid (e.g. 0) HashFormat: "murmur2", diff --git a/modrinth/export.go b/modrinth/export.go index 339ec08..3ab8813 100644 --- a/modrinth/export.go +++ b/modrinth/export.go @@ -135,6 +135,13 @@ var exportCmd = &cobra.Command{ serverEnv = envInstalled } + // Modrinth URLs must be RFC3986 + u, err := core.ReencodeURL(mod.Download.URL) + if err != nil { + fmt.Printf("Error re-encoding mod URL: %s\n", err.Error()) + u = mod.Download.URL + } + manifestFiles[i] = PackFile{ Path: path, Hashes: hashes, @@ -142,7 +149,7 @@ var exportCmd = &cobra.Command{ Client string `json:"client"` Server string `json:"server"` }{Client: clientEnv, Server: serverEnv}, - Downloads: []string{mod.Download.URL}, + Downloads: []string{u}, } }