示例#1
0
        public AssetCommandMiddlewareTests()
        {
            file = new AssetFile("my-image.png", "image/png", 1024, () => stream);

            asset = new AssetGrain(Store, tagService, assetQuery, A.Fake <IActivationLimit>(), A.Dummy <ISemanticLog>());
            asset.ActivateAsync(Id).Wait();

            A.CallTo(() => contextProvider.Context)
            .Returns(requestContext);

            A.CallTo(() => assetEnricher.EnrichAsync(A <IAssetEntity> .Ignored, requestContext))
            .ReturnsLazily(() => SimpleMapper.Map(asset.Snapshot, new AssetEntity()));

            A.CallTo(() => assetQuery.QueryByHashAsync(A <Context> .That.Matches(x => x.IsNoAssetEnrichment()), AppId, A <string> .Ignored))
            .Returns(new List <IEnrichedAssetEntity>());

            A.CallTo(() => grainFactory.GetGrain <IAssetGrain>(Id, null))
            .Returns(asset);

            A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream))
            .Returns(image);

            sut = new AssetCommandMiddleware(grainFactory,
                                             assetEnricher,
                                             assetQuery,
                                             assetStore,
                                             assetThumbnailGenerator,
                                             contextProvider, new[] { tagGenerator });
        }
示例#2
0
        public async override Task HandleAsync(CommandContext context, Func <Task> next)
        {
            switch (context.Command)
            {
            case CreateAsset createAsset:
            {
                createAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(createAsset.File.OpenRead());

                await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), createAsset.File.OpenRead());

                try
                {
                    var result = await ExecuteCommandAsync(createAsset) as AssetSavedResult;

                    context.Complete(EntityCreatedResult.Create(createAsset.AssetId, result.Version));

                    await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), createAsset.AssetId.ToString(), result.FileVersion, null);
                }
                finally
                {
                    await assetStore.DeleteTemporaryAsync(context.ContextId.ToString());
                }

                break;
            }

            case UpdateAsset updateAsset:
            {
                updateAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(updateAsset.File.OpenRead());

                await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), updateAsset.File.OpenRead());

                try
                {
                    var result = await ExecuteCommandAsync(updateAsset) as AssetSavedResult;

                    context.Complete(result);

                    await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), updateAsset.AssetId.ToString(), result.FileVersion, null);
                }
                finally
                {
                    await assetStore.DeleteTemporaryAsync(context.ContextId.ToString());
                }

                break;
            }

            default:
                await base.HandleAsync(context, next);

                break;
            }
        }
        public async Task Should_also_enhance_if_type_already_found()
        {
            var command = new CreateAsset {
                File = file, Type = AssetType.Image
            };

            await sut.EnhanceAsync(command);

            A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(A <Stream> ._))
            .MustHaveHappened();
        }
示例#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);
            }
        }
        protected async Task On(CreateAsset command, CommandContext context)
        {
            await handler.CreateAsync <AssetDomainObject>(context, async c =>
            {
                command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());

                c.Create(command);

                await assetStore.UploadAsync(c.Id.ToString(), c.FileVersion, null, command.File.OpenRead());

                context.Succeed(EntityCreatedResult.Create(c.Id, c.Version));
            });
        }
        public async Task Should_add_image_tag_if_small()
        {
            A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream))
            .Returns(new ImageInfo(100, 100));

            var command = new CreateAsset {
                File = file
            };

            await sut.EnhanceAsync(command, tags);

            Assert.Contains("image", tags);
            Assert.Contains("image/small", tags);
        }
示例#7
0
        public async Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags)
        {
            var imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());

            if (imageInfo != null)
            {
                command.Type = AssetType.Image;

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

                if (tags != null)
                {
                    tags.Add("image");

                    var wh = imageInfo.PixelWidth + imageInfo.PixelHeight;

                    if (wh > 2000)
                    {
                        tags.Add("image/large");
                    }
                    else if (wh > 1000)
                    {
                        tags.Add("image/medium");
                    }
                    else
                    {
                        tags.Add("image/small");
                    }
                }
            }
        }
示例#8
0
        public async Task Should_upload_image_to_store()
        {
            var file = new NoopAssetFile();

            var command = CreateCommand(new UploadAppImage {
                AppId = appId, File = file
            });
            var context = CreateContextForCommand(command);

            A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(A <Stream> ._))
            .Returns(new ImageInfo(100, 100, false));

            await sut.HandleAsync(context);

            A.CallTo(() => appImageStore.UploadAsync(appId.Id, A <Stream> ._, A <CancellationToken> ._))
            .MustHaveHappened();
        }
