Compare commits

...

4 Commits

Author SHA1 Message Date
CrispyBaguette
8fd01e2798 output CID
All checks were successful
Create and publish a Docker image / build-and-push-image (push) Successful in 55s
2024-01-08 07:53:00 +01:00
CrispyBaguette
91812ef33c Reduce verbosity 2024-01-08 07:42:36 +01:00
CrispyBaguette
f0094b1a7a Stop wrapping 2024-01-08 07:40:04 +01:00
CrispyBaguette
cc34779294 Move query escaping to multipartwriter 2024-01-08 07:37:17 +01:00

39
main.go
View File

@ -41,30 +41,32 @@ func NewIpfsMultipartWriter(w io.Writer) *IpfsMultipartWriter {
func (w *IpfsMultipartWriter) CreateIpfsDirectoryPart(name string) (io.Writer, error) { func (w *IpfsMultipartWriter) CreateIpfsDirectoryPart(name string) (io.Writer, error) {
h := make(textproto.MIMEHeader) h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, name)) encodedName := url.QueryEscape(name)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, encodedName))
h.Set("Content-Type", "application/x-directory") h.Set("Content-Type", "application/x-directory")
return w.CreatePart(h) return w.CreatePart(h)
} }
func (w *IpfsMultipartWriter) CreateIpfsFilePart(name string) (io.Writer, error) { func (w *IpfsMultipartWriter) CreateIpfsFilePart(name string) (io.Writer, error) {
h := make(textproto.MIMEHeader) h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(name))) encodedName := url.QueryEscape(name)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(encodedName)))
h.Set("Content-Type", "application/octet-stream") h.Set("Content-Type", "application/octet-stream")
return w.CreatePart(h) return w.CreatePart(h)
} }
func (w *IpfsMultipartWriter) CreateIpfsAbsFilePart(name, absPath string) (io.Writer, error) { func (w *IpfsMultipartWriter) CreateIpfsAbsFilePart(name, absPath string) (io.Writer, error) {
h := make(textproto.MIMEHeader) h := make(textproto.MIMEHeader)
encodedName := url.QueryEscape(name)
h.Set("AbsPath", absPath) h.Set("AbsPath", absPath)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(name))) h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, escapeQuotes(encodedName)))
h.Set("Content-Type", "application/octet-stream") h.Set("Content-Type", "application/octet-stream")
return w.CreatePart(h) return w.CreatePart(h)
} }
func main() { func main() {
githubactions.Infof("Checking inputs...") githubactions.Debugf("Checking inputs")
// Check inputs
path := githubactions.GetInput("path_to_add") path := githubactions.GetInput("path_to_add")
if path == "" { if path == "" {
githubactions.Fatalf("Missing: path_to_add") githubactions.Fatalf("Missing: path_to_add")
@ -95,7 +97,7 @@ func main() {
githubactions.Fatalf("%v is not a directory", path) githubactions.Fatalf("%v is not a directory", path)
} }
githubactions.Infof("Inputs OK") githubactions.Debugf("Inputs OK")
body, writer := io.Pipe() body, writer := io.Pipe()
@ -106,7 +108,6 @@ func main() {
} }
q := req.URL.Query() q := req.URL.Query()
q.Add("wrap-with-directory", "true")
q.Add("progress", "false") q.Add("progress", "false")
q.Add("stream-true", "false") q.Add("stream-true", "false")
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
@ -127,8 +128,7 @@ func main() {
return nil return nil
} }
relPath, _ := filepath.Rel(path, innerPath) w, err := mwriter.CreateIpfsFilePart(innerPath)
w, err := mwriter.CreateIpfsFilePart(url.QueryEscape(fmt.Sprintf("/%v", relPath)))
if err != nil { if err != nil {
return err return err
} }
@ -152,28 +152,37 @@ func main() {
} }
}() }()
githubactions.Infof("Calling node API...") githubactions.Debugf("Calling node API...")
client := &http.Client{} client := &http.Client{}
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
githubactions.Fatalf(err.Error()) githubactions.Fatalf(err.Error())
} }
githubactions.Infof("Reading response...") githubactions.Debugf("Reading response...")
resBody, err := io.ReadAll(res.Body) resBody, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
githubactions.Fatalf(err.Error()) githubactions.Fatalf(err.Error())
} }
githubactions.Infof("Response: %v", string(resBody)) githubactions.Debugf("Response: %v", string(resBody))
d := json.NewDecoder(res.Body) d := json.NewDecoder(res.Body)
addResponses := make([]*AddResponse, 0)
for { for {
var ipfsAddResponse []AddResponse var addResponse AddResponse
if err := d.Decode(&ipfsAddResponse); err == io.EOF { if err := d.Decode(&addResponse); err == io.EOF {
break break
} else if err != nil { } else if err != nil {
githubactions.Warningf("Failed to unmarshal response: %v", fmt.Errorf("%w", err)) githubactions.Warningf("Failed to unmarshal response: %v", fmt.Errorf("%w", err))
} }
githubactions.Infof("Received response: %v", ipfsAddResponse) githubactions.Debugf("Unmarshaled response: %v", addResponse)
addResponses = append(addResponses, &addResponse)
}
for _, addResponse := range addResponses {
if (*addResponse).Name == path {
githubactions.SetOutput("cid", addResponse.Hash)
break
}
} }
} }