You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
5.3 KiB

10 years ago
10 years ago
  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. "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. if err != nil {
  99. log.Error(4, "Convert content encoding: %s", err)
  100. }
  101. ctx.Data["FileContent"] = string(buf)
  102. } else {
  103. ctx.Data["FileContent"] = content
  104. }
  105. }
  106. }
  107. }
  108. } else {
  109. // Directory and file list.
  110. tree, err := ctx.Repo.Commit.SubTree(treename)
  111. if err != nil {
  112. ctx.Handle(404, "SubTree", err)
  113. return
  114. }
  115. entries, err := tree.ListEntries(treename)
  116. if err != nil {
  117. ctx.Handle(500, "ListEntries", err)
  118. return
  119. }
  120. entries.Sort()
  121. files := make([][]interface{}, 0, len(entries))
  122. for _, te := range entries {
  123. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  124. if err != nil {
  125. ctx.Handle(404, "GetCommitOfRelPath", err)
  126. return
  127. }
  128. files = append(files, []interface{}{te, c})
  129. }
  130. ctx.Data["Files"] = files
  131. var readmeFile *git.Blob
  132. for _, f := range entries {
  133. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  134. continue
  135. } else {
  136. readmeFile = f.Blob()
  137. break
  138. }
  139. }
  140. if readmeFile != nil {
  141. ctx.Data["ReadmeInHome"] = true
  142. ctx.Data["ReadmeExist"] = true
  143. if dataRc, err := readmeFile.Data(); err != nil {
  144. ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
  145. return
  146. } else {
  147. buf := make([]byte, 1024)
  148. n, _ := dataRc.Read(buf)
  149. if n > 0 {
  150. buf = buf[:n]
  151. }
  152. ctx.Data["FileSize"] = readmeFile.Size()
  153. ctx.Data["FileLink"] = rawLink + "/" + treename
  154. _, isTextFile := base.IsTextFile(buf)
  155. ctx.Data["FileIsText"] = isTextFile
  156. ctx.Data["FileName"] = readmeFile.Name()
  157. if isTextFile {
  158. d, _ := ioutil.ReadAll(dataRc)
  159. buf = append(buf, d...)
  160. switch {
  161. case base.IsMarkdownFile(readmeFile.Name()):
  162. buf = base.RenderMarkdown(buf, branchLink)
  163. default:
  164. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  165. }
  166. ctx.Data["FileContent"] = string(buf)
  167. }
  168. }
  169. }
  170. }
  171. ctx.Data["Username"] = userName
  172. ctx.Data["Reponame"] = repoName
  173. var treenames []string
  174. Paths := make([]string, 0)
  175. if len(treename) > 0 {
  176. treenames = strings.Split(treename, "/")
  177. for i, _ := range treenames {
  178. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  179. }
  180. ctx.Data["HasParentPath"] = true
  181. if len(Paths)-2 >= 0 {
  182. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  183. }
  184. }
  185. ctx.Data["LastCommit"] = ctx.Repo.Commit
  186. ctx.Data["Paths"] = Paths
  187. ctx.Data["TreeName"] = treename
  188. ctx.Data["Treenames"] = treenames
  189. ctx.Data["TreePath"] = treePath
  190. ctx.Data["BranchLink"] = branchLink
  191. ctx.HTML(200, HOME)
  192. }