file.go 1.6 KB

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