2022-05-02 20:07:15 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-06-05 01:56:46 +00:00
|
|
|
"path/filepath"
|
2022-05-02 20:07:15 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
type Progresser struct {
|
|
|
|
io.Reader
|
|
|
|
total int64
|
|
|
|
running int64
|
|
|
|
updates chan GenericUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *Progresser) Read(p []byte) (int, error) {
|
|
|
|
n, err := pt.Reader.Read(p)
|
|
|
|
pt.running += int64(n)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if pt.updates != nil {
|
|
|
|
select {
|
|
|
|
case pt.updates <- GenericUpdate{Progress: float64(pt.running) / float64(pt.total)}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:46:59 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return n, io.EOF
|
2022-06-03 22:44:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 22:46:59 +00:00
|
|
|
return n, errors.Wrap(err, "failed to read")
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GenericUpdate struct {
|
|
|
|
Progress float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadOrCache(cacheKey string, hash string, url string, updates chan GenericUpdate) (r io.ReaderAt, size int64, err error) {
|
2022-06-05 01:56:46 +00:00
|
|
|
downloadCache := filepath.Join(viper.GetString("cache-dir"), "downloadCache")
|
2022-05-02 20:07:15 +00:00
|
|
|
if err := os.MkdirAll(downloadCache, 0777); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed creating download cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:56:46 +00:00
|
|
|
location := filepath.Join(downloadCache, cacheKey)
|
2022-05-02 20:07:15 +00:00
|
|
|
|
|
|
|
stat, err := os.Stat(location)
|
|
|
|
if err == nil {
|
|
|
|
existingHash := ""
|
|
|
|
|
|
|
|
if hash != "" {
|
|
|
|
f, err := os.Open(location)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed to open file: "+location)
|
|
|
|
}
|
|
|
|
|
|
|
|
existingHash, err = SHA256Data(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "could not compute hash for file: "+location)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hash == existingHash {
|
|
|
|
f, err := os.Open(location)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed to open file: "+location)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, stat.Size(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(location); err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed to delete file: "+location)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed to stat file: "+location)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(location)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed creating file at: "+location)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed to fetch: "+url)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, 0, fmt.Errorf("bad status: %s on url: %s", resp.Status, url)
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
progresser := &Progresser{
|
|
|
|
Reader: resp.Body,
|
|
|
|
total: resp.ContentLength,
|
|
|
|
updates: updates,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(out, progresser)
|
2022-05-02 20:07:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed writing file to disk")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(location)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, errors.Wrap(err, "failed to open file: "+location)
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
if updates != nil {
|
|
|
|
select {
|
|
|
|
case updates <- GenericUpdate{Progress: 1}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
return f, resp.ContentLength, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SHA256Data(f io.Reader) (string, error) {
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return "", errors.Wrap(err, "failed to compute hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 23:36:28 +00:00
|
|
|
func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates chan GenericUpdate) error {
|
|
|
|
hashFile := filepath.Join(location, ".smm")
|
|
|
|
hashBytes, err := os.ReadFile(hashFile)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return errors.Wrap(err, "failed to read .smm mod hash file")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hash == string(hashBytes) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 20:07:15 +00:00
|
|
|
if err := os.MkdirAll(location, 0777); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
|
|
|
return errors.Wrap(err, "failed to create mod directory: "+location)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := os.RemoveAll(location); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to remove directory: "+location)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(location, 0777); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create mod directory: "+location)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := zip.NewReader(f, size)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to read file as zip")
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
for i, file := range reader.File {
|
2022-05-02 20:07:15 +00:00
|
|
|
if !file.FileInfo().IsDir() {
|
2022-06-05 01:56:46 +00:00
|
|
|
outFileLocation := filepath.Join(location, file.Name)
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2022-06-05 01:56:46 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(outFileLocation), 0777); err != nil {
|
2022-05-02 20:07:15 +00:00
|
|
|
return errors.Wrap(err, "failed to create mod directory: "+location)
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
if err := writeZipFile(outFileLocation, file); err != nil {
|
|
|
|
return err
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
if updates != nil {
|
|
|
|
select {
|
|
|
|
case updates <- GenericUpdate{Progress: float64(i) / float64(len(reader.File)-1)}:
|
|
|
|
default:
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-02 20:07:15 +00:00
|
|
|
|
2022-06-07 23:36:28 +00:00
|
|
|
if err := os.WriteFile(hashFile, []byte(hash), 0777); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to write .smm mod hash file")
|
|
|
|
}
|
|
|
|
|
2022-06-03 22:17:02 +00:00
|
|
|
if updates != nil {
|
|
|
|
select {
|
|
|
|
case updates <- GenericUpdate{Progress: 1}:
|
|
|
|
default:
|
2022-05-02 20:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-03 22:17:02 +00:00
|
|
|
|
|
|
|
func writeZipFile(outFileLocation string, file *zip.File) error {
|
|
|
|
outFile, err := os.OpenFile(outFileLocation, os.O_CREATE|os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to write to file: "+outFileLocation)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
inFile, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to process mod zip")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(outFile, inFile); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to write to file: "+outFileLocation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|