public AssetCommandMiddlewareTests() { file = new NoopAssetFile(); var assetDomainObject = new AssetDomainObject(Store, A.Dummy <ISemanticLog>(), tagService, assetQuery, contentRepository); A.CallTo(() => serviceProvider.GetService(typeof(AssetDomainObject))) .Returns(assetDomainObject); asset = new AssetDomainObjectGrain(serviceProvider, null !); asset.ActivateAsync(Id.ToString()).Wait(); A.CallTo(() => contextProvider.Context) .Returns(requestContext); A.CallTo(() => assetEnricher.EnrichAsync(A <IAssetEntity> ._, requestContext)) .ReturnsLazily(() => SimpleMapper.Map(asset.Snapshot, new AssetEntity())); A.CallTo(() => assetQuery.FindByHashAsync(A <Context> ._, A <string> ._, A <string> ._, A <long> ._)) .Returns(Task.FromResult <IEnrichedAssetEntity?>(null)); A.CallTo(() => grainFactory.GetGrain <IAssetGrain>(Id.ToString(), null)) .Returns(asset); sut = new AssetCommandMiddleware(grainFactory, assetEnricher, assetFileStore, assetQuery, contextProvider, new[] { assetMetadataSource }); }
private async Task UploadWithDuplicateCheckAsync(CommandContext context, UploadAssetCommand command, bool duplicate, NextDelegate next) { var tempFile = context.ContextId.ToString(); try { await EnrichWithHashAndUploadAsync(command, tempFile); if (!duplicate) { var existing = await assetQuery.FindByHashAsync(contextProvider.Context, command.FileHash, command.File.FileName, command.File.FileSize); if (existing != null) { context.Complete(new AssetDuplicate(existing)); await next(context); return; } } await EnrichWithMetadataAsync(command); await base.HandleAsync(context, next); } finally { await assetFileStore.DeleteAsync(tempFile); await command.File.DisposeAsync(); } }
public override async Task HandleAsync(CommandContext context, NextDelegate next) { switch (context.Command) { case CreateAsset createAsset: { var tempFile = context.ContextId.ToString(); try { await EnrichWithHashAndUploadAsync(createAsset, tempFile); if (!createAsset.Duplicate) { var existing = await assetQuery.FindByHashAsync(contextProvider.Context, createAsset.FileHash, createAsset.File.FileName, createAsset.File.FileSize); if (existing != null) { context.Complete(new AssetDuplicate(existing)); await next(context); return; } } await EnrichWithMetadataAsync(createAsset); await base.HandleAsync(context, next); } finally { await assetFileStore.DeleteAsync(tempFile); createAsset.File.Dispose(); } break; } case MoveAsset move: { await base.HandleAsync(context, next); break; } case UpsertAsset upsert: { await UploadAndHandleAsync(context, next, upsert); break; } case UpdateAsset upload: { await UploadAndHandleAsync(context, next, upload); 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: { try { await EnrichWithHashAndUploadAsync(createAsset, tempFile); if (!createAsset.Duplicate) { var existing = await assetQuery.FindByHashAsync(contextProvider.Context, createAsset.FileHash, createAsset.File.FileName, createAsset.File.FileSize); if (existing != null) { 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; } }