Make Healthcheck download and ffmpeg reusable
This commit is contained in:
parent
249a415cee
commit
fea955fb79
3 changed files with 202 additions and 173 deletions
94
task/download.go
Normal file
94
task/download.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/types"
|
||||
)
|
||||
|
||||
func downloadFile(ctx context.Context, l *slog.Logger, address string, 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", address, t.FileID), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("New Request: %w", err)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue