-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
93 lines (81 loc) · 1.71 KB
/
find.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
package bloom
import (
"archive/zip"
"context"
"fmt"
"github.com/pkg/errors"
"github.com/zeebo/errs"
"io"
"os"
"storj.io/common/storj"
"storj.io/uplink"
"strings"
)
type Find struct {
NodeID storj.NodeID `arg:""`
Bucket string
Prefix string
}
func (f Find) Run() error {
accessString := os.Getenv("UPLINK_ACCESS")
access, err := uplink.ParseAccess(accessString)
if err != nil {
return errors.WithStack(err)
}
ctx := context.Background()
project, err := uplink.OpenProject(ctx, access)
if err != nil {
return errors.WithStack(err)
}
defer project.Close()
iterator := project.ListObjects(ctx, f.Bucket, &uplink.ListObjectsOptions{
Prefix: f.Prefix + "/",
System: true,
Recursive: true,
})
i := 1
for iterator.Next() {
object := iterator.Item()
fmt.Println(i, "checking", object.Key)
i++
if !strings.HasSuffix(object.Key, ".zip") {
continue
}
reader, err := zip.NewReader(&ChunkReader{
ctx: ctx,
bucket: f.Bucket,
key: object.Key,
project: project,
}, object.System.ContentLength)
if err != nil {
return errors.WithStack(err)
}
for _, file := range reader.File {
if file.Name == f.NodeID.String() {
fmt.Println("FOUND", file.Name, object.Key)
return nil
}
}
}
return nil
}
type ChunkReader struct {
io.ReaderAt
ctx context.Context
key string
bucket string
project *uplink.Project
}
func (t *ChunkReader) ReadAt(p []byte, off int64) (n int, err error) {
download, err := t.project.DownloadObject(t.ctx, t.bucket, t.key, &uplink.DownloadOptions{
Offset: off,
Length: int64(len(p)),
})
if err != nil {
return -1, err
}
defer func() {
err = errs.Combine(err, download.Close())
}()
return io.ReadFull(download, p)
}