/
action_ipfs_bak.go
141 lines (140 loc) · 3.44 KB
/
action_ipfs_bak.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package action
//
//import (
// "bytes"
// "context"
// "encoding/json"
// "errors"
// "fmt"
// "github.com/hamster-shared/aline-engine/consts"
// model2 "github.com/hamster-shared/aline-engine/model"
// "github.com/hamster-shared/aline-engine/output"
// "github.com/hamster-shared/aline-engine/utils"
// "io"
// "mime/multipart"
// "net/http"
// "os"
// path2 "path"
//)
//
//// IpfsAction Upload files/directories to ipfs
//type IpfsAction struct {
// path string
// output *output.Output
// ctx context.Context
//}
//
//func NewIpfsAction(step model2.Step, ctx context.Context, output *output.Output) *IpfsAction {
// return &IpfsAction{
// path: step.With["path"],
// ctx: ctx,
// output: output,
// }
//}
//
//func (a *IpfsAction) Pre() error {
// return nil
//}
//
//func (a *IpfsAction) Hook() (*model2.ActionResult, error) {
//
// stack := a.ctx.Value(STACK).(map[string]interface{})
//
// workdir, ok := stack["workdir"].(string)
// if !ok {
// return nil, errors.New("get workdir error")
// }
//
// path := path2.Join(workdir, a.path)
// fmt.Println(path)
// fi, err := os.Stat(path)
// if err != nil {
// return nil, errors.New(fmt.Sprintf("get path fail, err is %s", err.Error()))
// }
// isDir := fi.IsDir()
// var dstPath string
// if isDir {
// dstPath = path + ".car"
// _, err := os.Stat(dstPath)
// if err == nil {
// os.Remove(dstPath)
// }
// car, err := utils.CreateCar(a.ctx, path, dstPath, consts.CarVersion)
// if err != nil {
// return nil, errors.New("dir to car fail")
// }
// fmt.Println(fmt.Sprintf("%s 的ipfs hash 是 %s", path, car))
// } else {
// dstPath = path
// }
//
// _, file := path2.Split(dstPath)
// body := new(bytes.Buffer)
// writer := multipart.NewWriter(body)
// formFile, err := writer.CreateFormFile("file", file)
// if err != nil {
// return nil, err
// }
// open, err := os.Open(dstPath)
// if err != nil {
// return nil, errors.New("dstPath open fail")
// }
// _, err = io.Copy(formFile, open)
// if err != nil {
// return nil, err
// }
// err = writer.Close()
// if err != nil {
// return nil, err
// }
// client := http.Client{}
// req, err := http.NewRequest("POST", consts.IpfsUploadUrl, body)
// if err != nil {
// return nil, err
// }
// req.Header.Add("Content-Type", writer.FormDataContentType())
// resp, err := client.Do(req)
// if err != nil {
// return nil, err
// }
// defer resp.Body.Close()
// bodyText, err := io.ReadAll(resp.Body)
// if err != nil {
// return nil, err
// }
// fmt.Printf("收到的req是 %s\n", bodyText)
// var ipfsGatewayCloudReq IpfsGatewayCloudReq
// err = json.Unmarshal(bodyText, &ipfsGatewayCloudReq)
// if err != nil {
// return nil, errors.New("req json unmarshal fail")
// }
// actionResult := model2.ActionResult{
// Artifactorys: []model2.Artifactory{
// {
// Name: a.path,
// Url: ipfsGatewayCloudReq.Url,
// },
// },
// Reports: nil,
// }
// fmt.Println(actionResult)
// return &actionResult, nil
//}
//
//func (a *IpfsAction) Post() error {
// return nil
//}
//
//type IpfsGatewayCloudReq struct {
// UploadID string `json:"uploadID"`
// UploadFileType string `json:"upload_file_type"`
// UploadType string `json:"upload_type"`
// Cid string `json:"cid"`
// Filename string `json:"filename"`
// ContentType string `json:"content_type"`
// Size int `json:"size"`
// Url string `json:"url"`
// Status string `json:"status"`
// Pin string `json:"pin"`
// Dht string `json:"dht"`
//}