summaryrefslogtreecommitdiffstats
path: root/modules/repofiles/blob.go
diff options
context:
space:
mode:
authorRichard Mahn <richmahn@users.noreply.github.com>2019-04-17 10:06:35 -0600
committertechknowlogick <matti@mdranta.net>2019-04-17 12:06:35 -0400
commit2262811e407facea09047e94aa1850c192511587 (patch)
treea478624613dc6cf095784d629f9627b197de15e8 /modules/repofiles/blob.go
parent059195b127848d96a4690257af19d8c97c6d2363 (diff)
downloadgitea-2262811e407facea09047e94aa1850c192511587.tar.gz
gitea-2262811e407facea09047e94aa1850c192511587.zip
Fixes 4762 - Content API for Creating, Updating, Deleting Files (#6314)
Diffstat (limited to 'modules/repofiles/blob.go')
-rw-r--r--modules/repofiles/blob.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/modules/repofiles/blob.go b/modules/repofiles/blob.go
new file mode 100644
index 0000000000..2f9ca72bd4
--- /dev/null
+++ b/modules/repofiles/blob.go
@@ -0,0 +1,38 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repofiles
+
+import (
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/setting"
+ api "code.gitea.io/sdk/gitea"
+)
+
+// GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
+func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, error) {
+ gitRepo, err := git.OpenRepository(repo.RepoPath())
+ if err != nil {
+ return nil, err
+ }
+ gitBlob, err := gitRepo.GetBlob(sha)
+ if err != nil {
+ return nil, err
+ }
+ content := ""
+ if gitBlob.Size() <= setting.API.DefaultMaxBlobSize {
+ content, err = gitBlob.GetBlobContentBase64()
+ if err != nil {
+ return nil, err
+ }
+ }
+ return &api.GitBlobResponse{
+ SHA: gitBlob.ID.String(),
+ URL: repo.APIURL() + "/git/blobs/" + gitBlob.ID.String(),
+ Size: gitBlob.Size(),
+ Encoding: "base64",
+ Content: content,
+ }, nil
+}