This repository was archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipfs.go
More file actions
72 lines (60 loc) · 1.47 KB
/
ipfs.go
File metadata and controls
72 lines (60 loc) · 1.47 KB
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
package utils
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"time"
shell "github.com/ipfs/go-ipfs-api"
)
const (
add = "/api/v0/add"
nocopy = "nocopy=true"
fileNameFmt = "%s.%s.bf"
)
// IPFSClient ...
type IPFSClient struct {
Host string
Shell *shell.Shell
}
// Response ...
type Response struct {
Name string `json:"Name"`
Hash string `json:"Hash"`
Bytes int64 `json:"Bytes"`
Size string `json:"Size"`
}
// File ...
type File struct {
Data string `json:"data"`
LastModified int64 `json:"last_modified"`
Name string `json:"name"`
FileType string `json:"file_type"`
}
// GetFileName ...
func (ipfs *IPFSClient) GetFileName(name, filetype string) string {
return fmt.Sprintf(fileNameFmt, name, filetype)
}
// NewFile ...
func (ipfs *IPFSClient) NewFile(name string, data []byte) (*File, []byte) {
lastModified := time.Now().Unix()
encoded := hex.EncodeToString(data)
file := &File{
Data: encoded,
LastModified: lastModified,
Name: name,
}
b, _ := json.Marshal(file)
return file, b
}
// AddFile ...
func (ipfs *IPFSClient) AddFile(filename, filetype string, data []byte) (string, string, error) {
fileName := ipfs.GetFileName(filename, filetype)
_, fileBytes := ipfs.NewFile(fileName, data)
hash, err := ipfs.Shell.Add(bytes.NewReader(fileBytes))
return fileName, hash, err
}
// NewIPFSHTTPClient ...
func NewIPFSHTTPClient(host string) *IPFSClient {
return &IPFSClient{host, shell.NewShell(host)}
}