view.go 7.7 KB

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