/// <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)); } } }