Transcode File Uploading
This commit is contained in:
parent
f232e08945
commit
36fe3202e8
7 changed files with 248 additions and 4 deletions
|
@ -2,12 +2,103 @@ package task
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/config"
|
||||
"git.lastassault.de/speatzle/morffix/constants"
|
||||
"git.lastassault.de/speatzle/morffix/types"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func uploadFile(ctx context.Context, l *slog.Logger, address string, path string, t *types.Task) error {
|
||||
return fmt.Errorf("File Upload not Implemented")
|
||||
func uploadFile(ctx context.Context, l *slog.Logger, conf config.Config, path string, t *types.Task, hash []byte) error {
|
||||
l.InfoContext(ctx, "Starting File Upload", "task_id", t.ID, "file_id", t.FileID, "path", path, "hash", hash)
|
||||
|
||||
uuid, err := uuid.Parse(conf.Worker.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot Parse ID: %w", err)
|
||||
}
|
||||
|
||||
r, w := io.Pipe()
|
||||
m := multipart.NewWriter(w)
|
||||
go func() {
|
||||
defer w.Close()
|
||||
defer m.Close()
|
||||
part, err := m.CreateFormFile(constants.FORM_FILE_KEY, path)
|
||||
if err != nil {
|
||||
l.ErrorContext(ctx, "Creating Form File", "err", err)
|
||||
return
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
l.ErrorContext(ctx, "Opening File", "err", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
if _, err = io.Copy(part, file); err != nil {
|
||||
l.ErrorContext(ctx, "Copy Form File", "err", err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
cr := &countReader{Reader: r}
|
||||
|
||||
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, "Upload Progress", "bytes", cr.n, "lastCount", lastCount, "speed", speed)
|
||||
lastCount = cr.n
|
||||
case <-stopProgress:
|
||||
tik.Stop()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
stopProgress <- true
|
||||
}()
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%v/upload/%v", conf.Worker.Address, t.FileID), cr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("New Request: %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.Header.Add(constants.HASH_HEADER, base64.StdEncoding.EncodeToString(hash))
|
||||
req.Header.Add("Content-Type", "multipart/form-data; boundary=\""+m.Boundary()+"\"")
|
||||
|
||||
var client = &http.Client{
|
||||
Transport: &http.Transport{},
|
||||
}
|
||||
|
||||
l.InfoContext(ctx, "Starting Upload")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Uploading File: %w", err)
|
||||
}
|
||||
|
||||
l.InfoContext(ctx, "Upload Done")
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("Got HTTP Status Code: %v", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue