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: { 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; } }
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: { 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, 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; } }