file.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/G-Node/git-module"
  7. "github.com/G-Node/gogs/models"
  8. "github.com/G-Node/gogs/pkg/context"
  9. "github.com/G-Node/gogs/routes/repo"
  10. "github.com/go-macaron/captcha"
  11. )
  12. // https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-raw-content
  13. func GetRawFile(c *context.APIContext) {
  14. if !c.Repo.HasAccess() {
  15. c.Status(404)
  16. return
  17. }
  18. if c.Repo.Repository.IsBare {
  19. c.Status(404)
  20. return
  21. }
  22. blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
  23. if err != nil {
  24. if git.IsErrNotExist(err) {
  25. c.Status(404)
  26. } else {
  27. c.Error(500, "GetBlobByPath", err)
  28. }
  29. return
  30. }
  31. cp := captcha.NewCaptcha(captcha.Options{})
  32. if err = repo.ServeBlob(c.Context, blob, cp); err != nil {
  33. c.Error(500, "ServeBlob", err)
  34. }
  35. }
  36. // https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-archive
  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. func GetEditorconfig(c *context.APIContext) {
  48. ec, err := c.Repo.GetEditorconfig()
  49. if err != nil {
  50. if git.IsErrNotExist(err) {
  51. c.Error(404, "GetEditorconfig", err)
  52. } else {
  53. c.Error(500, "GetEditorconfig", err)
  54. }
  55. return
  56. }
  57. fileName := c.Params("filename")
  58. def := ec.GetDefinitionForFilename(fileName)
  59. if def == nil {
  60. c.Error(404, "GetDefinitionForFilename", err)
  61. return
  62. }
  63. c.JSON(200, def)
  64. }