mirror of
https://github.com/packwiz/packwiz.git
synced 2025-11-19 01:24:32 +01:00
WIP: misc fixes, start updating CF/MR export to use download system
This commit is contained in:
110
core/mod.go
110
core/mod.go
@@ -2,15 +2,10 @@ package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/exp/slices"
|
||||
"github.com/BurntSushi/toml"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// Mod stores metadata about a mod. This is written to a TOML file for each mod.
|
||||
@@ -134,106 +129,3 @@ func (m Mod) GetFilePath() string {
|
||||
func (m Mod) GetDestFilePath() string {
|
||||
return filepath.Join(filepath.Dir(m.metaFile), filepath.FromSlash(m.FileName))
|
||||
}
|
||||
|
||||
// DownloadFile attempts to resolve and download the file
|
||||
func (m Mod) DownloadFile(dest io.Writer) error {
|
||||
// TODO: check mode
|
||||
resp, err := http.Get(m.Download.URL)
|
||||
// TODO: content type, user-agent?
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
_ = resp.Body.Close()
|
||||
return errors.New("invalid status code " + strconv.Itoa(resp.StatusCode))
|
||||
}
|
||||
h, err := GetHashImpl(m.Download.HashFormat)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get hash format %s to download file: %w", m.Download.HashFormat, err)
|
||||
}
|
||||
|
||||
w := io.MultiWriter(h, dest)
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
calculatedHash := h.HashToString(h.Sum(nil))
|
||||
|
||||
// Check if the hash of the downloaded file matches the expected hash.
|
||||
if calculatedHash != m.Download.Hash {
|
||||
return fmt.Errorf("Hash of downloaded file does not match with expected hash!\n download hash: %s\n expected hash: %s\n", calculatedHash, m.Download.Hash)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetHashes attempts to retrieve the values of all hashes passed to it, downloading if necessary
|
||||
func (m Mod) GetHashes(hashes []string) (map[string]string, error) {
|
||||
out := make(map[string]string)
|
||||
|
||||
// Get the hash already stored TODO: store multiple (requires breaking pack change)
|
||||
if m.Download.Hash != "" {
|
||||
idx := slices.Index(hashes, m.Download.HashFormat)
|
||||
if idx > -1 {
|
||||
out[m.Download.HashFormat] = m.Download.Hash
|
||||
// Remove hash from list to retrieve
|
||||
hashes = slices.Delete(hashes, idx, idx+1)
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve the remaining hashes
|
||||
if len(hashes) > 0 {
|
||||
// TODO: check mode
|
||||
resp, err := http.Get(m.Download.URL)
|
||||
// TODO: content type, user-agent?
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
_ = resp.Body.Close()
|
||||
return nil, errors.New("invalid status code " + strconv.Itoa(resp.StatusCode))
|
||||
}
|
||||
|
||||
// Special fast-path for file length only
|
||||
if len(hashes) == 1 && hashes[0] == "length-bytes" && resp.ContentLength > 0 {
|
||||
out["length-bytes"] = strconv.FormatInt(resp.ContentLength, 10)
|
||||
_ = resp.Body.Close()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
mainHasher, err := GetHashImpl(m.Download.HashFormat)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get hash format %s to download file: %w", m.Download.HashFormat, err)
|
||||
}
|
||||
|
||||
hashers := make([]HashStringer, len(hashes))
|
||||
allHashers := make([]io.Writer, len(hashers))
|
||||
for i, v := range hashes {
|
||||
hashers[i], err = GetHashImpl(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get hash format %s for file: %w", v, err)
|
||||
}
|
||||
allHashers[i] = hashers[i]
|
||||
}
|
||||
allHashers = append(allHashers, mainHasher)
|
||||
|
||||
w := io.MultiWriter(allHashers...)
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download file: %w", err)
|
||||
}
|
||||
|
||||
calculatedHash := mainHasher.HashToString(mainHasher.Sum(nil))
|
||||
|
||||
// Check if the hash of the downloaded file matches the expected hash
|
||||
if calculatedHash != m.Download.Hash {
|
||||
return nil, fmt.Errorf("Hash of downloaded file does not match with expected hash!\n download hash: %s\n expected hash: %s\n", calculatedHash, m.Download.Hash)
|
||||
}
|
||||
|
||||
for i, v := range hashers {
|
||||
out[hashes[i]] = v.HashToString(v.Sum(nil))
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user