view.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "time"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/git"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/middleware"
  16. )
  17. const (
  18. HOME base.TplName = "repo/home"
  19. )
  20. type fakeCommit struct {
  21. Id string
  22. Summary string
  23. Url string
  24. Committer struct {
  25. When time.Time
  26. }
  27. }
  28. func Home(ctx *middleware.Context) {
  29. ctx.Data["Title"] = ctx.Repo.Repository.Name
  30. branchName := ctx.Repo.BranchName
  31. userName := ctx.Repo.Owner.Name
  32. repoName := ctx.Repo.Repository.Name
  33. repoLink := ctx.Repo.RepoLink
  34. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  35. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  36. // Get tree path
  37. treename := ctx.Params("*")
  38. if len(treename) > 0 && treename[len(treename)-1] == '/' {
  39. ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
  40. return
  41. }
  42. ctx.Data["IsRepoToolbarSource"] = true
  43. isViewBranch := ctx.Repo.IsBranch
  44. ctx.Data["IsViewBranch"] = isViewBranch
  45. treePath := treename
  46. if len(treePath) != 0 {
  47. treePath = treePath + "/"
  48. }
  49. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
  50. if err != nil && err != git.ErrNotExist {
  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. ext := path.Ext(blob.Name())
  68. if len(ext) > 0 {
  69. ext = ext[1:]
  70. }
  71. ctx.Data["FileExt"] = ext
  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. ctx.Data["IsFileText"] = isTextFile
  81. switch {
  82. case isImageFile:
  83. ctx.Data["IsImageFile"] = true
  84. case isTextFile:
  85. d, _ := ioutil.ReadAll(dataRc)
  86. buf = append(buf, d...)
  87. readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
  88. ctx.Data["ReadmeExist"] = readmeExist
  89. if readmeExist {
  90. ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, ""))
  91. } else {
  92. if err, content := base.ToUtf8WithErr(buf); err != nil {
  93. if err != nil {
  94. log.Error(4, "Convert content encoding: %s", err)
  95. }
  96. ctx.Data["FileContent"] = string(buf)
  97. } else {
  98. ctx.Data["FileContent"] = content
  99. }
  100. }
  101. }
  102. }
  103. } else {
  104. // Directory and file list.
  105. tree, err := ctx.Repo.Commit.SubTree(treename)
  106. if err != nil {
  107. ctx.Handle(404, "SubTree", err)
  108. return
  109. }
  110. entries, err := tree.ListEntries(treename)
  111. if err != nil {
  112. ctx.Handle(500, "ListEntries", err)
  113. return
  114. }
  115. entries.Sort()
  116. files := make([][]interface{}, 0, len(entries))
  117. for _, te := range entries {
  118. if te.Type != git.COMMIT {
  119. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  120. if err != nil {
  121. ctx.Handle(404, "GetCommitOfRelPath", err)
  122. return
  123. }
  124. files = append(files, []interface{}{te, c})
  125. } else {
  126. sm, err := ctx.Repo.Commit.GetSubModule(path.Join(treename, te.Name()))
  127. if err != nil {
  128. ctx.Handle(404, "GetSubModule", err)
  129. return
  130. }
  131. commit := git.Commit{
  132. Tree: *tree,
  133. Id: te.Id,
  134. Committer: &git.Signature{
  135. When: time.Now(),
  136. },
  137. CommitMessage: sm.Url,
  138. }
  139. files = append(files, []interface{}{te, &commit})
  140. }
  141. }
  142. ctx.Data["Files"] = files
  143. var readmeFile *git.Blob
  144. for _, f := range entries {
  145. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  146. continue
  147. } else {
  148. readmeFile = f.Blob()
  149. break
  150. }
  151. }
  152. if readmeFile != nil {
  153. ctx.Data["ReadmeInHome"] = true
  154. ctx.Data["ReadmeExist"] = true
  155. if dataRc, err := readmeFile.Data(); err != nil {
  156. ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
  157. return
  158. } else {
  159. buf := make([]byte, 1024)
  160. n, _ := dataRc.Read(buf)
  161. if n > 0 {
  162. buf = buf[:n]
  163. }
  164. ctx.Data["FileSize"] = readmeFile.Size()
  165. ctx.Data["FileLink"] = rawLink + "/" + treename
  166. _, isTextFile := base.IsTextFile(buf)
  167. ctx.Data["FileIsText"] = isTextFile
  168. ctx.Data["FileName"] = readmeFile.Name()
  169. if isTextFile {
  170. d, _ := ioutil.ReadAll(dataRc)
  171. buf = append(buf, d...)
  172. switch {
  173. case base.IsMarkdownFile(readmeFile.Name()):
  174. buf = base.RenderMarkdown(buf, branchLink)
  175. default:
  176. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  177. }
  178. ctx.Data["FileContent"] = string(buf)
  179. }
  180. }
  181. }
  182. }
  183. ctx.Data["Username"] = userName
  184. ctx.Data["Reponame"] = repoName
  185. var treenames []string
  186. Paths := make([]string, 0)
  187. if len(treename) > 0 {
  188. treenames = strings.Split(treename, "/")
  189. for i, _ := range treenames {
  190. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  191. }
  192. ctx.Data["HasParentPath"] = true
  193. if len(Paths)-2 >= 0 {
  194. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  195. }
  196. }
  197. ctx.Data["LastCommit"] = ctx.Repo.Commit
  198. ctx.Data["Paths"] = Paths
  199. ctx.Data["TreeName"] = treename
  200. ctx.Data["Treenames"] = treenames
  201. ctx.Data["TreePath"] = treePath
  202. ctx.Data["BranchLink"] = branchLink
  203. ctx.HTML(200, HOME)
  204. }