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