-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (141 loc) · 3.7 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
"github.com/recentralized/structure/data"
"github.com/recentralized/structure/dst"
"github.com/recentralized/structure/index"
"github.com/recentralized/structure/meta"
"github.com/recentralized/structure/uri"
)
func main() {
index, err := buildIndex()
if err != nil {
fmt.Printf("Failed to build index: %s", err)
os.Exit(1)
}
layout := dst.NewFilesystemLayout()
err = addRefs(layout, index)
if err != nil {
fmt.Printf("Failed to add refs: %s", err)
os.Exit(1)
}
data, err := json.MarshalIndent(index, "", " ")
if err != nil {
fmt.Printf("Failed to create json: %s", err)
os.Exit(1)
}
fmt.Println(string(data))
}
func buildIndex() (*index.Index, error) {
srcPath, err := uri.ParseDir("/tmp/src")
if err != nil {
return nil, fmt.Errorf("Could not create src path: %s", err)
}
dstPath, err := uri.ParseDir("/tmp/dst")
if err != nil {
return nil, fmt.Errorf("Could not create dst path: %s", err)
}
srcURI, err := srcPath.URI()
if err != nil {
return nil, fmt.Errorf("Could not create src uri: %s", err)
}
dstURI, err := dstPath.URI()
if err != nil {
return nil, fmt.Errorf("Could not create dst uri: %s", err)
}
src := index.NewSrc(srcURI)
dst := index.NewDstAllAt(dstURI)
idx := index.New()
idx.Srcs = []index.Src{src}
idx.Dsts = []index.Dst{dst}
return idx, nil
}
func addRefs(layout dst.Layout, idx *index.Index) error {
src := idx.Srcs[0]
dst := idx.Dsts[0]
// Find something on the source
srcItem, meta, err := buildSrcItem(src)
if err != nil {
return err
}
// Get data from the source.
data := []byte(strings.Repeat("fictional image data", 100))
// Calculate fingerprint of data.
hash, err := layout.NewHash(bytes.NewReader(data))
if err != nil {
return err
}
// Decide how it will be stored on the destination.
dstItem, err := buildDstItem(layout, dst, hash, meta)
if err != nil {
return err
}
// Calculate size of data to be stored.
c, err := io.Copy(ioutil.Discard, bytes.NewReader(data))
if err != nil {
return err
}
dstItem.DataSize = c
// Calculate size of meta to be stored.
metaJSON, err := json.Marshal(meta)
if err != nil {
return err
}
dstItem.MetaSize = int64(len(metaJSON))
// Here you would store data and metadata on the destination.
// Add a ref to the index.
idx.AddRef(index.Ref{
Hash: hash,
Src: srcItem,
Dst: dstItem,
})
return nil
}
func buildSrcItem(src index.Src) (index.SrcItem, *meta.Meta, error) {
var item index.SrcItem
dataPath := uri.TrustedNew("fictional/image.jpg")
dataURI, err := src.SrcURI.ResolveReference(dataPath)
if err != nil {
return item, nil, err
}
metaPath := uri.TrustedNew("fictional/image.xmp")
metaURI, err := src.SrcURI.ResolveReference(metaPath)
if err != nil {
return item, nil, err
}
item = index.SrcItem{
SrcID: src.SrcID,
DataURI: dataURI,
MetaURI: metaURI,
ModifiedAt: time.Date(2018, 11, 12, 0, 0, 0, 0, time.UTC),
}
doc := meta.New()
doc.Type = data.JPG
doc.Inherent = meta.Content{
Created: time.Date(2018, 11, 10, 0, 0, 0, 0, time.UTC),
}
return item, doc, nil
}
func buildDstItem(layout dst.Layout, dst index.Dst, hash data.Hash, meta *meta.Meta) (index.DstItem, error) {
var item index.DstItem
dataURI := layout.DataURI(hash, meta)
metaURI := layout.MetaURI(hash, meta)
item = index.DstItem{
DstID: dst.DstID,
DataURI: dataURI,
MetaURI: metaURI,
DataType: data.Stored{Type: meta.Type},
DataSize: 0, // updated with actual data size
MetaSize: 0, // updated with actual meta size
StoredAt: time.Date(2018, 11, 13, 0, 0, 0, 0, time.UTC),
UpdatedAt: time.Date(2018, 11, 14, 0, 0, 0, 0, time.UTC),
}
return item, nil
}