private async Task <FileStreamCreator> GetStreamInternalAsync( string pathInPackage, CancellationToken cancellationToken) { var packageId = _packageIdentity.Id; var packageVersion = _packageIdentity.Version.ToNormalizedString(); var payload = new CopyFilesInPackageRequest( _packageSourceRepository, packageId, packageVersion, new[] { pathInPackage }, _tempDirectoryPath.Value); var response = await _plugin.Connection.SendRequestAndReceiveResponseAsync <CopyFilesInPackageRequest, CopyFilesInPackageResponse>( MessageMethod.CopyFilesInPackage, payload, CancellationToken.None); if (response != null) { switch (response.ResponseCode) { case MessageResponseCode.Success: return(new FileStreamCreator(response.CopiedFiles.Single())); case MessageResponseCode.Error: throw new PluginException( string.Format(CultureInfo.CurrentCulture, Strings.Plugin_FailedOperationForPackage, _plugin.Name, MessageMethod.CopyFilesInPackage, packageId, packageVersion)); case MessageResponseCode.NotFound: // This class is only created if a success response code is received for a // PrefetchPackageRequest, meaning that the plugin has already confirmed // that the package exists. default: break; } } return(null); }
/// <summary> /// Asynchronously copies specified files in the package to the destination location. /// </summary> /// <param name="destination">A directory path to copy files to.</param> /// <param name="packageFiles">An enumerable of files in the package to copy.</param> /// <param name="extractFile">A package file extraction delegate.</param> /// <param name="logger">A logger.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns>A task that represents the asynchronous operation. /// The task result (<see cref="Task{TResult}.Result" />) returns an /// <see cref="IEnumerable{String}" />.</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="destination" /> /// is either <c>null</c> or an empty string.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="packageFiles" /> /// is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="logger" /> is <c>null</c>.</exception> /// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" /> /// is cancelled.</exception> public override async Task <IEnumerable <string> > CopyFilesAsync( string destination, IEnumerable <string> packageFiles, ExtractPackageFileDelegate extractFile, ILogger logger, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(destination)) { throw new ArgumentException(Strings.ArgumentCannotBeNullOrEmpty, nameof(destination)); } if (packageFiles == null) { throw new ArgumentNullException(nameof(packageFiles)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } cancellationToken.ThrowIfCancellationRequested(); if (!packageFiles.Any()) { return(Enumerable.Empty <string>()); } var packageId = _packageIdentity.Id; var packageVersion = _packageIdentity.Version.ToNormalizedString(); var request = new CopyFilesInPackageRequest( _packageSourceRepository, packageId, packageVersion, packageFiles, destination); var response = await _plugin.Connection.SendRequestAndReceiveResponseAsync <CopyFilesInPackageRequest, CopyFilesInPackageResponse>( MessageMethod.CopyFilesInPackage, request, cancellationToken); if (response != null) { switch (response.ResponseCode) { case MessageResponseCode.Success: return(response.CopiedFiles); case MessageResponseCode.Error: throw new PluginException( string.Format(CultureInfo.CurrentCulture, Strings.Plugin_FailedOperationForPackage, _plugin.Name, MessageMethod.CopyFilesInPackage, packageId, packageVersion)); case MessageResponseCode.NotFound: // This class is only created if a success response code is received for a // PrefetchPackageRequest, meaning that the plugin has already confirmed // that the package exists. default: break; } } return(Enumerable.Empty <string>()); }