|
| 1 | +package cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "mime/multipart" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/zde37/pinata-go-sdk/pinata" |
| 12 | +) |
| 13 | + |
| 14 | +type PinataCloud struct { |
| 15 | + client *pinata.Client |
| 16 | + gatewayURL string |
| 17 | +} |
| 18 | + |
| 19 | +var _ Cloud = (*PinataCloud)(nil) |
| 20 | + |
| 21 | +func NewPinataCloud(jwt string, gatewayURL ...string) (*PinataCloud, error) { |
| 22 | + if jwt == "" { |
| 23 | + return nil, fmt.Errorf("pinata JWT cannot be empty") |
| 24 | + } |
| 25 | + |
| 26 | + auth := pinata.NewAuthWithJWT(jwt) |
| 27 | + client := pinata.New(auth) |
| 28 | + |
| 29 | + // ensure JWT is valid. |
| 30 | + if _, err := client.TestAuthentication(); err != nil { |
| 31 | + return nil, fmt.Errorf("pinata authentication failed: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + gwURL := "https://gateway.pinata.cloud" |
| 35 | + if len(gatewayURL) > 0 && gatewayURL[0] != "" { |
| 36 | + gwURL = gatewayURL[0] |
| 37 | + } |
| 38 | + |
| 39 | + return &PinataCloud{ |
| 40 | + client: client, |
| 41 | + gatewayURL: gwURL, |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +// Upload pins the given data to IPFS via Pinata and returns the content identifier (CID) as a Ref. |
| 46 | +func (p *PinataCloud) Upload(data []byte, contentType ...string) (Ref, error) { |
| 47 | + // We must use the file pinning endpoint to upload raw bytes without modification. |
| 48 | + // We build the multipart request body in memory to avoid writing a temporary file. |
| 49 | + body := &bytes.Buffer{} |
| 50 | + writer := multipart.NewWriter(body) |
| 51 | + |
| 52 | + // Create a form file part with a random name. |
| 53 | + // Pinata doesn't use this filename for the CID. |
| 54 | + part, err := writer.CreateFormFile("file", uuid.New().String()) |
| 55 | + if err != nil { |
| 56 | + return "", fmt.Errorf("failed to create form file: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + // Copy the raw byte data into the form file part. |
| 60 | + if _, err = io.Copy(part, bytes.NewReader(data)); err != nil { |
| 61 | + return "", fmt.Errorf( |
| 62 | + "failed to copy data to multipart writer: %w", |
| 63 | + err, |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + if err = writer.Close(); err != nil { |
| 68 | + return "", fmt.Errorf("failed to close multipart writer: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + // The Pinata SDK doesn't expose a method to pin raw bytes directly, |
| 72 | + // so we use the underlying requestBuilder to send the request. |
| 73 | + var response struct { |
| 74 | + IpfsHash string `json:"IpfsHash"` |
| 75 | + } |
| 76 | + |
| 77 | + err = p.client.NewRequest(http.MethodPost, "/pinning/pinFileToIPFS"). |
| 78 | + SetBody(body, writer.FormDataContentType()). |
| 79 | + Send(&response) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return "", fmt.Errorf("pinata API request failed: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + if response.IpfsHash == "" { |
| 86 | + return "", fmt.Errorf("pinata API response did not include an IpfsHash") |
| 87 | + } |
| 88 | + |
| 89 | + return Ref(response.IpfsHash), nil |
| 90 | +} |
| 91 | + |
| 92 | +// Read fetches the data from the IPFS gateway corresponding to the Ref (CID). |
| 93 | +func (p *PinataCloud) Read(ref Ref) ([]byte, error) { |
| 94 | + if ref == "" { |
| 95 | + return nil, fmt.Errorf("ref (CID) cannot be empty") |
| 96 | + } |
| 97 | + |
| 98 | + url := fmt.Sprintf("%s/ipfs/%s", p.gatewayURL, string(ref)) |
| 99 | + |
| 100 | + resp, err := http.Get(url) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("failed to fetch from IPFS gateway: %w", err) |
| 103 | + } |
| 104 | + defer func() { |
| 105 | + if closeErr := resp.Body.Close(); closeErr != nil { |
| 106 | + fmt.Printf("Warning: failed to close response body: %v\n", closeErr) |
| 107 | + } |
| 108 | + }() |
| 109 | + |
| 110 | + if resp.StatusCode != http.StatusOK { |
| 111 | + return nil, fmt.Errorf( |
| 112 | + "IPFS gateway returned non-200 status: %s", |
| 113 | + resp.Status, |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + data, err := io.ReadAll(resp.Body) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + return data, nil |
| 123 | +} |
0 commit comments