file.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "github.com/gogs/git-module"
  7. "github.com/gogs/gogs/models"
  8. "github.com/gogs/gogs/pkg/context"
  9. "github.com/gogs/gogs/routes/repo"
  10. )
  11. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Contents#download-raw-content
  12. func GetRawFile(c *context.APIContext) {
  13. if !c.Repo.HasAccess() {
  14. c.Status(404)
  15. return
  16. }
  17. if c.Repo.Repository.IsBare {
  18. c.Status(404)
  19. return
  20. }
  21. blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
  22. if err != nil {
  23. if git.IsErrNotExist(err) {
  24. c.Status(404)
  25. } else {
  26. c.Error(500, "GetBlobByPath", err)
  27. }
  28. return
  29. }
  30. if err = repo.ServeBlob(c.Context, blob); err != nil {
  31. c.Error(500, "ServeBlob", err)
  32. }
  33. }
  34. // https://github.com/gogs/go-gogs-client/wiki/Repositories-Contents#download-archive
  35. func GetArchive(c *context.APIContext) {
  36. repoPath := models.RepoPath(c.Params(":username"), c.Params(":reponame"))
  37. gitRepo, err := git.OpenRepository(repoPath)
  38. if err != nil {
  39. c.Error(500, "OpenRepository", err)
  40. return
  41. }
  42. c.Repo.GitRepo = gitRepo
  43. repo.Download(c.Context)
  44. }
  45. func GetEditorconfig(c *context.APIContext) {
  46. ec, err := c.Repo.GetEditorconfig()
  47. if err != nil {
  48. if git.IsErrNotExist(err) {
  49. c.Error(404, "GetEditorconfig", err)
  50. } else {
  51. c.Error(500, "GetEditorconfig", err)
  52. }
  53. return
  54. }
  55. fileName := c.Params("filename")
  56. def := ec.GetDefinitionForFilename(fileName)
  57. if def == nil {
  58. c.Error(404, "GetDefinitionForFilename", err)
  59. return
  60. }
  61. c.JSON(200, def)
  62. }