forked from kevacoin-project/keva_ipfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.go
199 lines (181 loc) · 4.13 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/checksum0/go-electrum/electrum"
"github.com/gin-gonic/gin"
)
func getPaymentInfo(c *gin.Context, paymentAddr string, minPayment float64) {
paymentInfo := PaymentInfo{
PaymentAddress: paymentAddr,
MinPayment: fmt.Sprintf("%f", minPayment),
}
c.JSON(200, &paymentInfo)
return
}
func uploadMedia(c *gin.Context) {
f, _, err := c.Request.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
"error": true,
})
return
}
defer f.Close()
tmpfile, err := ioutil.TempFile("", "CID")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Failed to create tmp file",
"error": true,
})
return
}
r := bufio.NewReader(f)
w := bufio.NewWriter(tmpfile)
buf := make([]byte, 1024)
for {
// read a chunk
n, err := r.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
// write a chunk
if _, err := w.Write(buf[:n]); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Error writing to tmp file",
"error": true,
})
return
}
}
if err = w.Flush(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Error flushing to tmp file",
"error": true,
})
return
}
// Get the CID of the tmp file.
tmpfileName := tmpfile.Name()
cid, err := exec.Command("ipfs", "add", "-n", tmpfileName).Output()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Failed to get CID",
"error": true,
})
return
}
reg := regexp.MustCompile(`added\s+([[:alnum:]]+)\s+`)
cidStr := string(reg.FindSubmatch(cid)[1])
err = os.Rename(tmpfileName, "/tmp/"+cidStr)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Failed to rename file",
"error": true,
})
return
}
c.JSON(200, gin.H{
"CID": cidStr,
})
return
}
func extractCID(keyStr string) string {
reg := regexp.MustCompile(`\{\{([[:ascii:]]+)\}\}`)
cidStr := string(reg.FindSubmatch([]byte(keyStr))[1])
cidAndMime := strings.Split(cidStr, "|")
return cidAndMime[0]
}
func publishMediaIPFS(server *electrum.Server, c *gin.Context, paymentAddr string, minPayment float64) {
var pinMedia PinMedia
c.BindJSON(&pinMedia)
tx, err := server.GetTransaction(pinMedia.Tx)
if err != nil {
var count = 0
// Try mutiple times - the transaction may not be in the mempool yet.
for (tx == nil) && (count < 6) {
time.Sleep(1 * time.Second)
tx, err = server.GetTransaction(pinMedia.Tx)
count++
}
}
if tx == nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Transaction",
"error": true,
})
return
}
paymentOK := false
for _, v := range tx.Vout {
if v.ScriptPubkey.Addresses[0] == paymentAddr {
if v.Value < minPayment {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Insuffcient payment",
"error": true,
})
return
}
paymentOK = true
break
}
}
if !paymentOK {
c.JSON(http.StatusBadRequest, gin.H{
"message": "No payment",
"error": true,
})
return
}
for _, v := range tx.Vout {
if strings.HasPrefix(v.ScriptPubkey.Asm, "OP_KEVA_PUT") {
items := strings.Split(v.ScriptPubkey.Asm, " ")
value := items[3]
dst := make([]byte, hex.DecodedLen(len(value)))
_, err := hex.Decode(dst, []byte(value))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "No payment",
"error": true,
})
return
}
cid := extractCID(string(dst))
cidFilename := "/tmp/" + cid
cidResult, err := exec.Command("ipfs", "add", cidFilename).Output()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Failed to get CID",
"error": true,
})
return
}
reg := regexp.MustCompile(`added\s+([[:alnum:]]+)\s+`)
cidStr := string(reg.FindSubmatch(cidResult)[1])
if cidStr != cid {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Incorrect CID",
"error": true,
})
return
}
}
}
c.JSON(200, gin.H{
"message": "OK",
})
return
}