| | | 1 | | using Elsa.WorkflowProviders.BlobStorage.Contracts; |
| | | 2 | | using Elsa.Workflows.Runtime; |
| | | 3 | | using FluentStorage.Blobs; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | |
| | | 6 | | namespace Elsa.WorkflowProviders.BlobStorage.Providers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A workflow definition provider that loads workflow definitions from a storage using FluentStorage (See https://githu |
| | | 10 | | /// </summary> |
| | | 11 | | [PublicAPI] |
| | | 12 | | public class BlobStorageWorkflowsProvider : IWorkflowsProvider |
| | | 13 | | { |
| | | 14 | | private readonly IBlobStorageProvider _blobStorageProvider; |
| | | 15 | | private readonly IEnumerable<IBlobWorkflowFormatHandler> _handlers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="BlobStorageWorkflowsProvider"/> class. |
| | | 19 | | /// </summary> |
| | 266 | 20 | | public BlobStorageWorkflowsProvider( |
| | 266 | 21 | | IBlobStorageProvider blobStorageProvider, |
| | 266 | 22 | | IEnumerable<IBlobWorkflowFormatHandler> handlers) |
| | | 23 | | { |
| | 266 | 24 | | _blobStorageProvider = blobStorageProvider; |
| | 266 | 25 | | _handlers = handlers; |
| | 266 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 0 | 29 | | public string Name => "FluentStorage"; |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public async ValueTask<IEnumerable<MaterializedWorkflow>> GetWorkflowsAsync(CancellationToken cancellationToken = de |
| | | 33 | | { |
| | | 34 | | // Aggregate supported extensions from all handlers |
| | 8 | 35 | | var supportedExtensions = _handlers |
| | 16 | 36 | | .SelectMany(h => h.SupportedExtensions) |
| | 16 | 37 | | .Where(ext => !string.IsNullOrEmpty(ext)) |
| | 16 | 38 | | .Select(ext => ext.ToLowerInvariant()) |
| | 8 | 39 | | .ToHashSet(); |
| | | 40 | | |
| | 8 | 41 | | var options = new ListOptions |
| | 8 | 42 | | { |
| | 8 | 43 | | Recurse = true, |
| | 8 | 44 | | BrowseFilter = blob => |
| | 8 | 45 | | { |
| | 8 | 46 | | // If no handlers declare extensions, accept all files |
| | 304 | 47 | | if (supportedExtensions.Count == 0) |
| | 0 | 48 | | return true; |
| | 8 | 49 | | |
| | 8 | 50 | | // Only accept files with supported extensions |
| | 304 | 51 | | var extension = Path.GetExtension(blob.Name).TrimStart('.').ToLowerInvariant(); |
| | 304 | 52 | | return supportedExtensions.Contains(extension); |
| | 8 | 53 | | } |
| | 8 | 54 | | }; |
| | | 55 | | |
| | 8 | 56 | | var blobStorage = _blobStorageProvider.GetBlobStorage(); |
| | 8 | 57 | | var blobs = await blobStorage.ListFilesAsync(options, cancellationToken); |
| | 8 | 58 | | var results = new List<MaterializedWorkflow>(); |
| | | 59 | | |
| | 352 | 60 | | foreach (var blob in blobs) |
| | | 61 | | { |
| | 168 | 62 | | var workflow = await TryReadWorkflowAsync(blob, cancellationToken); |
| | 168 | 63 | | if (workflow != null) |
| | 168 | 64 | | results.Add(workflow); |
| | | 65 | | } |
| | | 66 | | |
| | 8 | 67 | | return results; |
| | 8 | 68 | | } |
| | | 69 | | |
| | | 70 | | private async Task<MaterializedWorkflow?> TryReadWorkflowAsync(Blob blob, CancellationToken cancellationToken) |
| | | 71 | | { |
| | 168 | 72 | | var blobStorage = _blobStorageProvider.GetBlobStorage(); |
| | 168 | 73 | | var content = await blobStorage.ReadTextAsync(blob.FullPath, cancellationToken: cancellationToken); |
| | | 74 | | |
| | 168 | 75 | | var contentType = blob.Properties.TryGetValue("ContentType", out var ct) ? ct?.ToString() : null; |
| | | 76 | | |
| | 504 | 77 | | foreach (var handler in _handlers) |
| | | 78 | | { |
| | 168 | 79 | | if (!handler.CanHandle(blob, contentType)) |
| | | 80 | | continue; |
| | | 81 | | |
| | 168 | 82 | | var result = await handler.TryParseAsync(blob, content, cancellationToken); |
| | 168 | 83 | | if (result != null) |
| | 168 | 84 | | return result; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // No handler accepted this blob; ignore it |
| | 0 | 88 | | return null; |
| | 168 | 89 | | } |
| | | 90 | | } |