示例#9
0
        public async Task Should_upload_image_to_store()
        {
            var stream = new MemoryStream();

            var command = CreateCommand(new UploadAppImage {
                AppId = appId, File = () => stream
            });
            var context = CreateContextForCommand(command);

            A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream))
            .Returns(new ImageInfo(100, 100));

            await sut.HandleAsync(context);

            A.CallTo(() => assetStore.UploadAsync(appId.ToString(), stream, true, A <CancellationToken> .Ignored))
            .MustHaveHappened();
        }
示例#10
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_upload_image_to_store()
        {
            var stream = new MemoryStream();

            var file = new AssetFile("name.jpg", "image/jpg", 1024, () => stream);

            var command = CreateCommand(new UploadAppImage {
                AppId = appId, File = file
            });
            var context = CreateContextForCommand(command);

            A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream))
            .Returns(new ImageInfo(100, 100));

            await sut.HandleAsync(context);

            A.CallTo(() => appImageStore.UploadAsync(appId.Id, stream, A <CancellationToken> ._))
            .MustHaveHappened();
        }
示例#12
0
        public async Task Should_convert_between_formats(ImageFormat sourceFormat, ImageFormat targetFormat)
        {
            if (SupportedFormats?.Contains(sourceFormat) == false)
            {
                return;
            }

            if (SupportedFormats?.Contains(targetFormat) == false)
            {
                return;
            }

            var(mimeType, source) = GetImage(sourceFormat);

            await using (var target = GetStream($"transform.{sourceFormat.ToString().ToLowerInvariant()}", targetFormat.ToString().ToLowerInvariant()))
            {
                await sut.CreateThumbnailAsync(source, mimeType, target, new ResizeOptions
                {
                    Format = targetFormat
                });

                target.Position = 0;

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

                Assert.Equal(targetFormat, imageInfo?.Format);
            }
        }
示例#13
0
        protected async Task On(CreateAsset command, CommandContext context)
        {
            command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());

            try
            {
                var asset = await handler.CreateAsync <AssetDomainObject>(context, async a =>
                {
                    a.Create(command);

                    await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), command.File.OpenRead());

                    context.Complete(EntityCreatedResult.Create(a.Id, a.Version));
                });

                await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), asset.Id.ToString(), asset.FileVersion, null);
            }
            finally
            {
                await assetStore.DeleteTemporaryAsync(context.ContextId.ToString());
            }
        }
示例#14
0
        private async Task UploadAsync(UploadAppImage uploadImage)
        {
            var file = uploadImage.File;

            var image = await assetThumbnailGenerator.GetImageInfoAsync(file.OpenRead());

            if (image == null)
            {
                throw new ValidationException("File is not an image.");
            }

            await assetStore.UploadAsync(uploadImage.AppId.ToString(), file.OpenRead(), true);
        }
示例#15
0
        public async Task EnhanceAsync(Media media, AssetFile file)
        {
            if (media.Type == MediaType.Unknown)
            {
                using (var uploadStream = file.OpenRead())
                {
                    var imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream);

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

                        media.Metadata.SetPixelWidth(imageInfo.PixelWidth);
                        media.Metadata.SetPixelHeight(imageInfo.PixelHeight);
                    }
                }
            }
        }
示例#16
0
        private async Task UploadAsync(UploadAppImage uploadImage)
        {
            var file = uploadImage.File;

            using (var uploadStream = file.OpenRead())
            {
                var image = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream);

                if (image == null)
                {
                    throw new ValidationException(T.Get("apps.notImage"));
                }
            }

            using (var uploadStream = file.OpenRead())
            {
                await appImageStore.UploadAsync(uploadImage.AppId.Id, uploadStream);
            }
        }
示例#17
0
        public override async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            if (context.Command is UploadAppImage uploadImage)
            {
                var image = await assetThumbnailGenerator.GetImageInfoAsync(uploadImage.File());

                if (image == null)
                {
                    throw new ValidationException("File is not an image.");
                }

                await assetStore.UploadAsync(uploadImage.AppId.ToString(), uploadImage.File(), true);
            }

            await ExecuteCommandAsync(context);

            if (context.PlainResult is IAppEntity app)
            {
                contextProvider.Context.App = app;
            }

            await next();
        }
