-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconstFile1
119 lines (95 loc) · 2.44 KB
/
reconstFile1
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
package main
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"sort"
"sync"
"time"
shell "github.com/ipfs/go-ipfs-api"
)
type ChunkResult struct {
Index int
Data []byte
Valid bool
}
func getFile() {
// ... your other code here ...
fmt.Println("Provide the name of the file you want to retrieve:")
var fileName string
fmt.Scan(&fileName)
metadata, err := LoadMetadata(fileName)
if err != nil {
panic(err)
}
//retreivedChunks := make([]ReconFile, len(metadata.IpfsHashes))
retreiveddata := make([][]byte, len(metadata.IpfsHashes))
fmt.Println("########Retreiving########")
api := shell.NewShell("localhost:5001") // Create a new IPFS API client
for i := 0; i < 10; i++ {
startt := time.Now()
results := downloadChunks(metadata, api)
validChunks := 0
for _, result := range results {
if result.Valid {
validChunks++
}
}
if validChunks < metadata.DataShards {
fmt.Println("Not enough valid chunks to reconstruct the file")
return
}
// ... your other code here ...
sort.Slice(results, func(i, j int) bool {
return results[i].Index < results[j].Index // use ">" if you want descending order
})
//fmt.Println(len(retreivedChunks))
for i, chunk := range results {
//fmt.Println(i,chunk)
retreiveddata[i] = chunk.Data
}
decodedData, err := decodingData(retreiveddata, metadata)
if err != nil {
panic(err)
}
elapsed := time.Since(startt)
fmt.Println("File retreival time", elapsed)
// Save the decoded file
err = ioutil.WriteFile("RetrievedFile.txt", decodedData, 0644)
if err != nil {
panic(err)
}
fmt.Println("File retrieved successfully!")
}
// ... your other code here ...
}
func downloadChunks(metadata *Metadata, api *shell.Shell) []ChunkResult {
results := make([]ChunkResult, len(metadata.IpfsHashes))
var wg sync.WaitGroup
wg.Add(len(metadata.IpfsHashes))
for i := range metadata.IpfsHashes {
go func(i int) {
defer wg.Done()
fileContents, err := api.Cat(metadata.IpfsHashes[i])
if err != nil {
results[i] = ChunkResult{Index: i}
return
}
data, err := io.ReadAll(fileContents)
if err != nil {
results[i] = ChunkResult{Index: i}
return
}
hash := md5.Sum(data)
if fmt.Sprintf("%x", hash) == metadata.ShardHashes[i] {
results[i] = ChunkResult{Index: i, Data: data, Valid: true}
fmt.Printf("Chunk %v retrieved successfully!\n", i)
} else {
results[i] = ChunkResult{Index: i}
}
}(i)
}
wg.Wait()
return results
}