view.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2014 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. "bytes"
  7. "io/ioutil"
  8. "path"
  9. "path/filepath"
  10. "strings"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/git"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/middleware"
  16. )
  17. const (
  18. HOME base.TplName = "repo/home"
  19. )
  20. func Home(ctx *middleware.Context) {
  21. ctx.Data["Title"] = ctx.Repo.Repository.Name
  22. branchName := ctx.Repo.BranchName
  23. userName := ctx.Repo.Owner.Name
  24. repoName := ctx.Repo.Repository.Name
  25. repoLink := ctx.Repo.RepoLink
  26. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  27. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  28. // Get tree path
  29. treename := ctx.Repo.TreeName
  30. if len(treename) > 0 && treename[len(treename)-1] == '/' {
  31. ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
  32. return
  33. }
  34. ctx.Data["IsRepoToolbarSource"] = true
  35. isViewBranch := ctx.Repo.IsBranch
  36. ctx.Data["IsViewBranch"] = isViewBranch
  37. treePath := treename
  38. if len(treePath) != 0 {
  39. treePath = treePath + "/"
  40. }
  41. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
  42. if err != nil && err != git.ErrNotExist {
  43. ctx.Handle(404, "GetTreeEntryByPath", err)
  44. return
  45. }
  46. if len(treename) != 0 && entry == nil {
  47. ctx.Handle(404, "repo.Home", nil)
  48. return
  49. }
  50. if entry != nil && !entry.IsDir() {
  51. blob := entry.Blob()
  52. if dataRc, err := blob.Data(); err != nil {
  53. ctx.Handle(404, "blob.Data", err)
  54. return
  55. } else {
  56. ctx.Data["FileSize"] = blob.Size()
  57. ctx.Data["IsFile"] = true
  58. ctx.Data["FileName"] = blob.Name()
  59. ext := path.Ext(blob.Name())
  60. if len(ext) > 0 {
  61. ext = ext[1:]
  62. }
  63. ctx.Data["FileExt"] = ext
  64. ctx.Data["FileLink"] = rawLink + "/" + treename
  65. buf := make([]byte, 1024)
  66. n, _ := dataRc.Read(buf)
  67. if n > 0 {
  68. buf = buf[:n]
  69. }
  70. _, isTextFile := base.IsTextFile(buf)
  71. _, isImageFile := base.IsImageFile(buf)
  72. ctx.Data["IsFileText"] = isTextFile
  73. switch {
  74. case isImageFile:
  75. ctx.Data["IsImageFile"] = true
  76. case isTextFile:
  77. d, _ := ioutil.ReadAll(dataRc)
  78. buf = append(buf, d...)
  79. readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
  80. ctx.Data["ReadmeExist"] = readmeExist
  81. if readmeExist {
  82. ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, branchLink))
  83. } else {
  84. if err, content := base.ToUtf8WithErr(buf); err != nil {
  85. if err != nil {
  86. log.Error(4, "Convert content encoding: %s", err)
  87. }
  88. ctx.Data["FileContent"] = string(buf)
  89. } else {
  90. ctx.Data["FileContent"] = content
  91. }
  92. }
  93. }
  94. }
  95. } else {
  96. // Directory and file list.
  97. tree, err := ctx.Repo.Commit.SubTree(treename)
  98. if err != nil {
  99. ctx.Handle(404, "SubTree", err)
  100. return
  101. }
  102. entries, err := tree.ListEntries(treename)
  103. if err != nil {
  104. ctx.Handle(500, "ListEntries", err)
  105. return
  106. }
  107. entries.Sort()
  108. files := make([][]interface{}, 0, len(entries))
  109. for _, te := range entries {
  110. if te.Type != git.COMMIT {
  111. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  112. if err != nil {
  113. ctx.Handle(500, "GetCommitOfRelPath", err)
  114. return
  115. }
  116. files = append(files, []interface{}{te, c})
  117. } else {
  118. sm, err := ctx.Repo.Commit.GetSubModule(path.Join(treename, te.Name()))
  119. if err != nil {
  120. ctx.Handle(500, "GetSubModule", err)
  121. return
  122. }
  123. smUrl := ""
  124. if sm != nil {
  125. smUrl = sm.Url
  126. }
  127. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  128. if err != nil {
  129. ctx.Handle(500, "GetCommitOfRelPath", err)
  130. return
  131. }
  132. files = append(files, []interface{}{te, git.NewSubModuleFile(c, smUrl, te.Id.String())})
  133. }
  134. }
  135. ctx.Data["Files"] = files
  136. var readmeFile *git.Blob
  137. for _, f := range entries {
  138. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  139. continue
  140. } else {
  141. readmeFile = f.Blob()
  142. break
  143. }
  144. }
  145. if readmeFile != nil {
  146. ctx.Data["ReadmeInList"] = true
  147. ctx.Data["ReadmeExist"] = true
  148. if dataRc, err := readmeFile.Data(); err != nil {
  149. ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
  150. return
  151. } else {
  152. buf := make([]byte, 1024)
  153. n, _ := dataRc.Read(buf)
  154. if n > 0 {
  155. buf = buf[:n]
  156. }
  157. ctx.Data["FileSize"] = readmeFile.Size()
  158. ctx.Data["FileLink"] = rawLink + "/" + treename
  159. _, isTextFile := base.IsTextFile(buf)
  160. ctx.Data["FileIsText"] = isTextFile
  161. ctx.Data["FileName"] = readmeFile.Name()
  162. if isTextFile {
  163. d, _ := ioutil.ReadAll(dataRc)
  164. buf = append(buf, d...)
  165. switch {
  166. case base.IsMarkdownFile(readmeFile.Name()):
  167. buf = base.RenderMarkdown(buf, branchLink)
  168. default:
  169. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  170. }
  171. ctx.Data["FileContent"] = string(buf)
  172. }
  173. }
  174. }
  175. lastCommit := ctx.Repo.Commit
  176. if len(treePath) > 0 {
  177. c, err := ctx.Repo.Commit.GetCommitOfRelPath(treePath)
  178. if err != nil {
  179. ctx.Handle(500, "GetCommitOfRelPath", err)
  180. return
  181. }
  182. lastCommit = c
  183. }
  184. ctx.Data["LastCommit"] = lastCommit
  185. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  186. }
  187. ctx.Data["Username"] = userName
  188. ctx.Data["Reponame"] = repoName
  189. var treenames []string
  190. Paths := make([]string, 0)
  191. if len(treename) > 0 {
  192. treenames = strings.Split(treename, "/")
  193. for i, _ := range treenames {
  194. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  195. }
  196. ctx.Data["HasParentPath"] = true
  197. if len(Paths)-2 >= 0 {
  198. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  199. }
  200. }
  201. ctx.Data["Paths"] = Paths
  202. ctx.Data["TreeName"] = treename
  203. ctx.Data["Treenames"] = treenames
  204. ctx.Data["TreePath"] = treePath
  205. ctx.Data["BranchLink"] = branchLink
  206. ctx.HTML(200, HOME)
  207. }