view.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. gotemplate "html/template"
  9. "io/ioutil"
  10. "path"
  11. "strings"
  12. "github.com/Unknwon/paginater"
  13. "github.com/gogits/git-module"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/base"
  16. "github.com/gogits/gogs/modules/context"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/markdown"
  19. "github.com/gogits/gogs/modules/setting"
  20. "github.com/gogits/gogs/modules/template"
  21. "github.com/gogits/gogs/modules/template/highlight"
  22. )
  23. const (
  24. HOME base.TplName = "repo/home"
  25. WATCHERS base.TplName = "repo/watchers"
  26. FORKS base.TplName = "repo/forks"
  27. )
  28. func Home(ctx *context.Context) {
  29. title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
  30. if len(ctx.Repo.Repository.Description) > 0 {
  31. title += ": " + ctx.Repo.Repository.Description
  32. }
  33. ctx.Data["Title"] = title
  34. ctx.Data["PageIsViewCode"] = true
  35. ctx.Data["RequireHighlightJS"] = true
  36. branchName := ctx.Repo.BranchName
  37. userName := ctx.Repo.Owner.Name
  38. repoName := ctx.Repo.Repository.Name
  39. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  40. treeLink := branchLink
  41. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  42. // uploadFileLink := ctx.Repo.RepoLink + "/upload/" + branchName
  43. treePath := ctx.Repo.TreePath
  44. if len(treePath) > 0 {
  45. treeLink += "/" + treePath
  46. }
  47. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treePath)
  48. if err != nil {
  49. if git.IsErrNotExist(err) {
  50. ctx.Handle(404, "GetTreeEntryByPath", err)
  51. } else {
  52. ctx.Handle(500, "GetTreeEntryByPath", err)
  53. }
  54. return
  55. }
  56. if !entry.IsDir() {
  57. blob := entry.Blob()
  58. dataRc, err := blob.Data()
  59. if err != nil {
  60. ctx.Handle(404, "blob.Data", err)
  61. return
  62. }
  63. ctx.Data["FileSize"] = blob.Size()
  64. ctx.Data["IsFile"] = true
  65. ctx.Data["FileName"] = blob.Name()
  66. ctx.Data["HighlightClass"] = highlight.FileNameToHighlightClass(blob.Name())
  67. ctx.Data["FileLink"] = rawLink + "/" + treePath
  68. buf := make([]byte, 1024)
  69. n, _ := dataRc.Read(buf)
  70. if n > 0 {
  71. buf = buf[:n]
  72. }
  73. _, isTextFile := base.IsTextFile(buf)
  74. _, isImageFile := base.IsImageFile(buf)
  75. _, isPDFFile := base.IsPDFFile(buf)
  76. ctx.Data["IsFileText"] = isTextFile
  77. // Assume file is not editable first.
  78. if !isTextFile {
  79. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
  80. }
  81. switch {
  82. case isPDFFile:
  83. ctx.Data["IsPDFFile"] = true
  84. case isImageFile:
  85. ctx.Data["IsImageFile"] = true
  86. case isTextFile:
  87. if blob.Size() >= setting.UI.MaxDisplayFileSize {
  88. ctx.Data["IsFileTooLarge"] = true
  89. } else {
  90. d, _ := ioutil.ReadAll(dataRc)
  91. buf = append(buf, d...)
  92. isMarkdown := markdown.IsMarkdownFile(blob.Name())
  93. ctx.Data["IsMarkdown"] = isMarkdown
  94. readmeExist := isMarkdown || markdown.IsReadmeFile(blob.Name())
  95. ctx.Data["ReadmeExist"] = readmeExist
  96. if readmeExist {
  97. // TODO: don't need to render if it's a README but not Markdown file.
  98. ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
  99. } else {
  100. // Building code view blocks with line number on server side.
  101. var filecontent string
  102. if err, content := template.ToUTF8WithErr(buf); err != nil {
  103. if err != nil {
  104. log.Error(4, "ToUTF8WithErr: %s", err)
  105. }
  106. filecontent = string(buf)
  107. } else {
  108. filecontent = content
  109. }
  110. var output bytes.Buffer
  111. lines := strings.Split(filecontent, "\n")
  112. for index, line := range lines {
  113. output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n")
  114. }
  115. ctx.Data["FileContent"] = gotemplate.HTML(output.String())
  116. output.Reset()
  117. for i := 0; i < len(lines); i++ {
  118. output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
  119. }
  120. ctx.Data["LineNums"] = gotemplate.HTML(output.String())
  121. }
  122. }
  123. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  124. ctx.Data["CanEditFile"] = true
  125. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
  126. } else if !ctx.Repo.IsViewBranch {
  127. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  128. } else if !ctx.Repo.IsWriter() {
  129. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
  130. }
  131. }
  132. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  133. ctx.Data["CanDeleteFile"] = true
  134. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.delete_this_file")
  135. } else if !ctx.Repo.IsViewBranch {
  136. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  137. } else if !ctx.Repo.IsWriter() {
  138. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_have_write_access")
  139. }
  140. } else {
  141. // Directory and file list.
  142. tree, err := ctx.Repo.Commit.SubTree(treePath)
  143. if err != nil {
  144. ctx.Handle(404, "SubTree", err)
  145. return
  146. }
  147. entries, err := tree.ListEntries()
  148. if err != nil {
  149. ctx.Handle(500, "ListEntries", err)
  150. return
  151. }
  152. entries.Sort()
  153. ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, treePath)
  154. if err != nil {
  155. ctx.Handle(500, "GetCommitsInfo", err)
  156. return
  157. }
  158. var readmeFile *git.Blob
  159. for _, f := range entries {
  160. if f.IsDir() || !markdown.IsReadmeFile(f.Name()) {
  161. continue
  162. } else {
  163. readmeFile = f.Blob()
  164. break
  165. }
  166. }
  167. if readmeFile != nil {
  168. ctx.Data["ReadmeInList"] = true
  169. ctx.Data["ReadmeExist"] = true
  170. if dataRc, err := readmeFile.Data(); err != nil {
  171. ctx.Handle(404, "repo.SinglereadmeFile.Data", err)
  172. return
  173. } else {
  174. buf := make([]byte, 1024)
  175. n, _ := dataRc.Read(buf)
  176. if n > 0 {
  177. buf = buf[:n]
  178. }
  179. ctx.Data["FileSize"] = readmeFile.Size()
  180. ctx.Data["FileLink"] = rawLink + "/" + treePath
  181. _, isTextFile := base.IsTextFile(buf)
  182. ctx.Data["FileIsText"] = isTextFile
  183. ctx.Data["FileName"] = readmeFile.Name()
  184. if isTextFile {
  185. d, _ := ioutil.ReadAll(dataRc)
  186. buf = append(buf, d...)
  187. switch {
  188. case markdown.IsMarkdownFile(readmeFile.Name()):
  189. ctx.Data["IsMarkdown"] = true
  190. buf = markdown.Render(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
  191. default:
  192. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  193. }
  194. ctx.Data["FileContent"] = string(buf)
  195. }
  196. }
  197. }
  198. lastCommit := ctx.Repo.Commit
  199. if len(treePath) > 0 {
  200. c, err := ctx.Repo.Commit.GetCommitByPath(treePath)
  201. if err != nil {
  202. ctx.Handle(500, "GetCommitByPath", err)
  203. return
  204. }
  205. lastCommit = c
  206. }
  207. ctx.Data["LastCommit"] = lastCommit
  208. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  209. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  210. ctx.Data["CanAddFile"] = true
  211. // if setting.Repository.Upload.Enabled {
  212. // ctx.Data["UploadFileLink"] = uploadFileLink + "/" + treePath
  213. // }
  214. }
  215. }
  216. ctx.Data["Username"] = userName
  217. ctx.Data["Reponame"] = repoName
  218. ec, err := ctx.Repo.GetEditorconfig()
  219. if err != nil && !git.IsErrNotExist(err) {
  220. ctx.Handle(500, "ErrGettingEditorconfig", err)
  221. return
  222. }
  223. ctx.Data["Editorconfig"] = ec
  224. var treenames []string
  225. paths := make([]string, 0)
  226. if len(treePath) > 0 {
  227. treenames = strings.Split(treePath, "/")
  228. for i := range treenames {
  229. paths = append(paths, strings.Join(treenames[0:i+1], "/"))
  230. }
  231. ctx.Data["HasParentPath"] = true
  232. if len(paths)-2 >= 0 {
  233. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  234. }
  235. }
  236. ctx.Data["Paths"] = paths
  237. ctx.Data["TreePath"] = treePath
  238. ctx.Data["TreeLink"] = treeLink
  239. ctx.Data["Treenames"] = treenames
  240. ctx.Data["BranchLink"] = branchLink
  241. ctx.HTML(200, HOME)
  242. }
  243. func RenderUserCards(ctx *context.Context, total int, getter func(page int) ([]*models.User, error), tpl base.TplName) {
  244. page := ctx.QueryInt("page")
  245. if page <= 0 {
  246. page = 1
  247. }
  248. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  249. ctx.Data["Page"] = pager
  250. items, err := getter(pager.Current())
  251. if err != nil {
  252. ctx.Handle(500, "getter", err)
  253. return
  254. }
  255. ctx.Data["Cards"] = items
  256. ctx.HTML(200, tpl)
  257. }
  258. func Watchers(ctx *context.Context) {
  259. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  260. ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
  261. ctx.Data["PageIsWatchers"] = true
  262. RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, WATCHERS)
  263. }
  264. func Stars(ctx *context.Context) {
  265. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  266. ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
  267. ctx.Data["PageIsStargazers"] = true
  268. RenderUserCards(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers, WATCHERS)
  269. }
  270. func Forks(ctx *context.Context) {
  271. ctx.Data["Title"] = ctx.Tr("repos.forks")
  272. forks, err := ctx.Repo.Repository.GetForks()
  273. if err != nil {
  274. ctx.Handle(500, "GetForks", err)
  275. return
  276. }
  277. for _, fork := range forks {
  278. if err = fork.GetOwner(); err != nil {
  279. ctx.Handle(500, "GetOwner", err)
  280. return
  281. }
  282. }
  283. ctx.Data["Forks"] = forks
  284. ctx.HTML(200, FORKS)
  285. }