Пример #1
0
        /// <summary>
        /// Tries to find the license id from the project itself.
        /// </summary>
        private async Task <(License?, string)> TryGetLicenseFromProject(Uri?projectUrl)
        {
            // A random sample of packages shows that many are missing a license
            // URL, but having a github project URL shows that around 20% will
            // have a LICENSE file.
            if (projectUrl == null)
            {
                return(null, string.Empty);
            }

            var licenseUrls = GetPotentialLicenseUrls(projectUrl);

            if (!licenseUrls.Any())
            {
                return(null, string.Empty);
            }

            Uri?validUri = null;

            foreach (var licenseUrl in licenseUrls)
            {
                try
                {
                    var _ = await SharedHttpClient.GetStringAsync(licenseUrl);

                    validUri = licenseUrl;
                }
                catch
                {
                    // Expected. Only one in five will statistically have a license
                    // at the guessed URL.
                }
            }

            if (validUri == null)
            {
                return(null, string.Empty);
            }

            return(await this.licenseParsing.TryGetLicenseFromLicenseFile(validUri));
        }
Пример #2
0
        /// <summary>
        /// Tries to find the license identifier based on the text in the
        /// license file.
        /// </summary>
        public async Task <(License?, string)> TryGetLicenseFromLicenseFile(Uri licenseUrl)
        {
            string path = Path.Combine(licenceFileDirectory, Normalize(licenseUrl), "LICENSE");

            if (!DiskCache.TryGetValue(path, this.config.DiskCache.LicenseFiles, out string?licenseText))
            {
                try
                {
                    licenseText = await SharedHttpClient.GetStringAsync(licenseUrl).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    string message = $"Error getting license file from {licenseUrl}: {e.Message}";
                    Log.Warning(message);
                    return(null, message);
                }

                DiskCache.CacheData(path, licenseText);
            }

            return(await this.licenseDetector.Detect(Path.GetDirectoryName(path)).ConfigureAwait(false));
        }