file.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/middleware"
  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 *middleware.Context) {
  13. if !ctx.Repo.HasAccess() {
  14. ctx.Error(404)
  15. return
  16. }
  17. blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreeName)
  18. if err != nil {
  19. if git.IsErrNotExist(err) {
  20. ctx.Error(404)
  21. } else {
  22. ctx.APIError(500, "GetBlobByPath", err)
  23. }
  24. return
  25. }
  26. if err = repo.ServeBlob(ctx, blob); err != nil {
  27. ctx.APIError(500, "ServeBlob", err)
  28. }
  29. }
  30. // https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-archive
  31. func GetArchive(ctx *middleware.Context) {
  32. repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
  33. gitRepo, err := git.OpenRepository(repoPath)
  34. if err != nil {
  35. ctx.APIError(500, "OpenRepository", err)
  36. return
  37. }
  38. ctx.Repo.GitRepo = gitRepo
  39. repo.Download(ctx)
  40. }