Implement Modrinth pack exporting (fixes #34)

This commit is contained in:
comp500
2021-12-28 22:03:22 +00:00
parent 4abf3340a1
commit 60c08b93f3
7 changed files with 358 additions and 21 deletions

View File

@@ -5,23 +5,44 @@ import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/packwiz/packwiz/curseforge/murmur2"
"hash"
"strconv"
"strings"
)
// GetHashImpl gets an implementation of hash.Hash for the given hash type string
func GetHashImpl(hashType string) (hash.Hash, error) {
func GetHashImpl(hashType string) (hash.Hash, HashStringer, error) {
switch strings.ToLower(hashType) {
case "sha1":
return sha1.New(), nil
return sha1.New(), hexStringer{}, nil
case "sha256":
return sha256.New(), nil
return sha256.New(), hexStringer{}, nil
case "sha512":
return sha512.New(), nil
return sha512.New(), hexStringer{}, nil
case "md5":
return md5.New(), nil
return md5.New(), hexStringer{}, nil
case "murmur2":
return murmur2.New(), numberStringer{}, nil
}
// TODO: implement murmur2
return nil, errors.New("hash implementation not found")
return nil, nil, fmt.Errorf("hash implementation %s not found", hashType)
}
type HashStringer interface {
HashToString([]byte) string
}
type hexStringer struct{}
func (hexStringer) HashToString(data []byte) string {
return hex.EncodeToString(data)
}
type numberStringer struct{}
func (numberStringer) HashToString(data []byte) string {
return strconv.FormatUint(uint64(binary.BigEndian.Uint32(data)), 10)
}

View File

@@ -1,7 +1,6 @@
package core
import (
"encoding/hex"
"errors"
"io"
"os"
@@ -133,7 +132,7 @@ func (in *Index) updateFile(path string) error {
// Hash usage strategy (may change):
// Just use SHA256, overwrite existing hash regardless of what it is
// May update later to continue using the same hash that was already being used
h, err := GetHashImpl("sha256")
h, stringer, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return err
@@ -146,7 +145,7 @@ func (in *Index) updateFile(path string) error {
if err != nil {
return err
}
hashString = hex.EncodeToString(h.Sum(nil))
hashString = stringer.HashToString(h.Sum(nil))
}
mod := false
@@ -348,7 +347,7 @@ func (in Index) SaveFile(f IndexFile, dest io.Writer) error {
if err != nil {
return err
}
h, err := GetHashImpl(hashFormat)
h, stringer, err := GetHashImpl(hashFormat)
if err != nil {
return err
}
@@ -359,7 +358,7 @@ func (in Index) SaveFile(f IndexFile, dest io.Writer) error {
return err
}
calculatedHash := hex.EncodeToString(h.Sum(nil))
calculatedHash := stringer.HashToString(h.Sum(nil))
if calculatedHash != f.Hash && !viper.GetBool("no-internal-hashes") {
return errors.New("hash of saved file is invalid")
}

View File

@@ -1,7 +1,6 @@
package core
import (
"encoding/hex"
"errors"
"fmt"
"io"
@@ -90,7 +89,7 @@ func (m Mod) Write() (string, string, error) {
}
}
h, err := GetHashImpl("sha256")
h, stringer, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return "", "", err
@@ -101,7 +100,7 @@ func (m Mod) Write() (string, string, error) {
// Disable indentation
enc.Indent = ""
err = enc.Encode(m)
hashString := hex.EncodeToString(h.Sum(nil))
hashString := stringer.HashToString(h.Sum(nil))
if err != nil {
_ = f.Close()
return "sha256", hashString, err
@@ -135,7 +134,7 @@ func (m Mod) DownloadFile(dest io.Writer) error {
_ = resp.Body.Close()
return errors.New("invalid status code " + strconv.Itoa(resp.StatusCode))
}
h, err := GetHashImpl(m.Download.HashFormat)
h, stringer, err := GetHashImpl(m.Download.HashFormat)
if err != nil {
return err
}
@@ -146,7 +145,7 @@ func (m Mod) DownloadFile(dest io.Writer) error {
return err
}
calculatedHash := hex.EncodeToString(h.Sum(nil))
calculatedHash := stringer.HashToString(h.Sum(nil))
// Check if the hash of the downloaded file matches the expected hash.
if calculatedHash != m.Download.Hash {

View File

@@ -1,7 +1,6 @@
package core
import (
"encoding/hex"
"errors"
"fmt"
"io"
@@ -116,7 +115,7 @@ func (pack *Pack) UpdateIndexHash() error {
// Hash usage strategy (may change):
// Just use SHA256, overwrite existing hash regardless of what it is
// May update later to continue using the same hash that was already being used
h, err := GetHashImpl("sha256")
h, stringer, err := GetHashImpl("sha256")
if err != nil {
_ = f.Close()
return err
@@ -125,7 +124,7 @@ func (pack *Pack) UpdateIndexHash() error {
_ = f.Close()
return err
}
hashString := hex.EncodeToString(h.Sum(nil))
hashString := stringer.HashToString(h.Sum(nil))
pack.Index.HashFormat = "sha256"
pack.Index.Hash = hashString