-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
165 lines (147 loc) · 4.68 KB
/
handler.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
package main
import (
"crypto/ed25519"
"encoding/base64"
"fmt"
"math/rand"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/tonbyte/provider-node/config"
"github.com/tonbyte/provider-node/datatype"
"github.com/tonbyte/provider-node/storage"
"github.com/xssnick/tonutils-go/tvm/cell"
)
type handler struct {
pub ed25519.PublicKey
priv ed25519.PrivateKey
payload map[string]datatype.Payload
}
func newHandler(pub ed25519.PublicKey, priv ed25519.PrivateKey) *handler {
h := handler{
pub: pub,
priv: priv,
payload: make(map[string]datatype.Payload),
}
go h.worker()
return &h
}
func (h *handler) worker() {
for {
<-time.NewTimer(time.Minute).C
for k, v := range h.payload {
if time.Now().Unix() > v.ExpirtionTime {
delete(h.payload, k)
}
}
}
}
func (h *handler) Status(c echo.Context) error {
log.Info("/status")
providerInfo, err := storage.GetProviderInfo()
if err != nil {
return c.JSON(http.StatusInternalServerError, "can not get provider info")
}
providerParams, err := storage.GetProviderParams()
if err != nil {
return c.JSON(http.StatusInternalServerError, "can not get provider params")
}
result := datatype.Status{
Version: Version,
RatePerMbDay: providerParams.RatePerMbDay,
Span: providerParams.MaxSpan,
MinFileSize: providerParams.MinimalFileSize,
MaxFileSize: providerParams.MaximalFileSize,
ContractsCount: providerInfo.ContractsCount,
MaxContractsCount: providerInfo.ProviderConfig.MaxContracts,
UsedSpace: providerInfo.ContractsTotalSize,
MaxTotalSize: providerInfo.ProviderConfig.MaxTotalSize,
ContractAddress: config.StorageConfig.ContractAddress,
HasGateway: config.StorageConfig.HasGateway,
}
return c.JSON(http.StatusOK, echo.Map{
"result": result,
})
}
func (h *handler) UploadFile(c echo.Context) error {
log.Info("/uploadFile")
providerContractAddress := c.QueryParam("contract_address")
// Do file checks.
form, err := c.MultipartForm()
if err != nil {
return c.JSON(http.StatusBadRequest, "invalid form-data")
}
formDataFile := form.File["file"]
if len(formDataFile) < 1 {
return c.JSON(http.StatusBadRequest, "no files")
}
providerInfo, err := storage.GetProviderInfo()
if err != nil {
return c.JSON(http.StatusInternalServerError, "server error")
}
maxSize, err := strconv.ParseInt(providerInfo.ProviderConfig.MaxTotalSize, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, "server error")
}
usedSize, err := strconv.ParseInt(providerInfo.ContractsTotalSize, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, "server error")
}
spaceLeft := maxSize - usedSize
if spaceLeft < formDataFile[0].Size {
return c.JSON(http.StatusBadRequest, "no space left")
}
// Save file.
// TODO: timer to check and delete file if no contract created.
storagePath := filepath.Join(config.StorageConfig.StorageDBPath, "contracts-storage")
os.Mkdir(storagePath, 0750)
fileInfo, err := saveFile(storagePath, spaceLeft, formDataFile)
if err != nil {
log.Warn(err.Error())
os.Remove(fileInfo.FullPath)
return c.JSON(http.StatusBadRequest, "can not save file")
}
// Add file to storage.
newBagId := storage.CreateBagOutput(fileInfo.FullPath)
if len(newBagId) != 64 || strings.ContainsAny(newBagId, "[]:-!?,.") {
os.Remove(fileInfo.FullPath)
return c.JSON(http.StatusBadRequest, "can not create bag")
}
// Generate storage contract message.
rand.Seed(time.Now().UnixNano())
queryID := fmt.Sprint(rand.Uint32())
messagesFolder := filepath.Join(config.StorageConfig.StorageDBPath, "messages-storage")
os.Mkdir(messagesFolder, 0750)
messagePath := filepath.Join(messagesFolder, newBagId+"_"+queryID)
if !storage.NewContractMessage(newBagId, messagePath, queryID, providerContractAddress) {
storage.RemoveBag(newBagId)
return c.JSON(http.StatusBadRequest, "can not create message")
}
// Generate transaction.
messageBody, err := os.ReadFile(messagePath)
if err != nil {
storage.RemoveBag(newBagId)
return c.JSON(http.StatusBadRequest, "can not read message")
}
messageCell, err := cell.FromBOC(messageBody)
if err != nil {
storage.RemoveBag(newBagId)
return c.JSON(http.StatusBadRequest, "can not parse message")
}
cellString := base64.StdEncoding.EncodeToString(messageCell.ToBOCWithFlags(false))
return c.JSON(http.StatusOK, echo.Map{
"transaction": datatype.StorageContractInfo{
Bin: cellString,
ServeBagID: newBagId,
},
})
}
// TODO: check if this is needed.
func (h *handler) OptionsHandler(c echo.Context) error {
return c.JSON(http.StatusOK, "Allow: OPTIONS, GET, HEAD, POST")
}