view.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. "fmt"
  8. gotemplate "html/template"
  9. "io/ioutil"
  10. "path"
  11. "strings"
  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. // Building code view blocks with line number on server side.
  103. var filecontent string
  104. if err, content := template.ToUTF8WithErr(buf); err != nil {
  105. if err != nil {
  106. log.Error(4, "ToUTF8WithErr: %s", err)
  107. }
  108. filecontent = string(buf)
  109. } else {
  110. filecontent = content
  111. }
  112. var output bytes.Buffer
  113. lines := strings.Split(filecontent, "\n")
  114. for index, line := range lines {
  115. output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n")
  116. }
  117. ctx.Data["FileContent"] = gotemplate.HTML(output.String())
  118. output.Reset()
  119. for i := 0; i < len(lines); i++ {
  120. output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
  121. }
  122. ctx.Data["LineNums"] = gotemplate.HTML(output.String())
  123. }
  124. }
  125. }
  126. }
  127. } else {
  128. // Directory and file list.
  129. tree, err := ctx.Repo.Commit.SubTree(treename)
  130. if err != nil {
  131. ctx.Handle(404, "SubTree", err)
  132. return
  133. }
  134. entries, err := tree.ListEntries()
  135. if err != nil {
  136. ctx.Handle(500, "ListEntries", err)
  137. return
  138. }
  139. entries.Sort()
  140. ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, treePath)
  141. if err != nil {
  142. ctx.Handle(500, "GetCommitsInfo", err)
  143. return
  144. }
  145. var readmeFile *git.Blob
  146. for _, f := range entries {
  147. if f.IsDir() || !markdown.IsReadmeFile(f.Name()) {
  148. continue
  149. } else {
  150. readmeFile = f.Blob()
  151. break
  152. }
  153. }
  154. if readmeFile != nil {
  155. ctx.Data["ReadmeInList"] = true
  156. ctx.Data["ReadmeExist"] = true
  157. if dataRc, err := readmeFile.Data(); err != nil {
  158. ctx.Handle(404, "repo.SinglereadmeFile.Data", err)
  159. return
  160. } else {
  161. buf := make([]byte, 1024)
  162. n, _ := dataRc.Read(buf)
  163. if n > 0 {
  164. buf = buf[:n]
  165. }
  166. ctx.Data["FileSize"] = readmeFile.Size()
  167. ctx.Data["FileLink"] = rawLink + "/" + treename
  168. _, isTextFile := base.IsTextFile(buf)
  169. ctx.Data["FileIsText"] = isTextFile
  170. ctx.Data["FileName"] = readmeFile.Name()
  171. if isTextFile {
  172. d, _ := ioutil.ReadAll(dataRc)
  173. buf = append(buf, d...)
  174. switch {
  175. case markdown.IsMarkdownFile(readmeFile.Name()):
  176. buf = markdown.Render(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
  177. default:
  178. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  179. }
  180. ctx.Data["FileContent"] = string(buf)
  181. }
  182. }
  183. }
  184. lastCommit := ctx.Repo.Commit
  185. if len(treePath) > 0 {
  186. c, err := ctx.Repo.Commit.GetCommitByPath(treePath)
  187. if err != nil {
  188. ctx.Handle(500, "GetCommitByPath", err)
  189. return
  190. }
  191. lastCommit = c
  192. }
  193. ctx.Data["LastCommit"] = lastCommit
  194. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  195. }
  196. ctx.Data["Username"] = userName
  197. ctx.Data["Reponame"] = repoName
  198. var treenames []string
  199. paths := make([]string, 0)
  200. if len(treename) > 0 {
  201. treenames = strings.Split(treename, "/")
  202. for i := range treenames {
  203. paths = append(paths, strings.Join(treenames[0:i+1], "/"))
  204. }
  205. ctx.Data["HasParentPath"] = true
  206. if len(paths)-2 >= 0 {
  207. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  208. }
  209. }
  210. ctx.Data["Paths"] = paths
  211. ctx.Data["TreeName"] = treename
  212. ctx.Data["Treenames"] = treenames
  213. ctx.Data["TreePath"] = treePath
  214. ctx.Data["BranchLink"] = branchLink
  215. ctx.HTML(200, HOME)
  216. }
  217. func RenderUserCards(ctx *context.Context, total int, getter func(page int) ([]*models.User, error), tpl base.TplName) {
  218. page := ctx.QueryInt("page")
  219. if page <= 0 {
  220. page = 1
  221. }
  222. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  223. ctx.Data["Page"] = pager
  224. items, err := getter(pager.Current())
  225. if err != nil {
  226. ctx.Handle(500, "getter", err)
  227. return
  228. }
  229. ctx.Data["Cards"] = items
  230. ctx.HTML(200, tpl)
  231. }
  232. func Watchers(ctx *context.Context) {
  233. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  234. ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
  235. ctx.Data["PageIsWatchers"] = true
  236. RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, WATCHERS)
  237. }
  238. func Stars(ctx *context.Context) {
  239. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  240. ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
  241. ctx.Data["PageIsStargazers"] = true
  242. RenderUserCards(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers, WATCHERS)
  243. }
  244. func Forks(ctx *context.Context) {
  245. ctx.Data["Title"] = ctx.Tr("repos.forks")
  246. forks, err := ctx.Repo.Repository.GetForks()
  247. if err != nil {
  248. ctx.Handle(500, "GetForks", err)
  249. return
  250. }
  251. for _, fork := range forks {
  252. if err = fork.GetOwner(); err != nil {
  253. ctx.Handle(500, "GetOwner", err)
  254. return
  255. }
  256. }
  257. ctx.Data["Forks"] = forks
  258. ctx.HTML(200, FORKS)
  259. }