forked from SkynetLabs/go-skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
56 lines (46 loc) · 1.42 KB
/
metadata.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
package skynet
import (
"fmt"
"net/http"
"strconv"
"strings"
"gitlab.com/NebulousLabs/errors"
)
type (
Metadata struct {
ContentType string
Etag string
Skylink string
ContentLength int
}
)
// Metadata downloads metadata from the given skylink.
func (sc *SkynetClient) Metadata(skylink string, opts MetadataOptions) (*Metadata, error) {
skylink = strings.TrimPrefix(skylink, "sia://")
config := requestOptions{
query: map[string][]string{},
Options: opts.Options,
method: http.MethodHead,
extraPath: skylink,
}
resp, err := sc.executeRequest(config)
if err != nil {
fmt.Println("SKYNET: failed to execute req: ", err.Error())
return nil, errors.AddContext(err, "failed to execute request")
}
// Metadata API's biggest use case is for checking content-length and using is for concurrent downloads
// If contentLength is missing, it's sort of is equivalent of an error
if resp.Header.Get("content-length") == "" {
return nil, fmt.Errorf("error retrieving metadata for skylink: %s - ContentLength is absent", skylink)
}
var metadata Metadata
contentLength, err := strconv.ParseInt(resp.Header.Get("content-length"), 10, 64)
if err != nil {
return nil, err
}
metadata.ContentLength = int(contentLength)
metadata.Skylink = resp.Header.Get("skynet-skylink")
metadata.ContentType = resp.Header.Get("content-type")
metadata.Etag = resp.Header.Get("etag")
return &metadata, nil
}