mirror of
https://github.com/gogs/gogs.git
synced 2025-12-22 16:20:14 +01:00
api: support getting blob content (#7080)
Co-authored-by: Joe Chen <jc@unknwon.io>
This commit is contained in:
@@ -276,9 +276,14 @@ func RegisterRoutes(m *macaron.Macaron) {
|
|||||||
m.Get("/*", repo.GetContents)
|
m.Get("/*", repo.GetContents)
|
||||||
})
|
})
|
||||||
m.Get("/archive/*", repo.GetArchive)
|
m.Get("/archive/*", repo.GetArchive)
|
||||||
m.Group("/git/trees", func() {
|
m.Group("/git", func() {
|
||||||
|
m.Group("/trees", func() {
|
||||||
m.Get("/:sha", repo.GetRepoGitTree)
|
m.Get("/:sha", repo.GetRepoGitTree)
|
||||||
})
|
})
|
||||||
|
m.Group("/blobs", func() {
|
||||||
|
m.Get("/:sha", repo.RepoGitBlob)
|
||||||
|
})
|
||||||
|
})
|
||||||
m.Get("/forks", repo.ListForks)
|
m.Get("/forks", repo.ListForks)
|
||||||
m.Get("/tags", repo.ListTags)
|
m.Get("/tags", repo.ListTags)
|
||||||
m.Group("/branches", func() {
|
m.Group("/branches", func() {
|
||||||
|
|||||||
49
internal/route/api/v1/repo/blob.go
Normal file
49
internal/route/api/v1/repo/blob.go
Normal file
@@ -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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user