gitea源码

download.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "errors"
  6. "net/http"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/services/context"
  9. archiver_service "code.gitea.io/gitea/services/repository/archiver"
  10. )
  11. func serveRepoArchive(ctx *context.APIContext, reqFileName string) {
  12. aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, reqFileName)
  13. if err != nil {
  14. if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
  15. ctx.APIError(http.StatusBadRequest, err)
  16. } else if errors.Is(err, archiver_service.RepoRefNotFoundError{}) {
  17. ctx.APIError(http.StatusNotFound, err)
  18. } else {
  19. ctx.APIErrorInternal(err)
  20. }
  21. return
  22. }
  23. archiver_service.ServeRepoArchive(ctx.Base, ctx.Repo.Repository, ctx.Repo.GitRepo, aReq)
  24. }
  25. func DownloadArchive(ctx *context.APIContext) {
  26. var tp git.ArchiveType
  27. switch ballType := ctx.PathParam("ball_type"); ballType {
  28. case "tarball":
  29. tp = git.ArchiveTarGz
  30. case "zipball":
  31. tp = git.ArchiveZip
  32. case "bundle":
  33. tp = git.ArchiveBundle
  34. default:
  35. ctx.APIError(http.StatusBadRequest, "Unknown archive type: "+ballType)
  36. return
  37. }
  38. serveRepoArchive(ctx, ctx.PathParam("*")+"."+tp.String())
  39. }