/// <summary> /// Given a list of resolved files, /// determine which file will be used as the icon file and validate its size. /// </summary> /// <param name="files">Files resolved from the file entries in the nuspec</param> /// <param name="iconPath">iconpath found in the .nuspec</param> /// <exception cref="PackagingException">When a validation rule is not met</exception> private void ValidateIconFile(IEnumerable <IPackageFile> files, string iconPath) { if (!PackageTypes.Contains(PackageType.SymbolsPackage) && !string.IsNullOrEmpty(iconPath)) { // Validate entry var iconPathStripped = PathUtility.StripLeadingDirectorySeparators(iconPath); var iconFileList = files.Where(f => iconPathStripped.Equals( PathUtility.StripLeadingDirectorySeparators(f.Path), PathUtility.GetStringComparisonBasedOnOS())); if (iconFileList.Count() == 0) { throw new PackagingException( NuGetLogCode.NU5046, string.Format(CultureInfo.CurrentCulture, NuGetResources.IconNoFileElement, iconPath)); } IPackageFile iconFile = iconFileList.First(); try { // Validate Icon open file using (var iconStream = iconFile.GetStream()) { // Validate file size long fileSize = iconStream.Length; if (fileSize > MaxIconFileSize) { throw new PackagingException(Common.NuGetLogCode.NU5047, NuGetResources.IconMaxFileSizeExceeded); } if (fileSize == 0) { throw new PackagingException(Common.NuGetLogCode.NU5047, NuGetResources.IconErrorEmpty); } } } catch (FileNotFoundException e) { throw new PackagingException( NuGetLogCode.NU5046, string.Format(CultureInfo.CurrentCulture, NuGetResources.IconCannotOpenFile, iconPath, e.Message)); } } }
private void ValidateLicenseFile(IEnumerable <IPackageFile> files, LicenseMetadata licenseMetadata) { if (!PackageTypes.Contains(PackageType.SymbolsPackage) && licenseMetadata?.Type == LicenseType.File) { var ext = Path.GetExtension(licenseMetadata.License); if (!string.IsNullOrEmpty(ext) && !ext.Equals(".txt", StringComparison.OrdinalIgnoreCase) && !ext.Equals(".md", StringComparison.OrdinalIgnoreCase)) { throw new PackagingException(NuGetLogCode.NU5031, string.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_LicenseFileExtensionIsInvalid, licenseMetadata.License)); } var strippedLicenseFileLocation = PathUtility.StripLeadingDirectorySeparators(licenseMetadata.License); var count = files.Where(e => PathUtility.StripLeadingDirectorySeparators(e.Path).Equals(strippedLicenseFileLocation, PathUtility.GetStringComparisonBasedOnOS())).Count(); if (count == 0) { throw new PackagingException(NuGetLogCode.NU5030, string.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_LicenseFileIsNotInNupkg, licenseMetadata.License)); } } }
public IEnumerable <string> Validate() { if (String.IsNullOrEmpty(Id)) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Id")); } else { if (Id.Length > PackageIdValidator.MaxPackageIdLength) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_IdMaxLengthExceeded)); } else if (!PackageIdValidator.IsValidPackageId(Id)) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.InvalidPackageId, Id)); } } if (Version == null) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Version")); } if ((Authors == null || !Authors.Any(author => !String.IsNullOrEmpty(author))) && !PackageTypes.Contains(PackageType.SymbolsPackage)) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Authors")); } if (String.IsNullOrEmpty(Description)) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_RequiredMetadataMissing, "Description")); } if (_licenseUrl == String.Empty) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "LicenseUrl")); } if (_iconUrl == String.Empty) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "IconUrl")); } if (_projectUrl == String.Empty) { yield return(String.Format(CultureInfo.CurrentCulture, NuGetResources.Manifest_UriCannotBeEmpty, "ProjectUrl")); } if (RequireLicenseAcceptance && (string.IsNullOrWhiteSpace(_licenseUrl) && LicenseMetadata == null)) { yield return(NuGetResources.Manifest_RequireLicenseAcceptanceRequiresLicenseUrl); } if (_licenseUrl != null && LicenseMetadata != null && (string.IsNullOrWhiteSpace(_licenseUrl) || !LicenseUrl.Equals(LicenseMetadata.LicenseUrl))) { yield return(NuGetResources.Manifest_LicenseUrlCannotBeUsedWithLicenseMetadata); } }