public AssetCommandMiddlewareTests() { file = new AssetFile("my-image.png", "image/png", 1024, () => stream); var assetDomainObject = new AssetDomainObject(Store, tagService, assetQuery, A.Dummy <ISemanticLog>()); A.CallTo(() => serviceProvider.GetService(typeof(AssetDomainObject))) .Returns(assetDomainObject); asset = new AssetDomainObjectGrain(serviceProvider, null !); 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.ShouldEnrichAsset()), AppId, A <string> .Ignored)) .Returns(new List <IEnrichedAssetEntity>()); A.CallTo(() => grainFactory.GetGrain <IAssetGrain>(Id, null)) .Returns(asset); sut = new AssetCommandMiddleware(grainFactory, assetEnricher, assetFileStore, assetQuery, contextProvider, new[] { assetMetadataSource }); }
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 }); }
public AssetCommandMiddlewareTests() { file = new AssetFile("my-image.png", "image/png", 1024, () => stream); asset = new AssetGrain(Store, tagService, A.Dummy <ISemanticLog>()); asset.ActivateAsync(Id).Wait(); A.CallTo(() => assetQuery.QueryByHashAsync(AppId, A <string> .Ignored)) .Returns(new List <IAssetEntity>()); A.CallTo(() => tagService.NormalizeTagsAsync(AppId, TagGroups.Assets, A <HashSet <string> > .Ignored, A <HashSet <string> > .Ignored)) .Returns(new Dictionary <string, string>()); A.CallTo(() => grainFactory.GetGrain <IAssetGrain>(Id, null)) .Returns(asset); sut = new AssetCommandMiddleware(grainFactory, assetQuery, assetStore, assetThumbnailGenerator, new[] { tagGenerator }); }
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; } }
public override async Task HandleAsync(CommandContext context, NextDelegate next) { var tempFile = context.ContextId.ToString(); switch (context.Command) { case CreateAsset createAsset: { await EnrichWithHashAndUploadAsync(createAsset, tempFile); try { var ctx = contextProvider.Context.Clone().WithNoAssetEnrichment(); var existings = await assetQuery.QueryByHashAsync(ctx, createAsset.AppId.Id, createAsset.FileHash); foreach (var existing in existings) { if (IsDuplicate(existing, createAsset.File)) { var result = new AssetCreatedResult(existing, true); context.Complete(result); await next(context); return; } } await EnrichWithMetadataAsync(createAsset, createAsset.Tags); await HandleCoreAsync(context, next); var asset = context.Result <IEnrichedAssetEntity>(); context.Complete(new AssetCreatedResult(asset, false)); await assetFileStore.CopyAsync(tempFile, createAsset.AssetId, asset.FileVersion); } finally { await assetFileStore.DeleteAsync(tempFile); } break; } case UpdateAsset updateAsset: { await EnrichWithMetadataAsync(updateAsset); await EnrichWithHashAndUploadAsync(updateAsset, tempFile); try { await HandleCoreAsync(context, next); var asset = context.Result <IEnrichedAssetEntity>(); await assetFileStore.CopyAsync(tempFile, updateAsset.AssetId, asset.FileVersion); } finally { await assetFileStore.DeleteAsync(tempFile); } break; } default: await HandleCoreAsync(context, next); break; } }
public override async Task HandleAsync(CommandContext context, Func <Task> next) { var tempFile = context.ContextId.ToString(); switch (context.Command) { case CreateAsset createAsset: { await EnrichWithImageInfosAsync(createAsset); await EnrichWithHashAndUploadAsync(createAsset, tempFile); try { var existings = await assetQuery.QueryByHashAsync(createAsset.AppId.Id, createAsset.FileHash); foreach (var existing in existings) { if (IsDuplicate(existing, createAsset.File)) { var result = new AssetCreatedResult(existing, true); context.Complete(result); await next(); return; } } GenerateTags(createAsset); await HandleCoreAsync(context, next); var asset = context.Result <IEnrichedAssetEntity>(); context.Complete(new AssetCreatedResult(asset, false)); await assetStore.CopyAsync(tempFile, createAsset.AssetId.ToString(), asset.FileVersion, null); } finally { await assetStore.DeleteAsync(tempFile); } break; } case UpdateAsset updateAsset: { await EnrichWithImageInfosAsync(updateAsset); await EnrichWithHashAndUploadAsync(updateAsset, tempFile); try { await HandleCoreAsync(context, next); var asset = context.Result <IEnrichedAssetEntity>(); await assetStore.CopyAsync(tempFile, updateAsset.AssetId.ToString(), asset.FileVersion, null); } finally { await assetStore.DeleteAsync(tempFile); } break; } default: await HandleCoreAsync(context, next); break; } }
public override async Task HandleAsync(CommandContext context, NextDelegate next) { var tempFile = context.ContextId.ToString(); switch (context.Command) { case CreateAsset createAsset: { try { await EnrichWithHashAndUploadAsync(createAsset, tempFile); var ctx = contextProvider.Context.Clone().WithoutAssetEnrichment(); if (!createAsset.Duplicate) { var existings = await assetQuery.QueryByHashAsync(ctx, createAsset.AppId.Id, createAsset.FileHash); foreach (var existing in existings) { if (IsDuplicate(existing, createAsset.File)) { var result = new AssetCreatedResult(existing, true); context.Complete(result); await next(context); return; } } } await UploadAsync(context, tempFile, createAsset, createAsset.Tags, true, next); } finally { await assetFileStore.DeleteAsync(tempFile); createAsset.File.Dispose(); } break; } case UpdateAsset updateAsset: { try { await EnrichWithHashAndUploadAsync(updateAsset, tempFile); await UploadAsync(context, tempFile, updateAsset, null, false, next); } finally { await assetFileStore.DeleteAsync(tempFile); updateAsset.File.Dispose(); } break; } default: await HandleCoreAsync(context, false, next); break; } }