private async Task DownloadArtifact( IExecutionContext executionContext, IHostContext hostContext, string localFolderPath, CustomArtifactDetails customArtifactDetails, CustomArtifactDownloadDetails artifact) { IDictionary <string, string> artifactTypeStreamMapping = customArtifactDetails.ArtifactTypeStreamMapping; string streamType = GetArtifactStreamType(artifact, artifactTypeStreamMapping); if (string.Equals(streamType, WellKnownStreamTypes.FileShare, StringComparison.OrdinalIgnoreCase)) { if (!PlatformUtil.RunningOnWindows) { throw new NotSupportedException(StringUtil.Loc("RMFileShareArtifactErrorOnNonWindowsAgent")); } var fileShareArtifact = new FileShareArtifact(); customArtifactDetails.RelativePath = artifact.RelativePath ?? string.Empty; var location = artifact.FileShareLocation ?? artifact.DownloadUrl; await fileShareArtifact.DownloadArtifactAsync(executionContext, hostContext, new ArtifactDefinition { Details = customArtifactDetails }, new Uri(location).LocalPath, localFolderPath); } else if (string.Equals(streamType, WellKnownStreamTypes.Zip, StringComparison.OrdinalIgnoreCase)) { try { IEndpointAuthorizer authorizer = SchemeBasedAuthorizerFactory.GetEndpointAuthorizer( ToServiceEndpoint(customArtifactDetails.Endpoint), customArtifactDetails.AuthorizationHeaders?.Select(header => ToAuthorizationHeader(header)).ToList()); using (HttpWebResponse webResponse = GetWebResponse(executionContext, artifact.DownloadUrl, authorizer)) { var zipStreamDownloader = HostContext.GetService <IZipStreamDownloader>(); await zipStreamDownloader.DownloadFromStream( executionContext, webResponse.GetResponseStream(), string.Empty, artifact.RelativePath ?? string.Empty, localFolderPath); } } catch (WebException) { executionContext.Output(StringUtil.Loc("ArtifactDownloadFailed", artifact.DownloadUrl)); throw; } } else { string resourceType = streamType; var warningMessage = StringUtil.Loc("RMStreamTypeNotSupported", resourceType); executionContext.Warning(warningMessage); } }
private static HttpWebResponse GetWebResponse(IExecutionContext executionContext, string url, IEndpointAuthorizer authorizer) { var request = WebRequest.Create(url) as HttpWebRequest; if (request == null) { string errorMessage = StringUtil.Loc("RMArtifactDownloadRequestCreationFailed", url); executionContext.Output(errorMessage); throw new InvalidOperationException(errorMessage); } authorizer.AuthorizeRequest(request); var webResponse = request.GetResponseAsync().Result as HttpWebResponse; return(webResponse); }