file.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. "gitote/gitote/routes/repo"
  11. "gitlab.com/gitote/git-module"
  12. )
  13. // GetRawFile get a file by path on a repository
  14. func GetRawFile(c *context.APIContext) {
  15. if !c.Repo.HasAccess() {
  16. c.Status(404)
  17. return
  18. }
  19. if c.Repo.Repository.IsBare {
  20. c.Status(404)
  21. return
  22. }
  23. blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
  24. if err != nil {
  25. if git.IsErrNotExist(err) {
  26. c.Status(404)
  27. } else {
  28. c.Error(500, "GetBlobByPath", err)
  29. }
  30. return
  31. }
  32. if err = repo.ServeBlob(c.Context, blob); err != nil {
  33. c.Error(500, "ServeBlob", err)
  34. }
  35. }
  36. // GetArchive get archive of a repository
  37. func GetArchive(c *context.APIContext) {
  38. repoPath := models.RepoPath(c.Params(":username"), c.Params(":reponame"))
  39. gitRepo, err := git.OpenRepository(repoPath)
  40. if err != nil {
  41. c.Error(500, "OpenRepository", err)
  42. return
  43. }
  44. c.Repo.GitRepo = gitRepo
  45. repo.Download(c.Context)
  46. }
  47. // GetEditorconfig get editor config of a repository
  48. func GetEditorconfig(c *context.APIContext) {
  49. ec, err := c.Repo.GetEditorconfig()
  50. if err != nil {
  51. if git.IsErrNotExist(err) {
  52. c.Error(404, "GetEditorconfig", err)
  53. } else {
  54. c.Error(500, "GetEditorconfig", err)
  55. }
  56. return
  57. }
  58. fileName := c.Params("filename")
  59. def := ec.GetDefinitionForFilename(fileName)
  60. if def == nil {
  61. c.Error(404, "GetDefinitionForFilename", err)
  62. return
  63. }
  64. c.JSON(200, def)
  65. }