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