contents.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2020 The Gogs 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 repo
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "io/ioutil"
  9. "github.com/gogs/git-module"
  10. "gogs.io/gogs/internal/context"
  11. )
  12. type repoContent struct {
  13. Type string `json:"type"`
  14. Target string `json:"target,omitempty"`
  15. SubmoduleGitURL string `json:"submodule_git_url,omitempty"`
  16. Encoding string `json:"encoding,omitempty"`
  17. Size int64 `json:"size"`
  18. Name string `json:"name"`
  19. Path string `json:"path"`
  20. Content string `json:"content,omitempty"`
  21. Sha string `json:"sha"`
  22. URL string `json:"url"`
  23. GitURL string `json:"git_url"`
  24. HTMLURL string `json:"html_url"`
  25. DownloadURL string `json:"download_url"`
  26. Links Links `json:"_links"`
  27. }
  28. type Links struct {
  29. Git string `json:"git"`
  30. Self string `json:"self"`
  31. HTML string `json:"html"`
  32. }
  33. func GetContents(c *context.APIContext) {
  34. treeEntry, err := c.Repo.Commit.GetTreeEntryByPath(c.Repo.TreePath)
  35. if err != nil {
  36. c.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
  37. return
  38. }
  39. username := c.Params(":username")
  40. reponame := c.Params(":reponame")
  41. // TODO: figure out the best way to do this
  42. // :base-url/:username/:project/raw/:refs/:path
  43. templateDownloadURL := "%s/%s/%s/raw/%s"
  44. // :base-url/repos/:username/:project/contents/:path
  45. templateSelfLink := "%s/repos/%s/%s/contents/%s"
  46. // :baseurl/repos/:username/:project/git/trees/:sha
  47. templateGitURLLink := "%s/repos/%s/%s/trees/%s"
  48. // :baseurl/repos/:username/:project/tree/:sha
  49. templateHTMLLLink := "%s/repos/%s/%s/tree/%s"
  50. gitURL := fmt.Sprintf(templateGitURLLink, c.BaseURL, username, reponame, treeEntry.ID.String())
  51. htmlURL := fmt.Sprintf(templateHTMLLLink, c.BaseURL, username, reponame, treeEntry.ID.String())
  52. selfURL := fmt.Sprintf(templateSelfLink, c.BaseURL, username, reponame, c.Repo.TreePath)
  53. // TODO(unknwon): Make a treeEntryToRepoContent helper.
  54. contents := &repoContent{
  55. Size: treeEntry.Size(),
  56. Name: treeEntry.Name(),
  57. Path: c.Repo.TreePath,
  58. Sha: treeEntry.ID.String(),
  59. URL: selfURL,
  60. GitURL: gitURL,
  61. HTMLURL: htmlURL,
  62. DownloadURL: fmt.Sprintf(templateDownloadURL, c.BaseURL, username, reponame, c.Repo.TreePath),
  63. Links: Links{
  64. Git: gitURL,
  65. Self: selfURL,
  66. HTML: htmlURL,
  67. },
  68. }
  69. // A tree entry can only be one of the following types:
  70. // 1. Tree (directory)
  71. // 2. SubModule
  72. // 3. SymLink
  73. // 4. Blob (file)
  74. if treeEntry.IsSubModule() {
  75. // TODO(unknwon): submoduleURL is not set as current git-module doesn't handle it properly
  76. contents.Type = "submodule"
  77. c.JSONSuccess(contents)
  78. return
  79. } else if treeEntry.IsLink() {
  80. contents.Type = "symlink"
  81. blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
  82. if err != nil {
  83. c.ServerError("GetBlobByPath", err)
  84. return
  85. }
  86. b, err := blob.Data()
  87. if err != nil {
  88. c.ServerError("Data", err)
  89. return
  90. }
  91. buf, err := ioutil.ReadAll(b)
  92. if err != nil {
  93. c.ServerError("ReadAll", err)
  94. return
  95. }
  96. contents.Target = string(buf)
  97. c.JSONSuccess(contents)
  98. return
  99. } else if treeEntry.Type == "blob" {
  100. blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
  101. if err != nil {
  102. c.ServerError("GetBlobByPath", err)
  103. return
  104. }
  105. b, err := blob.Data()
  106. if err != nil {
  107. c.ServerError("Data", err)
  108. return
  109. }
  110. buf, err := ioutil.ReadAll(b)
  111. if err != nil {
  112. c.ServerError("ReadAll", err)
  113. return
  114. }
  115. contents.Content = base64.StdEncoding.EncodeToString(buf)
  116. contents.Type = "file"
  117. c.JSONSuccess(contents)
  118. return
  119. }
  120. // treeEntry is a directory
  121. dirTree, err := c.Repo.GitRepo.GetTree(treeEntry.ID.String())
  122. if err != nil {
  123. c.NotFoundOrServerError("GetTree", git.IsErrNotExist, err)
  124. return
  125. }
  126. entries, err := dirTree.ListEntries()
  127. if err != nil {
  128. c.NotFoundOrServerError("ListEntries", git.IsErrNotExist, err)
  129. return
  130. }
  131. if len(entries) == 0 {
  132. c.JSONSuccess([]string{})
  133. return
  134. }
  135. var results = make([]*repoContent, 0, len(entries))
  136. for _, entry := range entries {
  137. gitURL := fmt.Sprintf(templateGitURLLink, c.BaseURL, username, reponame, entry.ID.String())
  138. htmlURL := fmt.Sprintf(templateHTMLLLink, c.BaseURL, username, reponame, entry.ID.String())
  139. selfURL := fmt.Sprintf(templateSelfLink, c.BaseURL, username, reponame, c.Repo.TreePath)
  140. var contentType string
  141. if entry.IsDir() {
  142. contentType = "dir"
  143. } else if entry.IsSubModule() {
  144. // TODO(unknwon): submoduleURL is not set as current git-module doesn't handle it properly
  145. contentType = "submodule"
  146. } else if entry.IsLink() {
  147. contentType = "symlink"
  148. blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
  149. if err != nil {
  150. c.ServerError("GetBlobByPath", err)
  151. return
  152. }
  153. b, err := blob.Data()
  154. if err != nil {
  155. c.ServerError("Data", err)
  156. return
  157. }
  158. buf, err := ioutil.ReadAll(b)
  159. if err != nil {
  160. c.ServerError("ReadAll", err)
  161. return
  162. }
  163. contents.Target = string(buf)
  164. } else {
  165. contentType = "file"
  166. }
  167. results = append(results, &repoContent{
  168. Type: contentType,
  169. Size: entry.Size(),
  170. Name: entry.Name(),
  171. Path: c.Repo.TreePath,
  172. Sha: entry.ID.String(),
  173. URL: selfURL,
  174. GitURL: gitURL,
  175. HTMLURL: htmlURL,
  176. DownloadURL: fmt.Sprintf(templateDownloadURL, c.BaseURL, username, reponame, c.Repo.TreePath),
  177. Links: Links{
  178. Git: gitURL,
  179. Self: selfURL,
  180. HTML: htmlURL,
  181. },
  182. })
  183. }
  184. c.JSONSuccess(results)
  185. }