@@ -6,16 +6,15 @@ package repo
66import (
77 "errors"
88 "fmt"
9- "io"
109 "net/http"
1110 "net/url"
1211 "strings"
1312
1413 actions_model "code.gitea.io/gitea/models/actions"
1514 "code.gitea.io/gitea/models/db"
1615 secret_model "code.gitea.io/gitea/models/secret"
16+ "code.gitea.io/gitea/modules/actions"
1717 "code.gitea.io/gitea/modules/setting"
18- "code.gitea.io/gitea/modules/storage"
1918 api "code.gitea.io/gitea/modules/structs"
2019 "code.gitea.io/gitea/modules/util"
2120 "code.gitea.io/gitea/modules/web"
@@ -620,11 +619,10 @@ func GetArtifactsOfRun(ctx *context.APIContext) {
620619 runID := ctx .PathParamInt64 ("run" )
621620
622621 artifacts , total , err := db .FindAndCount [actions_model.ActionArtifact ](ctx , actions_model.FindArtifactsOptions {
623- RepoID : repoID ,
624- RunID : runID ,
625- ArtifactName : artifactName ,
626- // Status: int(actions_model.ArtifactStatusUploadConfirmed),
627- FinalizedArtifactsV2 : true ,
622+ RepoID : repoID ,
623+ RunID : runID ,
624+ ArtifactName : artifactName ,
625+ FinalizedArtifactsV4 : true ,
628626 ListOptions : utils .GetListOptions (ctx ),
629627 })
630628 if err != nil {
@@ -680,10 +678,9 @@ func GetArtifacts(ctx *context.APIContext) {
680678 artifactName := ctx .PathParam ("artifact_name" )
681679
682680 artifacts , total , err := db .FindAndCount [actions_model.ActionArtifact ](ctx , actions_model.FindArtifactsOptions {
683- RepoID : repoID ,
684- ArtifactName : artifactName ,
685- // Status: int(actions_model.ArtifactStatusUploadConfirmed),
686- FinalizedArtifactsV2 : true ,
681+ RepoID : repoID ,
682+ ArtifactName : artifactName ,
683+ FinalizedArtifactsV4 : true ,
687684 ListOptions : utils .GetListOptions (ctx ),
688685 })
689686 if err != nil {
@@ -745,9 +742,7 @@ func GetArtifact(ctx *context.APIContext) {
745742 return
746743 }
747744
748- // Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
749- // The v4 backend enshures ContentEncoding is set to "application/zip", which is not the case for the old backend
750- if art .ArtifactName + ".zip" == art .ArtifactPath && art .ContentEncoding == "application/zip" {
745+ if actions .IsArtifactV4 (art ) {
751746 repoName := ctx .Repo .Repository .FullName ()
752747 convertedArtifact , err := convert .ToActionArtifact (ctx , repoName , art )
753748 if err != nil {
@@ -804,18 +799,15 @@ func DownloadArtifact(ctx *context.APIContext) {
804799 }
805800 ctx .Resp .Header ().Set ("Content-Disposition" , fmt .Sprintf ("attachment; filename=%s.zip; filename*=UTF-8''%s.zip" , url .PathEscape (art .ArtifactName ), art .ArtifactName ))
806801
807- // Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
808- // The v4 backend enshures ContentEncoding is set to "application/zip", which is not the case for the old backend
809- if art .ArtifactName + ".zip" == art .ArtifactPath && art .ContentEncoding == "application/zip" {
810- art := art
811- if setting .Actions .ArtifactStorage .ServeDirect () {
812- u , err := storage .ActionsArtifacts .URL (art .StoragePath , art .ArtifactPath , nil )
813- if u != nil && err == nil {
814- ctx .Redirect (u .String (), http .StatusFound )
815- return
816- }
802+ if actions .IsArtifactV4 (art ) {
803+ ok , err := actions .DownloadArtifactV4ServeDirectOnly (ctx .Base , art )
804+ if ok {
805+ return
806+ }
807+ if err != nil {
808+ ctx .Error (http .StatusInternalServerError , err .Error (), err )
809+ return
817810 }
818- // ##[error]Unable to download artifact(s): Unable to download artifact. Unexpected status: 200
819811 repoName := ctx .Repo .Repository .FullName ()
820812 url := strings .TrimSuffix (setting .AppURL , "/" ) + "/api/v1/repos/" + repoName + "/actions/artifacts/" + fmt .Sprintf ("%d" , art .ID ) + "/zip/raw"
821813 ctx .Redirect (url , http .StatusFound )
@@ -868,24 +860,12 @@ func DownloadArtifactRaw(ctx *context.APIContext) {
868860 }
869861 ctx .Resp .Header ().Set ("Content-Disposition" , fmt .Sprintf ("attachment; filename=%s.zip; filename*=UTF-8''%s.zip" , url .PathEscape (art .ArtifactName ), art .ArtifactName ))
870862
871- // Artifacts using the v4 backend are stored as a single combined zip file per artifact on the backend
872- // The v4 backend enshures ContentEncoding is set to "application/zip", which is not the case for the old backend
873- if art .ArtifactName + ".zip" == art .ArtifactPath && art .ContentEncoding == "application/zip" {
874- art := art
875- if setting .Actions .ArtifactStorage .ServeDirect () {
876- u , err := storage .ActionsArtifacts .URL (art .StoragePath , art .ArtifactPath , nil )
877- if u != nil && err == nil {
878- ctx .Redirect (u .String ())
879- return
880- }
881- }
882- f , err := storage .ActionsArtifacts .Open (art .StoragePath )
863+ if actions .IsArtifactV4 (art ) {
864+ err := actions .DownloadArtifactV4 (ctx .Base , art )
883865 if err != nil {
884- ctx .Error (http .StatusInternalServerError , err . Error (), err )
866+ ctx .Error (http .StatusInternalServerError , "artifact has expired" , fmt . Errorf ( "artifact has expired" ) )
885867 return
886868 }
887- _ , _ = io .Copy (ctx .Resp , f )
888- return
889869 }
890870 // v3 not supported due to not having one unique id
891871 ctx .Error (http .StatusNotFound , "artifact not found" , fmt .Errorf ("artifact not found" ))
0 commit comments