view.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. "strings"
  10. "github.com/Unknwon/paginater"
  11. "github.com/gogits/git-module"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  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. WATCHERS base.TplName = "repo/watchers"
  21. FORKS base.TplName = "repo/forks"
  22. )
  23. func Home(ctx *middleware.Context) {
  24. ctx.Data["Title"] = ctx.Repo.Repository.Name
  25. ctx.Data["PageIsViewCode"] = true
  26. ctx.Data["RequireHighlightJS"] = true
  27. branchName := ctx.Repo.BranchName
  28. userName := ctx.Repo.Owner.Name
  29. repoName := ctx.Repo.Repository.Name
  30. repoLink := ctx.Repo.RepoLink
  31. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  32. treeLink := branchLink
  33. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  34. // Get tree path
  35. treename := ctx.Repo.TreeName
  36. if len(treename) > 0 {
  37. if treename[len(treename)-1] == '/' {
  38. ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
  39. return
  40. }
  41. treeLink += "/" + treename
  42. }
  43. treePath := treename
  44. if len(treePath) != 0 {
  45. treePath = treePath + "/"
  46. }
  47. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
  48. if err != nil && git.IsErrNotExist(err) {
  49. ctx.Handle(404, "GetTreeEntryByPath", err)
  50. return
  51. }
  52. if len(treename) != 0 && entry == nil {
  53. ctx.Handle(404, "repo.Home", nil)
  54. return
  55. }
  56. if entry != nil && !entry.IsDir() {
  57. blob := entry.Blob()
  58. if dataRc, err := blob.Data(); err != nil {
  59. ctx.Handle(404, "blob.Data", err)
  60. return
  61. } else {
  62. ctx.Data["FileSize"] = blob.Size()
  63. ctx.Data["IsFile"] = true
  64. ctx.Data["FileName"] = blob.Name()
  65. ctx.Data["HighlightClass"] = template.FileNameToHighlightClass(blob.Name())
  66. ctx.Data["FileLink"] = rawLink + "/" + treename
  67. buf := make([]byte, 1024)
  68. n, _ := dataRc.Read(buf)
  69. if n > 0 {
  70. buf = buf[:n]
  71. }
  72. _, isTextFile := base.IsTextFile(buf)
  73. _, isImageFile := base.IsImageFile(buf)
  74. ctx.Data["IsFileText"] = isTextFile
  75. switch {
  76. case isImageFile:
  77. ctx.Data["IsImageFile"] = true
  78. case isTextFile:
  79. d, _ := ioutil.ReadAll(dataRc)
  80. buf = append(buf, d...)
  81. readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
  82. ctx.Data["ReadmeExist"] = readmeExist
  83. if readmeExist {
  84. ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
  85. } else {
  86. if err, content := template.ToUtf8WithErr(buf); err != nil {
  87. if err != nil {
  88. log.Error(4, "Convert content encoding: %s", err)
  89. }
  90. ctx.Data["FileContent"] = string(buf)
  91. } else {
  92. ctx.Data["FileContent"] = content
  93. }
  94. }
  95. }
  96. }
  97. } else {
  98. // Directory and file list.
  99. tree, err := ctx.Repo.Commit.SubTree(treename)
  100. if err != nil {
  101. ctx.Handle(404, "SubTree", err)
  102. return
  103. }
  104. entries, err := tree.ListEntries()
  105. if err != nil {
  106. ctx.Handle(500, "ListEntries", err)
  107. return
  108. }
  109. entries.Sort()
  110. ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, treePath)
  111. if err != nil {
  112. ctx.Handle(500, "GetCommitsInfo", err)
  113. return
  114. }
  115. var readmeFile *git.Blob
  116. for _, f := range entries {
  117. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  118. continue
  119. } else {
  120. readmeFile = f.Blob()
  121. break
  122. }
  123. }
  124. if readmeFile != nil {
  125. ctx.Data["ReadmeInList"] = true
  126. ctx.Data["ReadmeExist"] = true
  127. if dataRc, err := readmeFile.Data(); err != nil {
  128. ctx.Handle(404, "repo.SinglereadmeFile.Data", err)
  129. return
  130. } else {
  131. buf := make([]byte, 1024)
  132. n, _ := dataRc.Read(buf)
  133. if n > 0 {
  134. buf = buf[:n]
  135. }
  136. ctx.Data["FileSize"] = readmeFile.Size()
  137. ctx.Data["FileLink"] = rawLink + "/" + treename
  138. _, isTextFile := base.IsTextFile(buf)
  139. ctx.Data["FileIsText"] = isTextFile
  140. ctx.Data["FileName"] = readmeFile.Name()
  141. if isTextFile {
  142. d, _ := ioutil.ReadAll(dataRc)
  143. buf = append(buf, d...)
  144. switch {
  145. case base.IsMarkdownFile(readmeFile.Name()):
  146. buf = base.RenderMarkdown(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
  147. default:
  148. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  149. }
  150. ctx.Data["FileContent"] = string(buf)
  151. }
  152. }
  153. }
  154. lastCommit := ctx.Repo.Commit
  155. if len(treePath) > 0 {
  156. c, err := ctx.Repo.Commit.GetCommitByPath(treePath)
  157. if err != nil {
  158. ctx.Handle(500, "GetCommitByPath", err)
  159. return
  160. }
  161. lastCommit = c
  162. }
  163. ctx.Data["LastCommit"] = lastCommit
  164. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  165. }
  166. ctx.Data["Username"] = userName
  167. ctx.Data["Reponame"] = repoName
  168. var treenames []string
  169. Paths := make([]string, 0)
  170. if len(treename) > 0 {
  171. treenames = strings.Split(treename, "/")
  172. for i, _ := range treenames {
  173. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  174. }
  175. ctx.Data["HasParentPath"] = true
  176. if len(Paths)-2 >= 0 {
  177. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  178. }
  179. }
  180. ctx.Data["Paths"] = Paths
  181. ctx.Data["TreeName"] = treename
  182. ctx.Data["Treenames"] = treenames
  183. ctx.Data["TreePath"] = treePath
  184. ctx.Data["BranchLink"] = branchLink
  185. ctx.HTML(200, HOME)
  186. }
  187. func RenderUserCards(ctx *middleware.Context, total int, getter func(page int) ([]*models.User, error), tpl base.TplName) {
  188. page := ctx.QueryInt("page")
  189. if page <= 0 {
  190. page = 1
  191. }
  192. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  193. ctx.Data["Page"] = pager
  194. items, err := getter(pager.Current())
  195. if err != nil {
  196. ctx.Handle(500, "getter", err)
  197. return
  198. }
  199. ctx.Data["Cards"] = items
  200. ctx.HTML(200, tpl)
  201. }
  202. func Watchers(ctx *middleware.Context) {
  203. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  204. ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
  205. ctx.Data["PageIsWatchers"] = true
  206. RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, WATCHERS)
  207. }
  208. func Stars(ctx *middleware.Context) {
  209. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  210. ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
  211. ctx.Data["PageIsStargazers"] = true
  212. RenderUserCards(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers, WATCHERS)
  213. }
  214. func Forks(ctx *middleware.Context) {
  215. ctx.Data["Title"] = ctx.Tr("repos.forks")
  216. forks, err := ctx.Repo.Repository.GetForks()
  217. if err != nil {
  218. ctx.Handle(500, "GetForks", err)
  219. return
  220. }
  221. for _, fork := range forks {
  222. if err = fork.GetOwner(); err != nil {
  223. ctx.Handle(500, "GetOwner", err)
  224. return
  225. }
  226. }
  227. ctx.Data["Forks"] = forks
  228. ctx.HTML(200, FORKS)
  229. }