gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "net/http"
  6. actions_model "code.gitea.io/gitea/models/actions"
  7. "code.gitea.io/gitea/modules/setting"
  8. "code.gitea.io/gitea/modules/storage"
  9. "code.gitea.io/gitea/services/context"
  10. )
  11. // Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
  12. // The v4 backend ensures ContentEncoding is set to "application/zip", which is not the case for the old backend
  13. func IsArtifactV4(art *actions_model.ActionArtifact) bool {
  14. return art.ArtifactName+".zip" == art.ArtifactPath && art.ContentEncoding == "application/zip"
  15. }
  16. func DownloadArtifactV4ServeDirectOnly(ctx *context.Base, art *actions_model.ActionArtifact) (bool, error) {
  17. if setting.Actions.ArtifactStorage.ServeDirect() {
  18. u, err := storage.ActionsArtifacts.URL(art.StoragePath, art.ArtifactPath, ctx.Req.Method, nil)
  19. if u != nil && err == nil {
  20. ctx.Redirect(u.String(), http.StatusFound)
  21. return true, nil
  22. }
  23. }
  24. return false, nil
  25. }
  26. func DownloadArtifactV4Fallback(ctx *context.Base, art *actions_model.ActionArtifact) error {
  27. f, err := storage.ActionsArtifacts.Open(art.StoragePath)
  28. if err != nil {
  29. return err
  30. }
  31. defer f.Close()
  32. http.ServeContent(ctx.Resp, ctx.Req, art.ArtifactName+".zip", art.CreatedUnix.AsLocalTime(), f)
  33. return nil
  34. }
  35. func DownloadArtifactV4(ctx *context.Base, art *actions_model.ActionArtifact) error {
  36. ok, err := DownloadArtifactV4ServeDirectOnly(ctx, art)
  37. if ok || err != nil {
  38. return err
  39. }
  40. return DownloadArtifactV4Fallback(ctx, art)
  41. }