view.go 5.9 KB

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