|
- package efile
-
- import (
- "fmt"
- "net/http"
- "net/url"
- "path"
-
- "gitlink.org.cn/cloudream/common/utils/http2"
- "gitlink.org.cn/cloudream/common/utils/os2"
- "gitlink.org.cn/cloudream/common/utils/serder"
- stgtypes "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- )
-
- type ECMultiplier struct {
- blder *builder
- url string
- feat *jcstypes.ECMultiplierFeature
- outputs []string
- }
-
- // 进行EC运算,coef * inputs。coef为编码矩阵,inputs为待编码数据,chunkSize为分块大小。
- // 输出为每一个块文件的路径,数组长度 = len(coef)
- func (m *ECMultiplier) Multiply(coef [][]byte, inputs []stgtypes.HTTPRequest, chunkSize int) ([]stgtypes.FileInfo, error) {
- type Request struct {
- Inputs []stgtypes.HTTPRequest `json:"inputs"`
- Outputs []string `json:"outputs"`
- Coefs [][]int `json:"coefs"` // 用int防止被base64编码
- ChunkSize int `json:"chunkSize"`
- }
- type Response struct {
- Code string `json:"code"`
- Msg string `json:"msg"`
- Data []struct {
- Size int64 `json:"size"`
- Sha256 string `json:"sha256"`
- }
- }
-
- intCoefs := make([][]int, len(coef))
- for i := range intCoefs {
- intCoefs[i] = make([]int, len(coef[i]))
- for j := range intCoefs[i] {
- intCoefs[i][j] = int(coef[i][j])
- }
- }
-
- fileName := os2.GenerateRandomFileName(10)
- tempDir := path.Join(m.blder.detail.UserSpace.WorkingDir.String(), stgtypes.TempWorkingDir)
- m.outputs = make([]string, len(coef))
- for i := range m.outputs {
- m.outputs[i] = path.Join(tempDir, fmt.Sprintf("%s_%d", fileName, i))
- }
-
- u, err := url.JoinPath(m.url, "efile/openapi/v2/file/createECTask")
- if err != nil {
- return nil, err
- }
-
- token, err := m.blder.getToken()
- if err != nil {
- return nil, fmt.Errorf("get token: %w", err)
- }
-
- resp, err := http2.PostJSON(u, http2.RequestParam{
- Header: map[string]string{"token": token},
- Body: Request{
- Inputs: inputs,
- Outputs: m.outputs,
- Coefs: intCoefs,
- ChunkSize: chunkSize,
- },
- })
- if err != nil {
- return nil, err
- }
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("status code: %d", resp.StatusCode)
- }
-
- var r Response
- err = serder.JSONToObjectStream(resp.Body, &r)
- if err != nil {
- return nil, err
- }
-
- if r.Code != "0" {
- return nil, fmt.Errorf("code: %s, msg: %s", r.Code, r.Msg)
- }
-
- if len(r.Data) != len(m.outputs) {
- return nil, fmt.Errorf("data length not match outputs length")
- }
-
- ret := make([]stgtypes.FileInfo, len(r.Data))
- for i, data := range r.Data {
- ret[i] = stgtypes.FileInfo{
- // TODO 要确认一下output的格式
- Path: jcstypes.PathFromJcsPathString(m.outputs[i]),
- Size: data.Size,
- Hash: jcstypes.NewFullHashFromString(data.Sha256),
- }
- }
-
- return ret, nil
- }
-
- func (m *ECMultiplier) Close() {}
|