Exemplo n.º 1
0
        public async Task <GenericLicense> DownloadLicenseByCodeAsync(string licenseCode, CancellationToken token)
        {
            var result = new GenericLicense
            {
                Code     = LicenseCode,
                FullName = "Code Project Open License (CPOL) 1.02",
                FileHRef = "https://www.codeproject.com/info/cpol10.aspx"
            };

            using (var client = HttpClientFactory())
                using (var response = await client.GetAsync("https://www.codeproject.com/info/CPOL.zip", token))
                {
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(null);
                    }

                    await response.AssertStatusCodeOk();

                    using (var stream = await response.Content.ReadAsStreamAsync())
                        using (var zip = new ZipArchive(stream))
                        {
                            var entry = zip.Entries.Count == 1 ? zip.Entries[0] : zip.Entries.First(i => ".htm".EqualsIgnoreCase(Path.GetExtension(i.Name)));
                            using (var entryStream = entry.Open())
                            {
                                result.FileContent = await entryStream.ToArrayAsync(token);

                                result.FileName = entry.Name;
                            }
                        }
                }

            return(result);
        }
Exemplo n.º 2
0
        public async Task <GenericLicense> DownloadLicenseByCodeAsync(string licenseCode, CancellationToken token)
        {
            licenseCode.AssertNotNull(nameof(licenseCode));

            if (!_urlByCode.TryGetValue(licenseCode, out var configuration))
            {
                return(null);
            }

            var result = new GenericLicense
            {
                Code     = configuration.Code,
                FullName = configuration.FullName.IsNullOrEmpty() ? configuration.Code : configuration.FullName,
                FileHRef = configuration.DownloadUrl
            };

            if (configuration.DownloadUrl.IsNullOrEmpty())
            {
                return(result);
            }

            string mediaType;

            using (var client = HttpClientFactory())
                using (var response = await client.GetAsync(configuration.DownloadUrl, token))
                {
                    await response.AssertStatusCodeOk();

                    mediaType = response.Content.Headers.ContentType.MediaType;

                    using (var stream = await response.Content.ReadAsStreamAsync())
                        using (var content = new MemoryStream())
                        {
                            await stream.CopyToAsync(content, token);

                            result.FileContent = content.ToArray();
                        }
                }

            if (MediaTypeNames.Text.Html.EqualsIgnoreCase(mediaType))
            {
                result.FileName = "license.html";
            }
            else
            {
                result.FileName = "license.txt";
            }

            return(result);
        }