Exemple #1
0
        public static async Task <DownloadResourceResult> GetDownloadResultAsync(
            HttpSource client,
            PackageIdentity identity,
            Uri uri,
            PackageDownloadContext downloadContext,
            string globalPackagesFolder,
            ILogger logger,
            CancellationToken token)
        {
            // Observe the NoCache argument.
            var directDownload = downloadContext.DirectDownload;
            DownloadResourceResult packageFromGlobalPackages = null;

            try
            {
                packageFromGlobalPackages = GlobalPackagesFolderUtility.GetPackage(
                    identity,
                    globalPackagesFolder);

                if (packageFromGlobalPackages != null)
                {
                    if (!downloadContext.SourceCacheContext.NoCache)
                    {
                        return(packageFromGlobalPackages);
                    }
                    else
                    {
                        // The package already exists in the global packages folder but the caller has requested NoCache,
                        // which means the package in the global packages folder should not be used. In this particular
                        // case, NoCache needs to imply DirectDownload.
                        directDownload = true;
                        packageFromGlobalPackages.Dispose();
                    }
                }
            }
            catch
            {
                packageFromGlobalPackages?.Dispose();
            }

            // Get the package from the source.
            for (var retry = 0; retry < 3; retry++)
            {
                try
                {
                    return(await client.ProcessStreamAsync(
                               new HttpSourceRequest(uri, logger)
                    {
                        IgnoreNotFounds = true,
                        MaxTries = 1
                    },
                               async packageStream =>
                    {
                        if (packageStream == null)
                        {
                            return new DownloadResourceResult(DownloadResourceResultStatus.NotFound);
                        }

                        if (directDownload)
                        {
                            return await DirectDownloadAsync(
                                client.PackageSource,
                                identity,
                                packageStream,
                                downloadContext,
                                token);
                        }
                        else
                        {
                            return await GlobalPackagesFolderUtility.AddPackageAsync(
                                client.PackageSource,
                                identity,
                                packageStream,
                                globalPackagesFolder,
                                downloadContext.ParentId,
                                downloadContext.ClientPolicyContext,
                                logger,
                                token);
                        }
                    },
                               downloadContext.SourceCacheContext,
                               logger,
                               token));
                }
                catch (OperationCanceledException)
                {
                    return(new DownloadResourceResult(DownloadResourceResultStatus.Cancelled));
                }
                catch (SignatureException)
                {
                    throw;
                }
                catch (Exception ex) when(retry < 2)
                {
                    var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_ErrorDownloading, identity, uri)
                                  + Environment.NewLine
                                  + ExceptionUtilities.DisplayMessage(ex);

                    logger.LogWarning(message);
                }
                catch (Exception ex)
                {
                    var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_ErrorDownloading, identity, uri);

                    throw new FatalProtocolException(message, ex);
                }
            }

            throw new InvalidOperationException("Reached an unexpected point in the code");
        }
        public static async Task <DownloadResourceResult> GetDownloadResultAsync(
            HttpSource client,
            PackageIdentity identity,
            Uri uri,
            ISettings settings,
            ILogger logger,
            CancellationToken token)
        {
            // Uri is not null, so the package exists in the source
            // Now, check if it is in the global packages folder, before, getting the package stream

            // TODO: This code should respect no_cache settings and not write or read packages from the global packages folder
            var packageFromGlobalPackages = GlobalPackagesFolderUtility.GetPackage(identity, settings);

            if (packageFromGlobalPackages != null)
            {
                return(packageFromGlobalPackages);
            }

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    return(await client.ProcessStreamAsync(
                               new HttpSourceRequest(uri, logger)
                    {
                        IgnoreNotFounds = true
                    },
                               async packageStream =>
                    {
                        if (packageStream == null)
                        {
                            return new DownloadResourceResult(DownloadResourceResultStatus.NotFound);
                        }

                        return await GlobalPackagesFolderUtility.AddPackageAsync(
                            identity,
                            packageStream,
                            settings,
                            logger,
                            token);
                    },
                               logger,
                               token));
                }
                catch (OperationCanceledException)
                {
                    return(new DownloadResourceResult(DownloadResourceResultStatus.Cancelled));
                }
                catch (Exception ex) when((
                                              (ex is IOException && ex.InnerException is SocketException) ||
                                              ex is TimeoutException) &&
                                          i < 2)
                {
                    string message = string.Format(CultureInfo.CurrentCulture, Strings.Log_ErrorDownloading, identity, uri)
                                     + Environment.NewLine
                                     + ExceptionUtilities.DisplayMessage(ex);

                    logger.LogWarning(message);
                }
                catch (Exception ex)
                {
                    string message = string.Format(CultureInfo.CurrentCulture, Strings.Log_ErrorDownloading, identity, uri);
                    logger.LogError(message + Environment.NewLine + ExceptionUtilities.DisplayMessage(ex));

                    throw new FatalProtocolException(message, ex);
                }
            }

            throw new InvalidOperationException("Reached an unexpected point in the code");
        }