|
| 1 | +using ClassifiedAds.Application.FileEntries.MessageBusEvents; |
| 2 | +using ClassifiedAds.Domain.Entities; |
| 3 | +using ClassifiedAds.Domain.Infrastructure.Messaging; |
| 4 | +using ClassifiedAds.Domain.Infrastructure.Storages; |
| 5 | +using CryptographyHelper; |
| 6 | +using CryptographyHelper.SymmetricAlgorithms; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using System; |
| 11 | +using System.Net.Http; |
| 12 | +using System.Net.Http.Headers; |
| 13 | +using System.Security.Cryptography; |
| 14 | +using System.Threading; |
| 15 | +using System.Threading.Tasks; |
| 16 | + |
| 17 | +namespace ClassifiedAds.Background.MessageBusConsumers; |
| 18 | + |
| 19 | +public sealed class FileEmbeddingConsumer : |
| 20 | + IMessageBusConsumer<FileEmbeddingConsumer, FileCreatedEvent>, |
| 21 | + IMessageBusConsumer<FileEmbeddingConsumer, FileUpdatedEvent>, |
| 22 | + IMessageBusConsumer<FileEmbeddingConsumer, FileDeletedEvent> |
| 23 | +{ |
| 24 | + private static readonly HttpClient _httpClient = new HttpClient(); |
| 25 | + |
| 26 | + private readonly ILogger<FileEmbeddingConsumer> _logger; |
| 27 | + private readonly IConfiguration _configuration; |
| 28 | + private readonly IServiceProvider _serviceProvider; |
| 29 | + |
| 30 | + public FileEmbeddingConsumer(ILogger<FileEmbeddingConsumer> logger, |
| 31 | + IConfiguration configuration, |
| 32 | + IServiceProvider serviceProvider) |
| 33 | + { |
| 34 | + _logger = logger; |
| 35 | + _configuration = configuration; |
| 36 | + _serviceProvider = serviceProvider; |
| 37 | + } |
| 38 | + |
| 39 | + public async Task HandleAsync(FileCreatedEvent data, MetaData metaData, CancellationToken cancellationToken = default) |
| 40 | + { |
| 41 | + _logger.LogInformation("Handling FileCreatedEvent for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 42 | + |
| 43 | + if (string.IsNullOrEmpty(data?.FileEntry?.FileLocation)) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (data.FileEntry.FileName.EndsWith(".txt") || |
| 49 | + data.FileEntry.FileName.EndsWith(".md") || |
| 50 | + data.FileEntry.FileName.EndsWith(".markdown")) |
| 51 | + { |
| 52 | + _logger.LogInformation("Skipping text file for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (data.FileEntry.FileName.EndsWith(".pdf") || |
| 57 | + data.FileEntry.FileName.EndsWith(".docx")) |
| 58 | + { |
| 59 | + _logger.LogInformation("Converting file to markdown for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 60 | + |
| 61 | + var markdown = await ConvertToMarkdownAsync(data.FileEntry, cancellationToken); |
| 62 | + |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + public async Task HandleAsync(FileUpdatedEvent data, MetaData metaData, CancellationToken cancellationToken = default) |
| 70 | + { |
| 71 | + _logger.LogInformation("Handling FileUpdatedEvent for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 72 | + |
| 73 | + if (string.IsNullOrEmpty(data?.FileEntry?.FileLocation)) |
| 74 | + { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (data.FileEntry.FileName.EndsWith(".txt") || |
| 79 | + data.FileEntry.FileName.EndsWith(".md") || |
| 80 | + data.FileEntry.FileName.EndsWith(".markdown")) |
| 81 | + { |
| 82 | + _logger.LogInformation("Skipping text file for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (data.FileEntry.FileName.EndsWith(".pdf") || |
| 87 | + data.FileEntry.FileName.EndsWith(".docx")) |
| 88 | + { |
| 89 | + _logger.LogInformation("Converting file to markdown for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 90 | + |
| 91 | + var markdown = await ConvertToMarkdownAsync(data.FileEntry, cancellationToken); |
| 92 | + |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + public Task HandleAsync(FileDeletedEvent data, MetaData metaData, CancellationToken cancellationToken = default) |
| 100 | + { |
| 101 | + _logger.LogInformation("Handling FileDeletedEvent for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 102 | + return Task.CompletedTask; |
| 103 | + } |
| 104 | + |
| 105 | + private async Task<string> ConvertToMarkdownAsync(FileEntry fileEntry, CancellationToken cancellationToken = default) |
| 106 | + { |
| 107 | + // TODO: xxx |
| 108 | + var content = await _serviceProvider.CreateScope().ServiceProvider.GetRequiredService<IFileStorageManager>().ReadAsync(fileEntry, cancellationToken); |
| 109 | + |
| 110 | + if (fileEntry.Encrypted) |
| 111 | + { |
| 112 | + var masterEncryptionKey = _configuration["Storage:MasterEncryptionKey"]; |
| 113 | + var encryptionKey = fileEntry.EncryptionKey.FromBase64String() |
| 114 | + .UseAES(masterEncryptionKey.FromBase64String()) |
| 115 | + .WithCipher(CipherMode.CBC) |
| 116 | + .WithIV(fileEntry.EncryptionIV.FromBase64String()) |
| 117 | + .WithPadding(PaddingMode.PKCS7) |
| 118 | + .Decrypt(); |
| 119 | + |
| 120 | + content = fileEntry.FileLocation != "Fake.txt" |
| 121 | + ? content |
| 122 | + .UseAES(encryptionKey) |
| 123 | + .WithCipher(CipherMode.CBC) |
| 124 | + .WithIV(fileEntry.EncryptionIV.FromBase64String()) |
| 125 | + .WithPadding(PaddingMode.PKCS7) |
| 126 | + .Decrypt() |
| 127 | + : content; |
| 128 | + } |
| 129 | + |
| 130 | + using var form = new MultipartFormDataContent(); |
| 131 | + using var fileContent = new ByteArrayContent(content); |
| 132 | + fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); |
| 133 | + form.Add(fileContent, "formFile", fileEntry.FileName); |
| 134 | + form.Add(new StringContent("Test Name"), "name"); |
| 135 | + |
| 136 | + var response = await _httpClient.PostAsync(_configuration["TextExtracting:MarkItDownServer:Endpoint"], form, cancellationToken); |
| 137 | + response.EnsureSuccessStatusCode(); |
| 138 | + |
| 139 | + var markdown = await response.Content.ReadAsStringAsync(cancellationToken); |
| 140 | + |
| 141 | + return markdown; |
| 142 | + } |
| 143 | +} |
0 commit comments