|
| 1 | +package arweave |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "mime/multipart" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + apiBaseURL string |
| 18 | +) |
| 19 | + |
| 20 | +var ArweaveCmd = &cobra.Command{ |
| 21 | + Use: "arweave", |
| 22 | + Short: "🕸️ Interact with Arweave", |
| 23 | +} |
| 24 | + |
| 25 | +func init() { |
| 26 | + ArweaveCmd.AddCommand(arweaveUploadCommand()) |
| 27 | + ArweaveCmd.AddCommand(arweaveDownloadCommand()) |
| 28 | +} |
| 29 | + |
| 30 | +func arweaveUploadCommand() *cobra.Command { |
| 31 | + var filePath string |
| 32 | + |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "upload", |
| 35 | + Short: "Upload a file to Arweave through a relay API", |
| 36 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 37 | + if filePath == "" { |
| 38 | + return fmt.Errorf("❌ you must provide a file path with --file") |
| 39 | + } |
| 40 | + |
| 41 | + file, err := os.Open(filePath) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("cannot open file: %w", err) |
| 44 | + } |
| 45 | + defer file.Close() |
| 46 | + |
| 47 | + var buf bytes.Buffer |
| 48 | + writer := multipart.NewWriter(&buf) |
| 49 | + part, err := writer.CreateFormFile("file", filepath.Base(filePath)) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("cannot create form file: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + if _, errCopy := io.Copy(part, file); errCopy != nil { |
| 55 | + return fmt.Errorf("failed to copy file data: %w", errCopy) |
| 56 | + } |
| 57 | + |
| 58 | + if errClose := writer.Close(); errClose != nil { |
| 59 | + return fmt.Errorf("failed to close multipart writer: %w", errClose) |
| 60 | + } |
| 61 | + |
| 62 | + resp, err := http.Post(apiBaseURL+"/upload", writer.FormDataContentType(), &buf) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("upload request failed: %w", err) |
| 65 | + } |
| 66 | + defer resp.Body.Close() |
| 67 | + |
| 68 | + body, _ := io.ReadAll(resp.Body) |
| 69 | + if resp.StatusCode != http.StatusOK { |
| 70 | + return fmt.Errorf("arweave upload failed: %s", string(body)) |
| 71 | + } |
| 72 | + |
| 73 | + var res struct { |
| 74 | + ArweaveID string `json:"arweaveId"` |
| 75 | + URL string `json:"url"` |
| 76 | + } |
| 77 | + if err := json.Unmarshal(body, &res); err != nil { |
| 78 | + return fmt.Errorf("invalid response: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + fmt.Println("✅ Upload successful") |
| 82 | + fmt.Printf("🆔 Arweave ID: %s\n🔗 URL: %s\n", res.ArweaveID, res.URL) |
| 83 | + return nil |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the local file to upload") |
| 88 | + cmd.Flags().StringVarP(&apiBaseURL, "api", "a", "http://localhost:3000", "Base URL of the Arweave relay API (without /upload)") |
| 89 | + |
| 90 | + return cmd |
| 91 | +} |
| 92 | + |
| 93 | +func arweaveDownloadCommand() *cobra.Command { |
| 94 | + var id string |
| 95 | + var output string |
| 96 | + |
| 97 | + cmd := &cobra.Command{ |
| 98 | + Use: "download", |
| 99 | + Short: "Download a file from Arweave using its transaction ID", |
| 100 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 101 | + if id == "" { |
| 102 | + return fmt.Errorf("❌ you must provide an Arweave transaction ID with --id") |
| 103 | + } |
| 104 | + if output == "" { |
| 105 | + output = id |
| 106 | + } |
| 107 | + |
| 108 | + url := fmt.Sprintf("https://arweave.net/%s", id) |
| 109 | + |
| 110 | + fmt.Printf("🔗 Downloading from: %s\n📁 Saving to: %s\n", url, output) |
| 111 | + // #nosec G107 -- ID is validated and used to build a known domain URL |
| 112 | + resp, err := http.Get(url) |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("failed to download from Arweave: %w", err) |
| 115 | + } |
| 116 | + defer resp.Body.Close() |
| 117 | + |
| 118 | + if resp.StatusCode != http.StatusOK { |
| 119 | + body, _ := io.ReadAll(resp.Body) |
| 120 | + return fmt.Errorf("arweave download failed: %s", string(body)) |
| 121 | + } |
| 122 | + |
| 123 | + outFile, err := os.Create(output) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("cannot create output file: %w", err) |
| 126 | + } |
| 127 | + defer outFile.Close() |
| 128 | + |
| 129 | + if _, err := io.Copy(outFile, resp.Body); err != nil { |
| 130 | + return fmt.Errorf("error saving file: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + fmt.Println("✅ Download complete") |
| 134 | + return nil |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + cmd.Flags().StringVarP(&id, "id", "i", "", "Arweave transaction ID to download (required)") |
| 139 | + cmd.Flags().StringVarP(&output, "out", "o", "", "Output file path (optional)") |
| 140 | + |
| 141 | + return cmd |
| 142 | +} |
0 commit comments