FCLauncher/fclauncher/https.go

37 lines
712 B
Go
Raw Normal View History

2024-10-24 17:05:47 -06:00
package main
import (
"context"
2024-10-24 17:05:47 -06:00
"io"
"net/http"
2024-11-26 15:49:44 -07:00
"strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
2024-10-24 17:05:47 -06:00
)
const BlockSize = 1024 * 64
2024-10-24 17:05:47 -06:00
func HttpDownload(path string, out io.Writer, ctx context.Context) error {
2024-11-26 15:49:44 -07:00
path = strings.ReplaceAll(path, "\\", "/")
2024-11-26 12:37:30 -07:00
res, err := http.Get("https://fclauncher.piwalker.net/fclauncher/" + path)
2024-10-24 17:05:47 -06:00
if err != nil {
return err
}
defer res.Body.Close()
var read int64 = 0
for read < res.ContentLength {
count, err := io.CopyN(out, res.Body, BlockSize)
read += count
if err != nil {
break
}
if ctx != nil {
runtime.EventsEmit(ctx, "download", read, res.ContentLength)
}
}
if ctx != nil {
runtime.EventsEmit(ctx, "download_complete")
2024-10-24 17:05:47 -06:00
}
return nil
}