This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
Migration019.go
162 lines (138 loc) · 3.95 KB
/
Migration019.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
package migrations
import (
"encoding/json"
"fmt"
"gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
"io/ioutil"
"os"
"path"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreunix"
"gx/ipfs/QmQmhotPUzVrMEWNK3x1R5jQ5ZHWyL7tVUrmRPjrBrvyCb/go-ipfs-files"
"github.com/ipfs/go-ipfs/core/mock"
)
// Circular imports importing the core package here so we need to copy this here.
type price struct {
CurrencyCode string `json:"currencyCode"`
Amount uint64 `json:"amount"`
Modifier float32 `json:"modifier"`
}
type thumbnail struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Medium string `json:"medium"`
}
type ListingData struct {
Hash string `json:"hash"`
Slug string `json:"slug"`
Title string `json:"title"`
Categories []string `json:"categories"`
NSFW bool `json:"nsfw"`
ContractType string `json:"contractType"`
Description string `json:"description"`
Thumbnail thumbnail `json:"thumbnail"`
Price price `json:"price"`
ShipsTo []string `json:"shipsTo"`
FreeShipping []string `json:"freeShipping"`
Language string `json:"language"`
AverageRating float32 `json:"averageRating"`
RatingCount uint32 `json:"ratingCount"`
ModeratorIDs []string `json:"moderators"`
AcceptedCurrencies []string `json:"acceptedCurrencies"`
CoinType string `json:"coinType"`
}
// Migration019 migrates the listing index file to use the new style (Qm) hashes
// for listings rather than the old CID (z) style hashes.
type Migration019 struct{}
func (Migration019) Up(repoPath string, dbPassword string, testnet bool) error {
ipfsNode, err := coremock.NewMockNode()
if err != nil {
return err
}
err = updateEachListingOnIndex(repoPath, func(ld *ListingData) error {
listingPath := path.Join(repoPath, "root", "listings", ld.Slug+".json")
listingHash, err := getHashOfFile(ipfsNode, listingPath)
if err != nil {
return err
}
ld.Hash = listingHash
return nil
})
if err != nil {
return err
}
if err := writeRepoVer(repoPath, 20); err != nil {
return fmt.Errorf("bumping repover to 20: %s", err.Error())
}
return nil
}
func (Migration019) Down(repoPath string, dbPassword string, testnet bool) error {
// Down migration is a no-op (outside of updating the version).
// We can't calculate the old hashes because the go-ipfs is not configured to
// do so.
return writeRepoVer(repoPath, 19)
}
func updateEachListingOnIndex(repoPath string, updateListing func(*ListingData) error) error {
indexPath := path.Join(repoPath, "root", "listings.json")
var index []ListingData
_, ferr := os.Stat(indexPath)
if os.IsNotExist(ferr) {
return nil
}
file, err := ioutil.ReadFile(indexPath)
if err != nil {
return err
}
err = json.Unmarshal(file, &index)
if err != nil {
return err
}
for i, d := range index {
if err := updateListing(&d); err != nil {
return err
}
index[i] = d
}
f, err := os.Create(indexPath)
if err != nil {
return err
}
defer f.Close()
j, jerr := json.MarshalIndent(index, "", " ")
if jerr != nil {
return jerr
}
_, werr := f.Write(j)
if werr != nil {
return werr
}
return nil
}
func getHashOfFile(n *core.IpfsNode, root string) (rootHash string, err error) {
defer n.Blockstore.PinLock().Unlock()
stat, err := os.Lstat(root)
if err != nil {
return "", err
}
f, err := files.NewSerialFile(root, false, stat)
if err != nil {
return "", err
}
defer f.Close()
fileAdder, err := coreunix.NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", err
}
fileAdder.Progress = false
fileAdder.Hidden = true
fileAdder.Pin = true
fileAdder.Trickle = false
fileAdder.Wrap = false
fileAdder.Chunker = ""
fileAdder.CidBuilder = cid.V0Builder{}
node, err := fileAdder.AddAllAndPin(f)
if err != nil {
return "", err
}
return node.Cid().String(), nil
}