You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repofiles
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/setting"
  9. api "code.gitea.io/gitea/modules/structs"
  10. )
  11. // GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
  12. func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, error) {
  13. gitRepo, err := git.OpenRepository(repo.RepoPath())
  14. if err != nil {
  15. return nil, err
  16. }
  17. defer gitRepo.Close()
  18. gitBlob, err := gitRepo.GetBlob(sha)
  19. if err != nil {
  20. return nil, err
  21. }
  22. content := ""
  23. if gitBlob.Size() <= setting.API.DefaultMaxBlobSize {
  24. content, err = gitBlob.GetBlobContentBase64()
  25. if err != nil {
  26. return nil, err
  27. }
  28. }
  29. return &api.GitBlobResponse{
  30. SHA: gitBlob.ID.String(),
  31. URL: repo.APIURL() + "/git/blobs/" + gitBlob.ID.String(),
  32. Size: gitBlob.Size(),
  33. Encoding: "base64",
  34. Content: content,
  35. }, nil
  36. }