120 lines
2.5 KiB
Go
120 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.algo.com.cn/public/saasapi"
|
|
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
|
)
|
|
|
|
type uploadTaskParams struct {
|
|
taskSha256 string
|
|
saasHttp *saashttp.SaasClient
|
|
}
|
|
|
|
func RunTaskUpload(args ...string) error {
|
|
fs := flag.NewFlagSet("upload", flag.ExitOnError)
|
|
cfgFile := paramConfig(fs)
|
|
sha256 := paramSha256(fs)
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
return fmt.Errorf("Command line parse error: %w", err)
|
|
}
|
|
|
|
if fs.NArg() > 0 || len(*sha256) == 0 {
|
|
fs.PrintDefaults()
|
|
return nil
|
|
}
|
|
|
|
cfg, err := LoadConfigFile(*cfgFile)
|
|
if err != nil {
|
|
return fmt.Errorf("LoadConfigFile error: %w", err)
|
|
}
|
|
|
|
uploadTaskParams := uploadTaskParams{
|
|
taskSha256: *sha256,
|
|
saasHttp: &saashttp.SaasClient{
|
|
Client: &http.Client{},
|
|
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
|
Auth: &cfg.Auth,
|
|
},
|
|
}
|
|
|
|
return doTaskUpload(uploadTaskParams)
|
|
}
|
|
|
|
func doTaskUpload(uploadTaskParams uploadTaskParams) error {
|
|
infoTaskParams := infoTaskParams{
|
|
taskSha256: uploadTaskParams.taskSha256,
|
|
saasHttp: uploadTaskParams.saasHttp,
|
|
}
|
|
taskInfo, err := doTaskInfo(infoTaskParams)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sourcePath := taskInfo.GetSourcePath()
|
|
totalFiles := len(taskInfo.GetTaskFileInfos())
|
|
fi := 0
|
|
for _, finfo := range taskInfo.GetTaskFileInfos() {
|
|
fi++
|
|
var f *os.File
|
|
offset := int64(0)
|
|
totalBlocks := len(finfo.GetFileBlocks())
|
|
bi := 0
|
|
for _, binfo := range finfo.GetFileBlocks() {
|
|
bi++
|
|
if !binfo.GetUploaded() {
|
|
if f == nil {
|
|
fname := finfo.GetFileName()
|
|
if len(sourcePath) > 0 {
|
|
fname = filepath.Join(sourcePath, fname)
|
|
}
|
|
f, err = os.Open(fname)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
blockRes, err := uploadTaskParams.saasHttp.TaskUpload(
|
|
binfo.GetBlockSha256(),
|
|
f,
|
|
offset,
|
|
int(binfo.GetBlockLength()),
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if blockRes.GetCode() != saasapi.ErrorCode_SUCC {
|
|
return fmt.Errorf("upload block error, code %d, msg %s", blockRes.GetCode(), blockRes.GetStatus())
|
|
} else {
|
|
fmt.Printf("upload block success. file: %v, sha256 %v. block %v/%v, file %v/%v\n",
|
|
finfo.GetFileName(), binfo.GetBlockSha256(),
|
|
bi, totalBlocks, fi, totalFiles,
|
|
)
|
|
}
|
|
|
|
} else {
|
|
fmt.Printf("uploaded block. file: %v, sha256 %v. block %v/%v, file %v/%v\n",
|
|
finfo.GetFileName(), binfo.GetBlockSha256(),
|
|
bi, totalBlocks, fi, totalFiles,
|
|
)
|
|
}
|
|
offset += int64(binfo.GetBlockLength())
|
|
|
|
}
|
|
|
|
if f != nil {
|
|
f.Close()
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|