-
Notifications
You must be signed in to change notification settings - Fork 66
/
misc.go
80 lines (66 loc) · 1.69 KB
/
misc.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
package util
import (
"net/http"
"github.com/application-research/filclient"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
"github.com/labstack/echo/v4"
"github.com/multiformats/go-multihash"
)
func TransferTerminated(st *filclient.ChannelState) bool {
switch st.Status {
case datatransfer.Cancelled,
datatransfer.Failed,
datatransfer.Completed:
return true
default:
return false
}
}
func ParseDealLabel(s string) (cid.Cid, error) {
return cid.Decode(s)
}
func FilterUnwalkableLinks(links []*ipld.Link) []*ipld.Link {
out := make([]*ipld.Link, 0, len(links))
for _, l := range links {
if CidIsUnwalkable(l.Cid) {
continue
}
out = append(out, l)
}
return out
}
func CidIsUnwalkable(c cid.Cid) bool {
pref := c.Prefix()
if pref.MhType == multihash.IDENTITY {
return true
}
if pref.Codec == cid.FilCommitmentSealed || pref.Codec == cid.FilCommitmentUnsealed {
return true
}
return false
}
func ErrorIfContentAddingDisabled(isContentAddingDisabled bool) error {
if isContentAddingDisabled {
return &HttpError{
Code: http.StatusBadRequest,
Reason: ERR_CONTENT_ADDING_DISABLED,
Details: "uploading content to this node is not allowed at the moment",
}
}
return nil
}
// required for car uploads
func WithContentLengthCheck(f func(echo.Context) error) func(echo.Context) error {
return func(c echo.Context) error {
if c.Request().Header.Get("Content-Length") == "" {
return &HttpError{
Code: http.StatusLengthRequired,
Reason: ERR_CONTENT_LENGTH_REQUIRED,
Details: "uploading car content requires Content-Length header value to be set",
}
}
return f(c)
}
}