view.go 6.8 KB

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