示例#18
0
 private void SetupImageInfo()
 {
     A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream))
     .Returns(image);
 }
        public override async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            switch (context.Command)
            {
            case CreateAsset createAsset:
            {
                if (createAsset.Tags == null)
                {
                    createAsset.Tags = new HashSet <string>();
                }

                createAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(createAsset.File.OpenRead());

                createAsset.FileHash = await UploadAsync(context, createAsset.File);

                try
                {
                    var existings = await assetQuery.QueryByHashAsync(createAsset.AppId.Id, createAsset.FileHash);

                    AssetCreatedResult result = null;

                    foreach (var existing in existings)
                    {
                        if (IsDuplicate(createAsset, existing))
                        {
                            result = new AssetCreatedResult(
                                existing.Id,
                                existing.Tags,
                                existing.Version,
                                existing.FileVersion,
                                existing.FileHash,
                                true);
                        }

                        break;
                    }

                    if (result == null)
                    {
                        foreach (var tagGenerator in tagGenerators)
                        {
                            tagGenerator.GenerateTags(createAsset, createAsset.Tags);
                        }

                        var commandResult = (AssetSavedResult) await ExecuteCommandAsync(createAsset);

                        result = new AssetCreatedResult(
                            createAsset.AssetId,
                            createAsset.Tags,
                            commandResult.Version,
                            commandResult.FileVersion,
                            commandResult.FileHash,
                            false);

                        await assetStore.CopyAsync(context.ContextId.ToString(), createAsset.AssetId.ToString(), result.FileVersion, null);
                    }

                    context.Complete(result);
                }
                finally
                {
                    await assetStore.DeleteAsync(context.ContextId.ToString());
                }

                break;
            }

            case UpdateAsset updateAsset:
            {
                updateAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(updateAsset.File.OpenRead());

                updateAsset.FileHash = await UploadAsync(context, updateAsset.File);

                try
                {
                    var result = (AssetSavedResult) await ExecuteCommandAsync(updateAsset);

                    context.Complete(result);

                    await assetStore.CopyAsync(context.ContextId.ToString(), updateAsset.AssetId.ToString(), result.FileVersion, null);
                }
                finally
                {
                    await assetStore.DeleteAsync(context.ContextId.ToString());
                }

                break;
            }

            default:
                await base.HandleAsync(context, next);

                break;
            }
        }
示例#20
0
文件: Program.cs 项目: Squidex/assets
 public async Task GetInfo()
 {
     await generator.GetImageInfoAsync(source, "image/png");
 }
示例#21
0
 private async Task EnrichWithImageInfosAsync(UploadAssetCommand command)
 {
     command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());
 }
示例#22
0
 public Task <ImageInfo?> GetImageInfoAsync(Stream source, string mimeType,
                                            CancellationToken ct = default)
 {
     return(inner.GetImageInfoAsync(source, mimeType, ct));
 }
示例#23
0
        public override async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            switch (context.Command)
            {
            case CreateAsset createAsset:
            {
                if (createAsset.Tags == null)
                {
                    createAsset.Tags = new HashSet <string>();
                }

                createAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(createAsset.File.OpenRead());

                foreach (var tagGenerator in tagGenerators)
                {
                    tagGenerator.GenerateTags(createAsset, createAsset.Tags);
                }

                var originalTags = new HashSet <string>(createAsset.Tags);

                await assetStore.UploadAsync(context.ContextId.ToString(), createAsset.File.OpenRead());

                try
                {
                    var result = await ExecuteCommandAsync(createAsset) as AssetSavedResult;

                    context.Complete(new AssetCreatedResult(createAsset.AssetId, originalTags, result.Version));

                    await assetStore.CopyAsync(context.ContextId.ToString(), createAsset.AssetId.ToString(), result.FileVersion, null);
                }
                finally
                {
                    await assetStore.DeleteAsync(context.ContextId.ToString());
                }

                break;
            }

            case UpdateAsset updateAsset:
            {
                updateAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(updateAsset.File.OpenRead());

                await assetStore.UploadAsync(context.ContextId.ToString(), updateAsset.File.OpenRead());

                try
                {
                    var result = await ExecuteCommandAsync(updateAsset) as AssetSavedResult;

                    context.Complete(result);

                    await assetStore.CopyAsync(context.ContextId.ToString(), updateAsset.AssetId.ToString(), result.FileVersion, null);
                }
                finally
                {
                    await assetStore.DeleteAsync(context.ContextId.ToString());
                }

                break;
            }

            default:
                await base.HandleAsync(context, next);

                break;
            }
        }
示例#24
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");
                }
            }
        }