view.go 5.3 KB

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