|
8 | 8 | using Microsoft.Extensions.DependencyInjection; |
9 | 9 | using Microsoft.Extensions.Logging; |
10 | 10 | using System; |
| 11 | +using System.IO; |
11 | 12 | using System.Net.Http; |
12 | 13 | using System.Net.Http.Headers; |
13 | 14 | using System.Security.Cryptography; |
@@ -40,72 +41,68 @@ public async Task HandleAsync(FileCreatedEvent data, MetaData metaData, Cancella |
40 | 41 | { |
41 | 42 | _logger.LogInformation("Handling FileCreatedEvent for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
42 | 43 |
|
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 | | - } |
| 44 | + await ProcessFileAsync(data.FileEntry, cancellationToken); |
| 45 | + } |
55 | 46 |
|
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); |
| 47 | + public async Task HandleAsync(FileUpdatedEvent data, MetaData metaData, CancellationToken cancellationToken = default) |
| 48 | + { |
| 49 | + _logger.LogInformation("Handling FileUpdatedEvent for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
60 | 50 |
|
61 | | - var markdown = await ConvertToMarkdownAsync(data.FileEntry, cancellationToken); |
| 51 | + await ProcessFileAsync(data.FileEntry, cancellationToken); |
| 52 | + } |
62 | 53 |
|
63 | | - return; |
64 | | - } |
| 54 | + public Task HandleAsync(FileDeletedEvent data, MetaData metaData, CancellationToken cancellationToken = default) |
| 55 | + { |
| 56 | + _logger.LogInformation("Handling FileDeletedEvent for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
65 | 57 |
|
66 | | - return; |
| 58 | + return Task.CompletedTask; |
67 | 59 | } |
68 | 60 |
|
69 | | - public async Task HandleAsync(FileUpdatedEvent data, MetaData metaData, CancellationToken cancellationToken = default) |
| 61 | + private async Task ProcessFileAsync(FileEntry fileEntry, CancellationToken cancellationToken) |
70 | 62 | { |
71 | | - _logger.LogInformation("Handling FileUpdatedEvent for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
72 | | - |
73 | | - if (string.IsNullOrEmpty(data?.FileEntry?.FileLocation)) |
| 63 | + if (string.IsNullOrEmpty(fileEntry?.FileLocation)) |
74 | 64 | { |
75 | 65 | return; |
76 | 66 | } |
77 | 67 |
|
78 | | - if (data.FileEntry.FileName.EndsWith(".txt") || |
79 | | - data.FileEntry.FileName.EndsWith(".md") || |
80 | | - data.FileEntry.FileName.EndsWith(".markdown")) |
| 68 | + using var scope = _serviceProvider.CreateScope(); |
| 69 | + var fileStorageManager = scope.ServiceProvider.GetService<IFileStorageManager>(); |
| 70 | + |
| 71 | + var fileExtension = Path.GetExtension(fileEntry.FileName); |
| 72 | + |
| 73 | + if (fileExtension == ".txt" || |
| 74 | + fileExtension == ".md" || |
| 75 | + fileExtension == ".markdown") |
81 | 76 | { |
82 | | - _logger.LogInformation("Skipping text file for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 77 | + // TODO: xxx |
83 | 78 | return; |
84 | 79 | } |
85 | 80 |
|
86 | | - if (data.FileEntry.FileName.EndsWith(".pdf") || |
87 | | - data.FileEntry.FileName.EndsWith(".docx")) |
| 81 | + if (fileExtension == ".pdf" || |
| 82 | + fileExtension == ".docx") |
88 | 83 | { |
89 | | - _logger.LogInformation("Converting file to markdown for FileEntry Id: {FileEntryId}", data?.FileEntry?.Id); |
| 84 | + _logger.LogInformation("Converting file to markdown for FileEntry Id: {FileEntryId}", fileEntry?.Id); |
90 | 85 |
|
91 | | - var markdown = await ConvertToMarkdownAsync(data.FileEntry, cancellationToken); |
| 86 | + var markdownFolder = Path.Combine(_configuration["Storage:TempFolderPath"], "Markdown"); |
92 | 87 |
|
93 | | - return; |
94 | | - } |
| 88 | + if (!Directory.Exists(markdownFolder)) |
| 89 | + { |
| 90 | + Directory.CreateDirectory(markdownFolder); |
| 91 | + } |
95 | 92 |
|
96 | | - return; |
97 | | - } |
| 93 | + var markdownFile = Path.Combine(markdownFolder, fileEntry.Id + ".md"); |
98 | 94 |
|
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; |
| 95 | + if (!File.Exists(markdownFile)) |
| 96 | + { |
| 97 | + var markdown = await ConvertToMarkdownAsync(fileStorageManager, fileEntry, cancellationToken); |
| 98 | + await File.WriteAllTextAsync(markdownFile, markdown, cancellationToken); |
| 99 | + } |
| 100 | + } |
103 | 101 | } |
104 | 102 |
|
105 | | - private async Task<string> ConvertToMarkdownAsync(FileEntry fileEntry, CancellationToken cancellationToken = default) |
| 103 | + private async Task<string> ConvertToMarkdownAsync(IFileStorageManager fileStorageManager, FileEntry fileEntry, CancellationToken cancellationToken = default) |
106 | 104 | { |
107 | | - // TODO: xxx |
108 | | - var content = await _serviceProvider.CreateScope().ServiceProvider.GetRequiredService<IFileStorageManager>().ReadAsync(fileEntry, cancellationToken); |
| 105 | + var content = await fileStorageManager.ReadAsync(fileEntry, cancellationToken); |
109 | 106 |
|
110 | 107 | if (fileEntry.Encrypted) |
111 | 108 | { |
|
0 commit comments