public void ValidOriginalSize(int width, int height, int minWidth, int minHeight, int maxWidth, int maxHeight)
        {
            using (var image = TestHelpers.CreateImage(width, height, new PngEncoder()))
            {
                const FileFormat PngFormat = FileFormat.Png;
                var constraints            = CreateConstraints(minWidth, minHeight, maxWidth, maxHeight, PngFormat);

                BitmapImageValidator.ValidateSizeRangedBitmapImageHeader(1, constraints, PngFormat, image);
            }
        }
        public void InvalidOriginalSize(int width, int height, int minWidth, int minHeight, int maxWidth, int maxHeight)
        {
            using (var image = TestHelpers.CreateImage(width, height, new GifEncoder()))
            {
                const FileFormat GifFormat = FileFormat.Gif;
                var constraints            = CreateConstraints(minWidth, minHeight, maxWidth, maxHeight, GifFormat);

                var ex = Assert.Throws <InvalidBinaryException>(() => BitmapImageValidator.ValidateSizeRangedBitmapImageHeader(1, constraints, GifFormat, image));
                Assert.IsType <ImageSizeOutOfRangeError>(ex.Error);
            }
        }
        public void ValidOriginalFormat(FileFormat format)
        {
            var encoder = TestHelpers.GetImageEncoder(format);

            using (var image = TestHelpers.CreateImage(5, 5, encoder))
            {
                var constraints = CreateConstraints(1, 1, 10, 10, format);

                BitmapImageValidator.ValidateSizeRangedBitmapImageHeader(1, constraints, format, image);
            }
        }
        public void InvalidOriginalFormat(FileFormat expectedFormat, FileFormat actualFormat)
        {
            var encoder = TestHelpers.GetImageEncoder(actualFormat);

            using (var image = TestHelpers.CreateImage(5, 5, encoder))
            {
                var constraints = CreateConstraints(1, 1, 10, 10, expectedFormat);

                var ex = Assert.Throws <InvalidBinaryException>(() => BitmapImageValidator.ValidateSizeRangedBitmapImageHeader(1, constraints, actualFormat, image));
                Assert.IsType <BinaryInvalidFormatError>(ex.Error);
            }
        }
示例#5
0
        public void ValidImage(FileFormat format)
        {
            var width  = 5;
            var height = 5;

            var encoder = TestHelpers.GetImageEncoder(format);

            using (var image = TestHelpers.CreateImage(width, height, encoder))
            {
                var constraints = CreateConstraints(1, 1, 10, 10, format);
                BitmapImageValidator.ValidateSizeRangedBitmapImageHeader(1, constraints, format, image);
            }
        }
        public 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.ScalableBitmapImage:
                BitmapImageValidator.ValidateSizeRangedBitmapImageHeader(
                    templateCode,
                    (ScalableBitmapImageElementConstraints)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.ValidateSizeRangedBitmapImageHeader(
                        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($"Specified element descriptor type '{elementDescriptorType}' is non-binary");

            default:
                throw new ArgumentOutOfRangeException(nameof(elementDescriptorType), elementDescriptorType, "Unsupported element descriptor type");
            }
        }