Skip to content

Commit 65161bc

Browse files
authored
Merge pull request #23 from gitopia/hariom/restrict-content-api
Restrict content API by default
2 parents 8f84665 + ad2abcd commit 65161bc

2 files changed

Lines changed: 16 additions & 17 deletions

File tree

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
508508
if err != nil {
509509
http.Error(w, err.Error(), http.StatusNotFound)
510510
}
511-
fc, err := utils.GrabFileContent(*blob, *treeEntry, body.Path)
511+
fc, err := utils.GrabFileContent(*blob, *treeEntry, body.Path, body.NoRestriction)
512512
if err != nil {
513513
http.Error(w, err.Error(), http.StatusNotFound)
514514
}

utils/content.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ContentRequestBody struct {
2525
Path string `json:"path"`
2626
IncludeLastCommit bool `json:"include_last_commit"`
2727
Pagination *PageRequest `json:"pagination"`
28+
NoRestriction bool `json:"no_restriction"`
2829
}
2930

3031
type ContentResponse struct {
@@ -43,14 +44,18 @@ type Content struct {
4344
LastCommit *Commit `json:"last_commit,omitempty"`
4445
}
4546

46-
func GrabFileContent(blob object.Blob, treeEntry object.TreeEntry, pathPrefix string) (res *Content, err error) {
47-
blobReader, err := blob.Reader()
48-
if err != nil {
49-
return nil, err
47+
func GrabFileContent(blob object.Blob, treeEntry object.TreeEntry, pathPrefix string, noRestriction bool) (res *Content, err error) {
48+
var encodedData, encoding string
49+
if blob.Size < 1000000 || noRestriction {
50+
blobReader, err := blob.Reader()
51+
if err != nil {
52+
return nil, err
53+
}
54+
blobReader.Close()
55+
data, err := ioutil.ReadAll(blobReader)
56+
encodedData = base64.StdEncoding.EncodeToString(data)
57+
encoding = "base64"
5058
}
51-
blobReader.Close()
52-
data, err := ioutil.ReadAll(blobReader)
53-
encodedData := base64.StdEncoding.EncodeToString(data)
5459

5560
fileContent := Content{
5661
Name: treeEntry.Name,
@@ -61,17 +66,11 @@ func GrabFileContent(blob object.Blob, treeEntry object.TreeEntry, pathPrefix st
6166
return pathPrefix
6267
}
6368
}(),
64-
Sha: treeEntry.Hash.String(),
65-
Type: func() string {
66-
if treeEntry.Mode.IsFile() {
67-
return BLOB.String()
68-
} else {
69-
return TREE.String()
70-
}
71-
}(),
69+
Sha: treeEntry.Hash.String(),
70+
Type: BLOB.String(),
7271
Size: blob.Size,
7372
Content: encodedData,
74-
Encoding: "base64",
73+
Encoding: encoding,
7574
}
7675
return &fileContent, nil
7776
}

0 commit comments

Comments
 (0)