private IDictionary<string, string> GetPackageMetadata(PackageVersion version)
        {
            var path = _nupkgDownloader.GetPackagePath(version);

            using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var errors = new List<string>();
                var metadata = NupkgPackageMetadataExtraction.MakePackageMetadata(fileStream, errors);

                if (errors.Any())
                {
                    throw new InvalidOperationException(
                        $"NuGet package for '{version.Id}' (version '{version.Version}') could not be read for metadata. " +
                        $"Errors: {string.Join(", ", errors)}");
                }

                metadata["listed"] = version.Listed.ToString().ToLowerInvariant();

                return metadata;
            }
        }
        private async Task DownloadPackageAsync(PackageVersion version)
        {
            var path = GetPackagePath(version);
            if (File.Exists(path))
            {
                return;
            }

            // download the package
            if (_packageBaseAddress == null)
            {
                _packageBaseAddress = await GetPackageBaseAddressAsync();
            }

            string relativeUri = $"{version.Id}/{version.Version}/{version.Id}.{version.Version}.nupkg".ToLower();
            var requestUri = new Uri(new Uri(_packageBaseAddress, UriKind.Absolute), relativeUri);
            var response = await _client.GetAsync(requestUri);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new InvalidOperationException($"HTTP {(int)response.StatusCode} {response.ReasonPhrase} was encountered when fetching package '{version.Id}' (version '{version.Version}').");
            }

            var packageStream = await response.Content.ReadAsStreamAsync();

            // get or initialize the package lock
            SemaphoreSlim packageLock;
            lock (Lock)
            {
                if (!PackageLocks.TryGetValue(path, out packageLock))
                {
                    packageLock = new SemaphoreSlim(1);
                    PackageLocks[path] = packageLock;
                }
            }

            // get a lock and write the package to disk
            var acquired = await packageLock.WaitAsync(TimeSpan.FromSeconds(30));
            if (!acquired)
            {
                throw new InvalidOperationException($"Could not get a lock to write the package '{version.Id}' (version '{version.Version}') to '{path}'.");
            }

            try
            {
                if (!File.Exists(path))
                {
                    using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        await packageStream.CopyToAsync(fileStream);
                    }
                }
            }
            catch (IOException)
            {
                // an IOException is thrown when another thread already downloaded this package
            }
            finally
            {
                packageLock.Release();
            }
        }
 public string GetPackagePath(PackageVersion version)
 {
     return Path.Combine(_settings.PackageDirectory, $"{version.Id}.{version.Version}.nupkg".ToLower());
 }
Example #4
0
        private async Task DownloadPackageAsync(PackageVersion version)
        {
            var path = GetPackagePath(version);

            if (File.Exists(path))
            {
                return;
            }

            // download the package
            if (_packageBaseAddress == null)
            {
                _packageBaseAddress = await GetPackageBaseAddressAsync();
            }

            string relativeUri = $"{version.Id}/{version.Version}/{version.Id}.{version.Version}.nupkg".ToLower();
            var    requestUri  = new Uri(new Uri(_packageBaseAddress, UriKind.Absolute), relativeUri);
            var    response    = await _client.GetAsync(requestUri);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new InvalidOperationException($"HTTP {(int)response.StatusCode} {response.ReasonPhrase} was encountered when fetching package '{version.Id}' (version '{version.Version}').");
            }

            var packageStream = await response.Content.ReadAsStreamAsync();

            // get or initialize the package lock
            SemaphoreSlim packageLock;

            lock (Lock)
            {
                if (!PackageLocks.TryGetValue(path, out packageLock))
                {
                    packageLock        = new SemaphoreSlim(1);
                    PackageLocks[path] = packageLock;
                }
            }

            // get a lock and write the package to disk
            var acquired = await packageLock.WaitAsync(TimeSpan.FromSeconds(30));

            if (!acquired)
            {
                throw new InvalidOperationException($"Could not get a lock to write the package '{version.Id}' (version '{version.Version}') to '{path}'.");
            }

            try
            {
                if (!File.Exists(path))
                {
                    using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        await packageStream.CopyToAsync(fileStream);
                    }
                }
            }
            catch (IOException)
            {
                // an IOException is thrown when another thread already downloaded this package
            }
            finally
            {
                packageLock.Release();
            }
        }
Example #5
0
 public string GetPackagePath(PackageVersion version)
 {
     return(Path.Combine(_settings.PackageDirectory, $"{version.Id}.{version.Version}.nupkg".ToLower()));
 }