Ejemplo n.º 1
0
        private async Task OrientAsync(HttpContext context)
        {
            await using var tempStream = GetTempStream();

            await context.Request.Body.CopyToAsync(tempStream, context.RequestAborted);

            tempStream.Position = 0;

            try
            {
                await assetThumbnailGenerator.FixOrientationAsync(
                    tempStream,
                    context.Request.ContentType ?? "image/png",
                    context.Response.Body,
                    context.RequestAborted);
            }
            catch (Exception ex)
            {
                var log = context.RequestServices.GetRequiredService <ISemanticLog>();

                log.LogError(ex, w => w
                             .WriteProperty("action", "Resize")
                             .WriteProperty("status", "Failed"));

                context.Response.StatusCode = 400;
            }
        }
Ejemplo n.º 2
0
        public async Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags)
        {
            if (command.Type == AssetType.Unknown || command.Type == AssetType.Image)
            {
                ImageInfo?imageInfo = null;

                using (var uploadStream = command.File.OpenRead())
                {
                    imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream);
                }

                if (imageInfo != null)
                {
                    var isSwapped = imageInfo.IsRotatedOrSwapped;

                    if (isSwapped)
                    {
                        var tempFile = new TempAssetFile(command.File);

                        using (var uploadStream = command.File.OpenRead())
                        {
                            imageInfo = await assetThumbnailGenerator.FixOrientationAsync(uploadStream, tempFile.Stream);
                        }

                        command.File.Dispose();
                        command.File = tempFile;
                    }

                    if (command.Type == AssetType.Unknown || isSwapped)
                    {
                        command.Type = AssetType.Image;

                        command.Metadata.SetPixelWidth(imageInfo.PixelWidth);
                        command.Metadata.SetPixelHeight(imageInfo.PixelHeight);
                    }
                }
            }

            if (command.Type == AssetType.Image && tags != null)
            {
                tags.Add("image");

                var wh = command.Metadata.GetPixelWidth() + command.Metadata.GetPixelWidth();

                if (wh > 2000)
                {
                    tags.Add("image/large");
                }
                else if (wh > 1000)
                {
                    tags.Add("image/medium");
                }
                else
                {
                    tags.Add("image/small");
                }
            }
        }
        public async Task Should_get_dimensions_from_image_library()
        {
            var command = new CreateAsset {
                File = file
            };

            A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream))
            .Returns(new ImageInfo(800, 600, false));

            await sut.EnhanceAsync(command);

            Assert.Equal(800, command.Metadata.GetPixelWidth());
            Assert.Equal(600, command.Metadata.GetPixelHeight());
            Assert.Equal(AssetType.Image, command.Type);

            A.CallTo(() => assetThumbnailGenerator.FixOrientationAsync(stream, A <Stream> ._))
            .MustNotHaveHappened();
        }
Ejemplo n.º 4
0
        public async Task EnhanceAsync(MetadataRequest request)
        {
            var file = request.File;

            if (request.Type != MediaType.Unknown)
            {
                return;
            }

            var mimeType = file.MimeType;

            ImageInfo?imageInfo = null;

            await using (var uploadStream = file.OpenRead())
            {
                imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream, mimeType);
            }

            if (imageInfo != null)
            {
                var isSwapped = imageInfo.Orientation > ImageOrientation.TopLeft;

                if (isSwapped)
                {
                    var tempFile = TempAssetFile.Create(file);

                    await using (var uploadStream = file.OpenRead())
                    {
                        await using (var tempStream = tempFile.OpenWrite())
                        {
                            await assetThumbnailGenerator.FixOrientationAsync(uploadStream, mimeType, tempStream);
                        }
                    }

                    await using (var tempStream = tempFile.OpenRead())
                    {
                        imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(tempStream, mimeType) ?? imageInfo;
                    }

                    await file.DisposeAsync();

                    request.File = tempFile;
                }
            }

            if (imageInfo != null)
            {
                request.Type = MediaType.Image;

                request.Metadata.SetPixelWidth(imageInfo.PixelWidth);
                request.Metadata.SetPixelHeight(imageInfo.PixelHeight);
            }
        }
Ejemplo n.º 5
0
        public async Task Should_auto_orient_image()
        {
            var(mimeType, source) = GetRotatedJpeg();

            await using (var target = GetStream("oriented", "jpeg"))
            {
                await sut.FixOrientationAsync(source, mimeType, target);

                target.Position = 0;

                var imageInfo = await sut.GetImageInfoAsync(target, mimeType);

                Assert.Equal(135, imageInfo.PixelHeight);
                Assert.Equal(600, imageInfo.PixelWidth);

                Assert.Equal(ImageOrientation.TopLeft, imageInfo.Orientation);
            }
        }
Ejemplo n.º 6
0
        public async Task EnhanceAsync(UploadAssetCommand command)
        {
            if (command.Type == AssetType.Unknown || command.Type == AssetType.Image)
            {
                var mimeType = command.File.MimeType;

                ImageInfo?imageInfo = null;

                await using (var uploadStream = command.File.OpenRead())
                {
                    imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream, mimeType);
                }

                if (imageInfo != null)
                {
                    var isSwapped = imageInfo.Orientation > ImageOrientation.TopLeft;

                    if (command.File != null && isSwapped)
                    {
                        var tempFile = TempAssetFile.Create(command.File);

                        await using (var uploadStream = command.File.OpenRead())
                        {
                            await using (var tempStream = tempFile.OpenWrite())
                            {
                                await assetThumbnailGenerator.FixOrientationAsync(uploadStream, mimeType, tempStream);
                            }
                        }

                        await using (var tempStream = tempFile.OpenRead())
                        {
                            imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(tempStream, mimeType) ?? imageInfo;
                        }

                        await command.File.DisposeAsync();

                        command.File = tempFile;
                    }

                    if (command.Type == AssetType.Unknown || isSwapped)
                    {
                        command.Type = AssetType.Image;

                        command.Metadata.SetPixelWidth(imageInfo.PixelWidth);
                        command.Metadata.SetPixelHeight(imageInfo.PixelHeight);
                    }
                }
            }

            if (command.Tags == null)
            {
                return;
            }

            if (command.Type == AssetType.Image)
            {
                command.Tags.Add("image");

                var wh = command.Metadata.GetPixelWidth() + command.Metadata.GetPixelWidth();

                if (wh > 2000)
                {
                    command.Tags.Add("image/large");
                }
                else if (wh > 1000)
                {
                    command.Tags.Add("image/medium");
                }
                else
                {
                    command.Tags.Add("image/small");
                }
            }
        }