1
0
mirror of https://github.com/fumiama/RVC-Models-Downloader.git synced 2024-11-17 21:57:10 +09:00
RVC-Models-Downloader/net.go

111 lines
3.1 KiB
Go
Raw Permalink Normal View History

2024-04-19 00:29:30 +09:00
package main
import (
"fmt"
"io"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/fumiama/terasu/http2"
"github.com/pkg/errors"
)
2024-04-19 01:25:10 +09:00
func (c *config) download(path, prefix string, waits time.Duration, usecust, usetrs, force bool) error {
2024-04-19 00:29:30 +09:00
for i, t := range c.Targets {
if t.Refer != "" {
refp := path[:strings.LastIndex(path, "/")+1] + t.Refer
2024-04-20 17:44:18 +09:00
infof("#%s%d refer to target '%s'.", prefix, i+1, refp)
2024-04-19 00:29:30 +09:00
refcfg, err := readconfig(refp, usecust)
if err != nil {
return err
}
2024-04-19 01:25:10 +09:00
err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", waits, usecust, usetrs, force)
2024-04-19 00:29:30 +09:00
if err != nil {
return err
}
continue
}
if t.OS != "" && t.OS != runtime.GOOS {
2024-04-20 17:44:18 +09:00
warnf("#%s%d target required OS: %s but you are %s, skip.", prefix, i+1, t.OS, runtime.GOOS)
2024-04-19 00:29:30 +09:00
continue
}
if t.Arch != "" && t.Arch != runtime.GOARCH {
2024-04-20 17:44:18 +09:00
warnf("#%s%d target required Arch: %s but you are %s, skip.", prefix, i+1, t.Arch, runtime.GOARCH)
2024-04-19 00:29:30 +09:00
continue
}
err := os.MkdirAll(t.Folder, 0755)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("#%s%d make target folder '%s'", prefix, i+1, t.Folder))
}
2024-04-20 17:44:18 +09:00
infof("#%s%d open target folder '%s'.", prefix, i+1, t.Folder)
2024-04-19 00:29:30 +09:00
if len(t.Copy) == 0 {
2024-04-20 17:44:18 +09:00
warnf("#%s%d empty copy target.", prefix, i+1)
2024-04-19 00:29:30 +09:00
continue
}
wg := sync.WaitGroup{}
wg.Add(len(t.Copy))
2024-04-20 17:44:18 +09:00
infof("#%s%d download copy: '%v'.", prefix, i+1, t.Copy)
2024-04-19 00:29:30 +09:00
for j, cp := range t.Copy {
go func(i int, cp, prefix string) {
defer wg.Done()
2024-04-19 01:25:10 +09:00
sleep := waits * time.Duration(i)
2024-04-19 00:29:30 +09:00
if sleep > time.Millisecond {
time.Sleep(sleep)
}
2024-04-19 01:25:10 +09:00
fname := t.Folder + "/" + cp[strings.LastIndex(cp, "/")+1:]
2024-04-19 00:29:30 +09:00
if !force {
if _, err := os.Stat(fname); err == nil || os.IsExist(err) {
2024-04-20 17:44:18 +09:00
warnf("#%s%d skip exist file %s", prefix, i+1, fname)
2024-04-19 00:29:30 +09:00
return
}
}
req, err := http.NewRequest("GET", c.BaseURL+"/"+cp, nil)
if err != nil {
2024-04-20 17:44:18 +09:00
errorf("#%s%d new request to %s err: %v", prefix, i+1, cp, err)
2024-04-19 00:29:30 +09:00
return
}
2024-04-20 17:44:18 +09:00
infof("#%s%d get: %s", prefix, i+1, req.URL)
2024-04-19 00:29:30 +09:00
req.Header.Add("user-agent", ua)
var resp *http.Response
if usetrs {
resp, err = http2.DefaultClient.Do(req)
} else {
resp, err = http.DefaultClient.Do(req)
}
if err != nil {
2024-04-20 17:44:18 +09:00
errorf("#%s%d get %s err: %v", prefix, i+1, req.URL, err)
2024-04-19 00:29:30 +09:00
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err := errors.New(fmt.Sprintf("HTTP %d %s", resp.StatusCode, resp.Status))
2024-04-20 17:44:18 +09:00
errorf("#%s%d get %s err: %v", prefix, i+1, req.URL, err)
2024-04-19 00:29:30 +09:00
return
}
f, err := os.Create(fname)
if err != nil {
2024-04-20 17:44:18 +09:00
errorf("#%s%d create file %s err: %v", prefix, i+1, fname, err)
2024-04-19 00:29:30 +09:00
return
}
defer f.Close()
2024-04-20 17:44:18 +09:00
infof("#%s%d writing file %s", prefix, i+1, fname)
2024-04-19 00:29:30 +09:00
pm := newmeter(fmt.Sprintf("#%s%d", prefix, i+1), fname, int(resp.ContentLength))
2024-04-20 17:44:18 +09:00
defer pm.finish()
2024-04-19 00:29:30 +09:00
_, err = io.Copy(io.MultiWriter(f, &pm), resp.Body)
if err != nil {
2024-04-20 17:44:18 +09:00
errorf("#%s%d download file %s err: %v", prefix, i+1, fname, err)
2024-04-19 00:29:30 +09:00
return
}
2024-04-20 17:44:18 +09:00
infof("#%s%d finished download %s", prefix, i+1, fname)
2024-04-19 00:29:30 +09:00
}(j, cp, fmt.Sprintf("%s%d.", prefix, i+1))
}
wg.Wait()
}
return nil
}