diff options
author | Yehonatan Ezron <37303618+jonatan5524@users.noreply.github.com> | 2022-07-17 10:16:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-17 15:16:52 +0800 |
commit | a5d3e1900ef7a967619bdb9cf13162ffdadcfec1 (patch) | |
tree | fdd1bd5e618f15e9a4b9ed314f09e8567e083e05 /internal/route/api/v1/repo | |
parent | ab7133b35aabf9ce2ebf83e5fec2ab37ffa7c60a (diff) |
api: support getting blob content (#7080)
Co-authored-by: Joe Chen <jc@unknwon.io>
Diffstat (limited to 'internal/route/api/v1/repo')
-rw-r--r-- | internal/route/api/v1/repo/blob.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/internal/route/api/v1/repo/blob.go b/internal/route/api/v1/repo/blob.go new file mode 100644 index 00000000..7c3b2615 --- /dev/null +++ b/internal/route/api/v1/repo/blob.go @@ -0,0 +1,49 @@ +package repo + +import ( + "encoding/base64" + "fmt" + + "github.com/gogs/git-module" + + "gogs.io/gogs/internal/context" + "gogs.io/gogs/internal/gitutil" + "gogs.io/gogs/internal/repoutil" +) + +func RepoGitBlob(c *context.APIContext) { + gitRepo, err := git.Open(repoutil.RepositoryPath(c.Params(":username"), c.Params(":reponame"))) + if err != nil { + c.Error(err, "open repository") + return + } + + sha := c.Params(":sha") + blob, err := gitRepo.CatFileBlob(sha) + if err != nil { + c.NotFoundOrError(gitutil.NewError(err), "get blob") + return + } + + type repoGitBlob struct { + Content string `json:"content"` + Encoding string `json:"encoding"` + URL string `json:"url"` + SHA string `json:"sha"` + Size int64 `json:"size"` + } + + content, err := blob.Blob().Bytes() + if err != nil { + c.NotFoundOrError(gitutil.NewError(err), "get blob content") + return + } + + c.JSONSuccess(&repoGitBlob{ + Content: base64.StdEncoding.EncodeToString(content), + Encoding: "base64", + URL: fmt.Sprintf("%s/repos/%s/%s/git/blobs/%s", c.BaseURL, c.Params(":username"), c.Params(":reponame"), sha), + SHA: sha, + Size: blob.Size(), + }) +} |