ipfs-node-pin/main.go

133 lines
2.9 KiB
Go
Raw Normal View History

2024-01-04 19:36:26 +00:00
package main
import (
2024-01-06 12:51:26 +00:00
"encoding/json"
"fmt"
"io"
"io/fs"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"github.com/sethvargo/go-githubactions"
2024-01-04 19:36:26 +00:00
)
2024-01-06 12:51:26 +00:00
type AddResponse struct {
Bytes int64
Hash string
Name string
Size string
}
2024-01-04 19:36:26 +00:00
func main() {
2024-01-06 18:54:00 +00:00
githubactions.Infof("Checking inputs...")
2024-01-06 12:51:26 +00:00
// Check inputs
path := githubactions.GetInput("path_to_add")
if path == "" {
githubactions.Fatalf("Missing: path_to_add")
}
ipfsHost := githubactions.GetInput("ipfs_host")
if ipfsHost == "" {
githubactions.Fatalf("Missing: ipfs_host")
}
ipfsPort := githubactions.GetInput("ipfs_port")
if ipfsPort == "" {
githubactions.Fatalf("Missing: ipfs_port")
}
targetPath, err := os.Open(path)
if err != nil {
githubactions.Fatalf("Unable to access path_to_add: %v", err.Error())
}
defer targetPath.Close()
targetPathInfo, err := targetPath.Stat()
if err != nil {
githubactions.Fatalf("Unable to access to access path_to_add info: %v", fmt.Errorf("%w", err))
}
if !targetPathInfo.IsDir() {
githubactions.Fatalf("%v is not a directory", path)
}
2024-01-06 18:54:00 +00:00
githubactions.Infof("Inputs OK")
2024-01-06 12:51:26 +00:00
body, writer := io.Pipe()
2024-01-06 18:58:18 +00:00
url := fmt.Sprintf("http://%v:%v/api/v0/add", ipfsHost, ipfsPort)
req, err := http.NewRequest(http.MethodPost, url, body)
2024-01-06 12:51:26 +00:00
if err != nil {
githubactions.Fatalf("Unable to create request: %v", err.Error())
}
2024-01-06 20:36:09 +00:00
mwriter := multipart.NewWriter(writer)
req.Header.Add("Content-Type", mwriter.FormDataContentType())
2024-01-06 12:51:26 +00:00
2024-01-06 20:49:47 +00:00
go func() {
2024-01-06 21:17:31 +00:00
defer mwriter.Close()
defer writer.Close()
2024-01-06 20:49:47 +00:00
err = filepath.Walk(path, func(innerPath string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
2024-01-06 21:43:35 +00:00
relativeParh, _ := filepath.Rel(path, innerPath)
2024-01-06 20:49:47 +00:00
githubactions.Infof("Reading %v", relativeParh)
w, err := mwriter.CreateFormFile("file", relativeParh)
if err != nil {
return err
}
2024-01-07 11:58:46 +00:00
fileReader, err := os.Open(innerPath)
2024-01-06 20:49:47 +00:00
if err != nil {
return err
}
2024-01-07 11:58:46 +00:00
defer fileReader.Close()
2024-01-06 20:49:47 +00:00
2024-01-07 11:58:46 +00:00
written, err := io.Copy(w, fileReader)
if err != nil {
2024-01-06 20:49:47 +00:00
return fmt.Errorf("error copying %s (%d bytes written): %v", relativeParh, written, err)
2024-01-07 11:58:46 +00:00
} else {
githubactions.Infof("Read %v bytes from %s", written, relativeParh)
2024-01-06 20:49:47 +00:00
}
return nil
})
2024-01-06 20:05:26 +00:00
2024-01-06 20:36:09 +00:00
if err != nil {
2024-01-06 20:57:37 +00:00
githubactions.Fatalf("Unable to create request body: %v", fmt.Errorf("%w", err))
}
2024-01-06 20:49:47 +00:00
}()
2024-01-06 12:51:26 +00:00
2024-01-06 18:54:00 +00:00
githubactions.Infof("Calling node API")
2024-01-06 12:51:26 +00:00
client := &http.Client{}
res, err := client.Do(req)
2024-01-06 18:46:27 +00:00
if err != nil {
githubactions.Fatalf(err.Error())
}
2024-01-06 12:51:26 +00:00
2024-01-06 18:54:00 +00:00
githubactions.Infof("Reading response")
2024-01-06 12:51:26 +00:00
resBody, err := io.ReadAll(res.Body)
if err != nil {
2024-01-06 18:46:27 +00:00
githubactions.Fatalf(err.Error())
2024-01-06 12:51:26 +00:00
}
2024-01-07 12:06:21 +00:00
githubactions.Infof("Response: %v", string(resBody))
2024-01-07 11:58:46 +00:00
2024-01-06 12:51:26 +00:00
var ipfsAddResponse AddResponse
json.Unmarshal(resBody, &ipfsAddResponse)
2024-01-06 18:54:00 +00:00
githubactions.Infof("Upload size is %v", ipfsAddResponse.Bytes)
githubactions.Infof("CID is %v", ipfsAddResponse.Hash)
2024-01-06 12:54:39 +00:00
githubactions.SetOutput("cid", ipfsAddResponse.Hash)
2024-01-04 19:36:26 +00:00
}