Esempio n. 1
0
        /// <summary>
        /// Returns a stream containing the file at the specified path in the specified package if it is available; otherwise null.
        /// </summary>
        /// <param name="id">Full name of the package.</param>
        /// <param name="version">Version of the package. Specify null for the latest version.</param>
        /// <param name="cancellationToken">Cancellation token for asynchronous operations.</param>
        /// <param name="filePath">Path of the file inside the package.</param>
        /// <returns>Stream containing the specified package if it is available; otherwise null.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="id"/> is null.</exception>
        /// <remarks>
        /// The stream returned by this method is not buffered at all. If random access is required, the caller must
        /// first copy it to another stream.
        /// </remarks>
        public async Task <Stream> GetPackageFileStreamAsync(UniversalPackageId id, UniversalPackageVersion version, string filePath, CancellationToken cancellationToken)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (this.Endpoint.IsLocalDirectory)
            {
                return(this.localRepository.Value.GetPackageFileStream(id, version, filePath));
            }

            var url = "download-file/" + Uri.EscapeUriString(id.ToString());

            if (version != null)
            {
                url += "/" + Uri.EscapeUriString(version.ToString()) + "?path=" + Uri.EscapeDataString(filePath);
            }
            else
            {
                url += "?latest&path=" + Uri.EscapeDataString(filePath);
            }

            var request  = new ApiRequest(this.Endpoint, url);
            var response = await this.transport.GetResponseAsync(request, cancellationToken).ConfigureAwait(false);

            try
            {
                return(response.GetResponseStream());
            }
            catch
            {
                response?.Dispose();
                throw;
            }
        }
        public Stream GetPackageStream(UniversalPackageId id, UniversalPackageVersion version)
        {
            var packageVersions = this.allPackages.Value[new PackageKey(id.Group, id.Name)];
            var match           = default(PackageFile);

            if (version == null)
            {
                match = packageVersions.OrderByDescending(p => UniversalPackageVersion.Parse((string)p.JObject["version"])).FirstOrDefault();
            }
            else
            {
                var s = version.ToString();
                match = packageVersions.FirstOrDefault(p => string.Equals((string)p.JObject["version"], s, StringComparison.OrdinalIgnoreCase));
            }

            if (match.IsNull)
            {
                return(null);
            }

            return(new FileStream(match.FileName, FileMode.Open, FileAccess.Read, FileShare.Read));
        }
Esempio n. 3
0
        private async Task <IReadOnlyList <RemoteUniversalPackageVersion> > ListVersionsInternalAsync(UniversalPackageId id, UniversalPackageVersion version, bool includeFileList, int?maxCount, CancellationToken cancellationToken)
        {
            if (this.Endpoint.IsLocalDirectory)
            {
                if (version == null)
                {
                    return(this.localRepository.Value.ListPackageVersions(id).ToList());
                }
                else
                {
                    var v = this.localRepository.Value.GetPackageVersion(id, version);
                    return(v != null ? new[] { v } : new RemoteUniversalPackageVersion[0]);
                }
            }

            var url     = FormatUrl("versions", ("group", id?.Group), ("name", id?.Name), ("version", version?.ToString()), ("includeFileList", includeFileList), ("count", maxCount));
            var request = new ApiRequest(this.Endpoint, url);

            using (var response = await this.transport.GetResponseAsync(request, cancellationToken).ConfigureAwait(false))
            {
                if (response.ContentType?.StartsWith("application/json", StringComparison.OrdinalIgnoreCase) != true)
                {
                    throw new InvalidDataException($"Server returned {response.ContentType} content type; expected application/json.");
                }

                using (var responseStream = response.GetResponseStream())
                    using (var reader = new StreamReader(responseStream, AH.UTF8))
                        using (var jsonReader = new JsonTextReader(reader))
                        {
                            if (version == null)
                            {
                                var arr = await JArray.LoadAsync(jsonReader, cancellationToken).ConfigureAwait(false);

                                var results = new List <RemoteUniversalPackageVersion>(arr.Count);

                                foreach (var token in arr)
                                {
                                    if (!(token is JObject obj))
                                    {
                                        throw new InvalidDataException("Unexpected token in JSON array.");
                                    }

                                    results.Add(new RemoteUniversalPackageVersion(obj));
                                }

                                return(results.AsReadOnly());
                            }
                            else
                            {
                                var obj = await JObject.LoadAsync(jsonReader, cancellationToken).ConfigureAwait(false);

                                return(new[] { new RemoteUniversalPackageVersion(obj) });
                            }
                        }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Deletes the specified package from the remote feed.
        /// </summary>
        /// <param name="id">Full name of the package.</param>
        /// <param name="version">Version of the package.</param>
        /// <param name="cancellationToken">Cancellation token for asynchronous operations.</param>
        /// <exception cref="ArgumentNullException"><paramref name="id"/> is null or <paramref name="version"/> is null.</exception>
        public async Task DeletePackageAsync(UniversalPackageId id, UniversalPackageVersion version, CancellationToken cancellationToken)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (version == null)
            {
                throw new ArgumentNullException(nameof(version));
            }

            if (this.Endpoint.IsLocalDirectory)
            {
                throw new NotSupportedException();
            }

            var url     = "delete/" + Uri.EscapeUriString(id.ToString()) + "/" + Uri.EscapeUriString(version.ToString());
            var request = new ApiRequest(this.Endpoint, url, method: "DELETE");

            using (var response = await this.transport.GetResponseAsync(request, cancellationToken).ConfigureAwait(false))
            {
            }
        }