private static void EnsureFileHeaderIsValid( int templateCode, ElementDescriptorType elementDescriptorType, IElementConstraints elementConstraints, Stream inputStream, IUploadedFileMetadata uploadedFileMetadata) { var fileFormat = DetectFileFormat(uploadedFileMetadata.FileName); switch (elementDescriptorType) { case ElementDescriptorType.BitmapImage: BitmapImageValidator.ValidateBitmapImageHeader(templateCode, (BitmapImageElementConstraints)elementConstraints, fileFormat, inputStream); break; case ElementDescriptorType.CompositeBitmapImage: if (uploadedFileMetadata.FileType == FileType.SizeSpecificBitmapImage) { var imageMetadata = (UploadedImageMetadata)uploadedFileMetadata; BitmapImageValidator.ValidateSizeSpecificBitmapImageHeader( templateCode, (CompositeBitmapImageElementConstraints)elementConstraints, fileFormat, inputStream, imageMetadata.Size); } else { BitmapImageValidator.ValidateCompositeBitmapImageOriginalHeader( templateCode, (CompositeBitmapImageElementConstraints)elementConstraints, fileFormat, inputStream); } break; case ElementDescriptorType.VectorImage: VectorImageValidator.ValidateVectorImageHeader(templateCode, fileFormat, inputStream); break; case ElementDescriptorType.Article: break; case ElementDescriptorType.PlainText: case ElementDescriptorType.FormattedText: case ElementDescriptorType.FasComment: case ElementDescriptorType.Link: case ElementDescriptorType.Phone: case ElementDescriptorType.VideoLink: case ElementDescriptorType.Color: throw new NotSupportedException($"Not binary element descriptor type {elementDescriptorType}"); default: throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type"); } }
public void ValidSizeSpecificImage() { var width = 5; var height = 5; var image = CreateImage(width, height, new GifEncoder()); const FileFormat GifFormat = FileFormat.Gif; var constraints = CreateConstraints(1, 1, 10, 10, GifFormat); BitmapImageValidator.ValidateSizeSpecificBitmapImageHeader(1, constraints, GifFormat, image, new ImageSize { Width = width, Height = height }); }
public void SizeSpecificImageSizeNotEqualToTargetSize() { using (var image = TestHelpers.CreateImage(5, 5, new PngEncoder())) { const FileFormat PngFormat = FileFormat.Png; var constraints = CreateConstraints(1, 1, 10, 10, PngFormat); var ex = Assert.Throws <InvalidBinaryException>( () => BitmapImageValidator.ValidateSizeSpecificBitmapImageHeader(1, constraints, PngFormat, image, new ImageSize { Width = 4, Height = 4 })); Assert.IsType <SizeSpecificImageTargetSizeNotEqualToActualSizeError>(ex.Error); } }
public void NotSquaredSizeSpecificImage() { var width = 5; var height = 4; using (var image = TestHelpers.CreateImage(width, height, new JpegEncoder())) { const FileFormat JpegFormat = FileFormat.Jpeg; var constraints = CreateConstraints(1, 1, 10, 10, JpegFormat); var ex = Assert.Throws <InvalidBinaryException>( () => BitmapImageValidator.ValidateSizeSpecificBitmapImageHeader(1, constraints, JpegFormat, image, new ImageSize { Width = width, Height = height })); Assert.IsType <SizeSpecificImageIsNotSquareError>(ex.Error); } }