-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.go
More file actions
187 lines (158 loc) · 4.06 KB
/
content.go
File metadata and controls
187 lines (158 loc) · 4.06 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
package utils
import (
"encoding/base64"
"fmt"
"io/ioutil"
"github.com/gitopia/go-git/v5/plumbing/object"
)
type ContentType int
const (
TREE ContentType = iota
BLOB
)
func (c ContentType) String() string {
return [...]string{"TREE", "BLOB"}[c]
}
type ContentRequestBody struct {
RepositoryID uint64 `json:"repository_id"`
RefId string `json:"ref_id"`
Path string `json:"path"`
IncludeLastCommit bool `json:"include_last_commit"`
From uint64 `json:"from"`
To uint64 `json:"to"`
Pagination *PageRequest `json:"pagination"`
NoRestriction bool `json:"no_restriction"`
}
type ContentResponse struct {
Content []*Content `json:"content,omitempty"`
Pagination *PageResponse `json:"pagination,omitempty"`
}
type Content struct {
Name string `json:"name"`
Path string `json:"path"`
Sha string `json:"sha"`
Type string `json:"type"`
Size int64 `json:"size,omitempty"`
Content string `json:"content,omitempty"`
Encoding string `json:"encoding,omitempty"`
LastCommit *Commit `json:"last_commit,omitempty"`
}
func GrabFileContent(blob object.Blob, treeEntry object.TreeEntry, pathPrefix string, noRestriction bool) (res *Content, err error) {
var encodedData, encoding string
if blob.Size < 1000000 || noRestriction {
blobReader, err := blob.Reader()
if err != nil {
return nil, err
}
blobReader.Close()
data, err := ioutil.ReadAll(blobReader)
encodedData = base64.StdEncoding.EncodeToString(data)
encoding = "base64"
}
fileContent := Content{
Name: treeEntry.Name,
Path: func() string {
if pathPrefix == "" {
return treeEntry.Name
} else {
return pathPrefix
}
}(),
Sha: treeEntry.Hash.String(),
Type: BLOB.String(),
Size: blob.Size,
Content: encodedData,
Encoding: encoding,
}
return &fileContent, nil
}
func GrabTreeContent(treeEntry object.TreeEntry, pathPrefix string) (res *Content, err error) {
treeContent := Content{
Name: treeEntry.Name,
Path: func() string {
if pathPrefix == "" {
return treeEntry.Name
} else {
return pathPrefix + "/" + treeEntry.Name
}
}(),
Sha: treeEntry.Hash.String(),
Type: func() string {
if treeEntry.Mode.IsFile() {
return BLOB.String()
} else {
return TREE.String()
}
}(),
}
return &treeContent, nil
}
func PaginateTreeContentResponse(
tree *object.Tree,
pageRequest *PageRequest,
defaultLimit uint64,
pathPrefix string,
onResult func(treeContent Content) error,
) (*PageResponse, error) {
totalTreeEntriesCount := uint64(len(tree.Entries))
// if the PageRequest is nil, use default PageRequest
if pageRequest == nil {
pageRequest = &PageRequest{}
}
offset := pageRequest.Offset
key := pageRequest.Key
limit := pageRequest.Limit
countTotal := pageRequest.CountTotal
if offset > 0 && key != nil {
return nil, fmt.Errorf("invalid request, either offset or key is expected, got both")
}
if limit == 0 {
limit = defaultLimit
// show total issue count when the limit is zero/not supplied
countTotal = true
}
if len(key) != 0 {
var count uint64
var nextKey []byte
for i := BytesToUInt64(key); uint64(i) < totalTreeEntriesCount; i++ {
if count == limit {
nextKey = UInt64ToBytes(uint64(i))
break
}
treeEntry, err := GrabTreeContent(tree.Entries[i], pathPrefix)
if err != nil {
return nil, err
}
err = onResult(*treeEntry)
if err != nil {
return nil, err
}
count++
}
return &PageResponse{
NextKey: nextKey,
}, nil
}
end := offset + limit
var nextKey []byte
for i := offset; uint64(i) < totalTreeEntriesCount; i++ {
if uint64(i) < end {
diff, err := GrabTreeContent(tree.Entries[i], pathPrefix)
if err != nil {
return nil, err
}
err = onResult(*diff)
if err != nil {
return nil, err
}
} else if uint64(i) == end+1 {
nextKey = UInt64ToBytes(uint64(i))
break
}
}
res := &PageResponse{NextKey: nextKey}
if countTotal {
res.Total = totalTreeEntriesCount
}
return res, nil
}