Browse Source

Azure metadata source.

pull/748/head
Sebastian 4 years ago
parent
commit
a0213d5ec0
  1. 2
      backend/extensions/Squidex.Extensions/Actions/Algolia/AlgoliaActionHandler.cs
  2. 6
      backend/extensions/Squidex.Extensions/Actions/Kafka/KafkaPlugin.cs
  3. 100
      backend/extensions/Squidex.Extensions/Assets/Azure/AzureMetadataSource.cs
  4. 23
      backend/extensions/Squidex.Extensions/Assets/Azure/AzureMetadataSourceOptions.cs
  5. 30
      backend/extensions/Squidex.Extensions/Assets/Azure/AzureMetadataSourcePlugin.cs
  6. 1
      backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj

2
backend/extensions/Squidex.Extensions/Actions/Algolia/AlgoliaActionHandler.cs

@ -45,7 +45,7 @@ namespace Squidex.Extensions.Actions.Algolia
var ruleDescription = string.Empty;
var contentId = entityEvent.Id.ToString();
var content = (JObject?)null;
var content = (JObject)null;
if (delete)
{

6
backend/extensions/Squidex.Extensions/Actions/Kafka/KafkaPlugin.cs

@ -16,14 +16,14 @@ namespace Squidex.Extensions.Actions.Kafka
{
public void ConfigureServices(IServiceCollection services, IConfiguration config)
{
var kafkaOptions = config.GetSection("kafka").Get<KafkaProducerOptions>();
var options = config.GetSection("kafka").Get<KafkaProducerOptions>();
if (kafkaOptions.IsProducerConfigured())
if (options.IsProducerConfigured())
{
services.AddRuleAction<KafkaAction, KafkaActionHandler>();
services.AddSingleton<KafkaProducer>();
services.AddSingleton(Options.Create(kafkaOptions));
services.AddSingleton(Options.Create(options));
}
}
}

100
backend/extensions/Squidex.Extensions/Assets/Azure/AzureMetadataSource.cs

@ -0,0 +1,100 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using Microsoft.Extensions.Options;
using Squidex.Domain.Apps.Core.Assets;
using Squidex.Domain.Apps.Entities.Assets;
using Squidex.Domain.Apps.Entities.Assets.Commands;
using Squidex.Infrastructure.Json.Objects;
using Squidex.Log;
namespace Squidex.Extensions.Assets.Azure
{
public sealed class AzureMetadataSource : IAssetMetadataSource
{
private const long MaxSize = 5 * 1025 * 1024;
private readonly ISemanticLog log;
private readonly ComputerVisionClient client;
private readonly char[] trimChars =
{
' ',
'_',
'-'
};
private readonly List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>
{
VisualFeatureTypes.Categories,
VisualFeatureTypes.Description,
VisualFeatureTypes.Color
};
public int Order => int.MaxValue;
public AzureMetadataSource(IOptions<AzureMetadataSourceOptions> options, ISemanticLog log)
{
client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(options.Value.ApiKey))
{
Endpoint = options.Value.Endpoint
};
this.log = log;
}
public async Task EnhanceAsync(UploadAssetCommand command)
{
try
{
if (command.Type == AssetType.Image && command.File.FileSize <= MaxSize)
{
using (var stream = command.File.OpenRead())
{
var result = await client.AnalyzeImageInStreamAsync(stream, features);
command.Tags ??= new HashSet<string>();
if (result.Color?.DominantColorForeground != null)
{
command.Tags.Add($"color/{result.Color.DominantColorForeground.Trim(trimChars).ToLowerInvariant()}");
}
if (result.Categories != null)
{
foreach (var category in result.Categories.OrderByDescending(x => x.Score).Take(3))
{
command.Tags.Add($"category/{category.Name.Trim(trimChars).ToLowerInvariant()}");
}
}
var description = result.Description?.Captions?.OrderByDescending(x => x.Confidence)?.FirstOrDefault()?.Text;
if (description != null)
{
command.Metadata["caption"] = JsonValue.Create(description);
}
}
}
}
catch (Exception ex)
{
log.LogError(ex, w => w
.WriteProperty("action", "EnrichWithAssure")
.WriteProperty("status", "Failed"));
}
}
public IEnumerable<string> Format(IAssetEntity asset)
{
yield break;
}
}
}

23
backend/extensions/Squidex.Extensions/Assets/Azure/AzureMetadataSourceOptions.cs

@ -0,0 +1,23 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
namespace Squidex.Extensions.Assets.Azure
{
public sealed class AzureMetataSourceOptions
{
public string Endpoint { get; set; }
public string ApiKey { get; set; }
public bool IsConfigured()
{
return
!string.IsNullOrWhiteSpace(Endpoint) &&
!string.IsNullOrWhiteSpace(ApiKey);
}
}
}

30
backend/extensions/Squidex.Extensions/Assets/Azure/AzureMetadataSourcePlugin.cs

@ -0,0 +1,30 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Squidex.Domain.Apps.Entities.Assets;
using Squidex.Infrastructure.Plugins;
namespace Squidex.Extensions.Assets.Azure
{
public sealed class AzureMetadataSourcePlugin : IPlugin
{
public void ConfigureServices(IServiceCollection services, IConfiguration config)
{
var options = config.GetSection("assets:azureCognitive").Get<AzureMetadataSourceOptions>();
if (options.IsConfigured())
{
services.AddSingleton<IAssetMetadataSource, AzureMetadataSource>();
services.AddSingleton(Options.Create(options));
}
}
}
}

1
backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj

@ -16,6 +16,7 @@
<PackageReference Include="Datadog.Trace" Version="1.27.1" />
<PackageReference Include="Elasticsearch.Net" Version="7.13.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.17.0" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.Vision.ComputerVision" Version="7.0.0" />
<PackageReference Include="Microsoft.Azure.SignalR.Management" Version="1.9.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="Microsoft.OData.Core" Version="7.9.0" />

Loading…
Cancel
Save