view.go 6.9 KB

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