view.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. "path/filepath"
  10. "strings"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/git"
  13. "github.com/gogits/gogs/modules/middleware"
  14. )
  15. const (
  16. HOME base.TplName = "repo/home"
  17. )
  18. func Home(ctx *middleware.Context) {
  19. ctx.Data["Title"] = ctx.Repo.Repository.Name
  20. branchName := ctx.Repo.BranchName
  21. userName := ctx.Repo.Owner.Name
  22. repoName := ctx.Repo.Repository.Name
  23. repoLink := ctx.Repo.RepoLink
  24. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  25. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  26. // Get tree path
  27. treename := ctx.Params("*")
  28. if len(treename) > 0 && treename[len(treename)-1] == '/' {
  29. ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
  30. return
  31. }
  32. ctx.Data["IsRepoToolbarSource"] = true
  33. isViewBranch := ctx.Repo.IsBranch
  34. ctx.Data["IsViewBranch"] = isViewBranch
  35. treePath := treename
  36. if len(treePath) != 0 {
  37. treePath = treePath + "/"
  38. }
  39. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
  40. if err != nil && err != git.ErrNotExist {
  41. ctx.Handle(404, "GetTreeEntryByPath", err)
  42. return
  43. }
  44. if len(treename) != 0 && entry == nil {
  45. ctx.Handle(404, "repo.Home", nil)
  46. return
  47. }
  48. if entry != nil && !entry.IsDir() {
  49. blob := entry.Blob()
  50. if dataRc, err := blob.Data(); err != nil {
  51. ctx.Handle(404, "blob.Data", err)
  52. return
  53. } else {
  54. ctx.Data["FileSize"] = blob.Size()
  55. ctx.Data["IsFile"] = true
  56. ctx.Data["FileName"] = blob.Name()
  57. ext := path.Ext(blob.Name())
  58. if len(ext) > 0 {
  59. ext = ext[1:]
  60. }
  61. ctx.Data["FileExt"] = ext
  62. ctx.Data["FileLink"] = rawLink + "/" + treename
  63. buf := make([]byte, 1024)
  64. n, _ := dataRc.Read(buf)
  65. if n > 0 {
  66. buf = buf[:n]
  67. }
  68. _, isTextFile := base.IsTextFile(buf)
  69. _, isImageFile := base.IsImageFile(buf)
  70. ctx.Data["IsFileText"] = isTextFile
  71. switch {
  72. case isImageFile:
  73. ctx.Data["IsImageFile"] = true
  74. case isTextFile:
  75. d, _ := ioutil.ReadAll(dataRc)
  76. buf = append(buf, d...)
  77. readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
  78. ctx.Data["ReadmeExist"] = readmeExist
  79. if readmeExist {
  80. ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, ""))
  81. } else {
  82. ctx.Data["FileContent"] = string(buf)
  83. }
  84. }
  85. }
  86. } else {
  87. // Directory and file list.
  88. tree, err := ctx.Repo.Commit.SubTree(treename)
  89. if err != nil {
  90. ctx.Handle(404, "SubTree", err)
  91. return
  92. }
  93. entries, err := tree.ListEntries(treename)
  94. if err != nil {
  95. ctx.Handle(500, "ListEntries", err)
  96. return
  97. }
  98. entries.Sort()
  99. files := make([][]interface{}, 0, len(entries))
  100. for _, te := range entries {
  101. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  102. if err != nil {
  103. ctx.Handle(404, "GetCommitOfRelPath", err)
  104. return
  105. }
  106. files = append(files, []interface{}{te, c})
  107. }
  108. ctx.Data["Files"] = files
  109. var readmeFile *git.Blob
  110. for _, f := range entries {
  111. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  112. continue
  113. } else {
  114. readmeFile = f.Blob()
  115. break
  116. }
  117. }
  118. if readmeFile != nil {
  119. ctx.Data["ReadmeInHome"] = true
  120. ctx.Data["ReadmeExist"] = true
  121. if dataRc, err := readmeFile.Data(); err != nil {
  122. ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
  123. return
  124. } else {
  125. buf := make([]byte, 1024)
  126. n, _ := dataRc.Read(buf)
  127. if n > 0 {
  128. buf = buf[:n]
  129. }
  130. ctx.Data["FileSize"] = readmeFile.Size()
  131. ctx.Data["FileLink"] = rawLink + "/" + treename
  132. _, isTextFile := base.IsTextFile(buf)
  133. ctx.Data["FileIsText"] = isTextFile
  134. ctx.Data["FileName"] = readmeFile.Name()
  135. if isTextFile {
  136. d, _ := ioutil.ReadAll(dataRc)
  137. buf = append(buf, d...)
  138. switch {
  139. case base.IsMarkdownFile(readmeFile.Name()):
  140. buf = base.RenderMarkdown(buf, branchLink)
  141. default:
  142. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  143. }
  144. ctx.Data["FileContent"] = string(buf)
  145. }
  146. }
  147. }
  148. }
  149. ctx.Data["Username"] = userName
  150. ctx.Data["Reponame"] = repoName
  151. var treenames []string
  152. Paths := make([]string, 0)
  153. if len(treename) > 0 {
  154. treenames = strings.Split(treename, "/")
  155. for i, _ := range treenames {
  156. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  157. }
  158. ctx.Data["HasParentPath"] = true
  159. if len(Paths)-2 >= 0 {
  160. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  161. }
  162. }
  163. ctx.Data["LastCommit"] = ctx.Repo.Commit
  164. ctx.Data["Paths"] = Paths
  165. ctx.Data["TreeName"] = treename
  166. ctx.Data["Treenames"] = treenames
  167. ctx.Data["TreePath"] = treePath
  168. ctx.Data["BranchLink"] = branchLink
  169. ctx.HTML(200, HOME)
  170. }