Alexandre
d2824ce7e4
All checks were successful
Create and publish a Docker image / build-and-push-image (push) Successful in 55s
128 lines
2.7 KiB
Go
128 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/sethvargo/go-githubactions"
|
|
)
|
|
|
|
type AddResponse struct {
|
|
Bytes int64
|
|
Hash string
|
|
Name string
|
|
Size string
|
|
}
|
|
|
|
func main() {
|
|
githubactions.Infof("Checking inputs...")
|
|
|
|
// 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)
|
|
}
|
|
|
|
githubactions.Infof("Inputs OK")
|
|
|
|
body, writer := io.Pipe()
|
|
|
|
// TODO: URL
|
|
req, err := http.NewRequest(http.MethodPost, "ipfs.narwhal-frog.ts.net", body)
|
|
if err != nil {
|
|
githubactions.Fatalf("Unable to create request: %v", err.Error())
|
|
}
|
|
|
|
go func() {
|
|
defer writer.Close()
|
|
|
|
mwriter := multipart.NewWriter(writer)
|
|
req.Header.Add("Content-Type", mwriter.FormDataContentType())
|
|
|
|
err = filepath.Walk(path, func(innerPath string, info fs.FileInfo, err error) error {
|
|
githubactions.Infof("Reading %v", innerPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
relativeParh := strings.TrimPrefix(innerPath, path)
|
|
w, err := mwriter.CreateFormFile("file", relativeParh)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
in, err := os.Open(innerPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer in.Close()
|
|
|
|
if written, err := io.Copy(w, in); err != nil {
|
|
return fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err)
|
|
}
|
|
|
|
if err := mwriter.Close(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
githubactions.Fatalf("Unable to create request body : %v", fmt.Errorf("%w", err))
|
|
}
|
|
}()
|
|
|
|
githubactions.Infof("Calling node API")
|
|
client := &http.Client{}
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
githubactions.Fatalf(err.Error())
|
|
}
|
|
|
|
githubactions.Infof("Reading response")
|
|
resBody, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
githubactions.Fatalf(err.Error())
|
|
}
|
|
|
|
var ipfsAddResponse AddResponse
|
|
json.Unmarshal(resBody, &ipfsAddResponse)
|
|
|
|
githubactions.Infof("Upload size is %v", ipfsAddResponse.Bytes)
|
|
githubactions.Infof("CID is %v", ipfsAddResponse.Hash)
|
|
githubactions.SetOutput("cid", ipfsAddResponse.Hash)
|
|
}
|