view.go 5.0 KB

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