107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"slices"
|
|
"time"
|
|
|
|
"git.lastassault.de/speatzle/morffix/config"
|
|
"git.lastassault.de/speatzle/morffix/constants"
|
|
"git.lastassault.de/speatzle/morffix/types"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func downloadFile(ctx context.Context, l *slog.Logger, conf config.Config, path string, t *types.Task) error {
|
|
l.InfoContext(ctx, "Starting File Download", "task_id", t.ID, "file_id", t.FileID, "path", path, "md5", t.FileMD5)
|
|
|
|
out, err := os.Create(path)
|
|
if err != nil {
|
|
return fmt.Errorf("Creating File: %w", err)
|
|
}
|
|
defer out.Close()
|
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%v/files/%v", conf.Worker.Address, t.FileID), nil)
|
|
if err != nil {
|
|
return fmt.Errorf("New Request: %w", err)
|
|
}
|
|
|
|
uuid, err := uuid.Parse(conf.Worker.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("Cannot Parse ID: %w", err)
|
|
}
|
|
|
|
req.Header.Add(constants.SHARED_SECRET_HEADER, conf.SharedSecret)
|
|
req.Header.Add(constants.NAME_HEADER, conf.Worker.Name)
|
|
req.Header.Add(constants.UUID_HEADER, uuid.String())
|
|
req.Header.Add(constants.WORKER_VERSION_HEADER, constants.WORKER_VERSION)
|
|
|
|
req.Close = true
|
|
|
|
var client = &http.Client{
|
|
Transport: &http.Transport{},
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("Getting File: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("Got HTTP Status Code: %v", resp.StatusCode)
|
|
}
|
|
|
|
req.Close = true
|
|
|
|
// TODO Log at interval logs read
|
|
|
|
// Calculate hash and write file at the same time
|
|
hash := md5.New()
|
|
tr := io.TeeReader(resp.Body, hash)
|
|
|
|
cr := &countReader{Reader: tr}
|
|
|
|
stopProgress := make(chan bool, 1)
|
|
|
|
go func() {
|
|
tik := time.NewTicker(time.Second)
|
|
|
|
lastCount := 0
|
|
|
|
for {
|
|
select {
|
|
case <-tik.C:
|
|
speed := cr.n - lastCount
|
|
l.InfoContext(ctx, "Download Progress", "bytes", cr.n, "lastCount", lastCount, "length", resp.ContentLength, "speed", speed)
|
|
lastCount = cr.n
|
|
case <-stopProgress:
|
|
tik.Stop()
|
|
}
|
|
|
|
}
|
|
}()
|
|
|
|
defer func() {
|
|
stopProgress <- true
|
|
}()
|
|
|
|
defer resp.Body.Close()
|
|
n, err := io.Copy(out, cr)
|
|
if err != nil {
|
|
return fmt.Errorf("Reading File: %w", err)
|
|
}
|
|
|
|
md5 := hash.Sum(nil)
|
|
|
|
l.InfoContext(ctx, "Downloaded File", "bytes", n, "md5", md5)
|
|
|
|
if slices.Compare[[]byte](md5, t.FileMD5) != 0 {
|
|
return fmt.Errorf("Downloaded File does not match md5")
|
|
}
|
|
|
|
return nil
|
|
}
|