-
Notifications
You must be signed in to change notification settings - Fork 33
Adding 'IO' module from elsa-core #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
src/modules/io/Elsa.IO.Compression/Activities/CreateZipArchive.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| using System.IO.Compression; | ||
| using System.Text.Json.Serialization; | ||
| using Elsa.Extensions; | ||
| using Elsa.IO.Contracts; | ||
| using Elsa.IO.Extensions; | ||
| using Elsa.Workflows; | ||
| using Elsa.Workflows.Attributes; | ||
| using Elsa.Workflows.Models; | ||
| using Elsa.Workflows.UIHints; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Elsa.IO.Compression.Activities; | ||
|
|
||
| /// <summary> | ||
| /// Creates a ZIP archive from a collection of entries. | ||
| /// </summary> | ||
| [Activity("Elsa", "Compression", "Creates a ZIP archive from a collection of entries.", DisplayName = "Create Zip Archive")] | ||
| public class CreateZipArchive : CodeActivity<Stream> | ||
| { | ||
| private const string DefaultArchiveName = "archive.zip"; | ||
| private const string ZipExtension = ".zip"; | ||
| private const string DefaultEntryNameFormat = "entry_{0}"; | ||
|
|
||
| /// <inheritdoc /> | ||
| [JsonConstructor] | ||
| public CreateZipArchive(string? source = null, int? line = null) : base(source, line) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The entries to include in the ZIP archive. Can be byte[], Stream, file path, file URL, base64 string, ZipEntry objects, or arrays of these types. | ||
| /// </summary> | ||
| [Input( | ||
| Description = "The entries to include in the ZIP archive. Can be byte[], Stream, file path, file URL, base64 string, ZipEntry objects, or arrays of these types", | ||
| UIHint = InputUIHints.MultiLine | ||
| )] | ||
| public Input<object?> Entries { get; set; } = null!; | ||
|
|
||
| /// <summary> | ||
| /// The compression level for the Zip Entries. Default is Optimal | ||
| /// </summary> | ||
| [Input( | ||
| Description = "The compression level for the Zip Entries. Default is Optimal", | ||
| UIHint = InputUIHints.DropDown | ||
| )] | ||
| public Input<CompressionLevel> CompressionLevel { get; set; } = new(System.IO.Compression.CompressionLevel.Optimal); | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) | ||
| { | ||
| var entriesInput = Entries.Get(context); | ||
| var resolver = context.GetRequiredService<IContentResolver>(); | ||
| var logger = context.GetRequiredService<ILogger<CreateZipArchive>>(); | ||
|
|
||
| var entries = ParseEntries(entriesInput); | ||
|
|
||
| var zipStream = await CreateZipStreamFromEntries(entries, resolver, context, logger); | ||
|
|
||
| Result.Set(context, zipStream); | ||
| } | ||
|
|
||
| private static IEnumerable<object> ParseEntries(object? entriesInput) | ||
| { | ||
| return entriesInput switch | ||
| { | ||
| null => [], | ||
| IEnumerable<object> enumerable => enumerable, | ||
| Array array => array.Cast<object>(), | ||
| _ => [entriesInput] | ||
| }; | ||
| } | ||
|
|
||
| private async Task<Stream> CreateZipStreamFromEntries( | ||
| IEnumerable<object> entries, | ||
| IContentResolver resolver, | ||
| ActivityExecutionContext context, | ||
| ILogger logger) | ||
| { | ||
| var zipStream = new MemoryStream(); | ||
|
|
||
| try | ||
| { | ||
| using var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Update, leaveOpen: true); | ||
| var entryIndex = 0; | ||
|
|
||
| var compressionLevel = CompressionLevel.Get(context); | ||
| foreach (var entryContent in entries) | ||
| { | ||
| try | ||
| { | ||
| await ProcessZipEntry(entryContent, zipArchive, resolver, context, entryIndex, compressionLevel); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogWarning(ex, "Failed to add entry {EntryIndex} to ZIP archive. Reason: {ExceptionMessage}", | ||
| entryIndex, ex.Message); | ||
| } | ||
| entryIndex++; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "Failed to create ZIP archive"); | ||
| await zipStream.DisposeAsync(); | ||
| throw; | ||
| } | ||
|
|
||
| // Reset stream position for reading | ||
| zipStream.Position = 0; | ||
| return zipStream; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Processes a single zip entry and adds it to the archive. | ||
| /// </summary> | ||
| private static async Task ProcessZipEntry( | ||
| object entryContent, | ||
| ZipArchive zipArchive, | ||
| IContentResolver resolver, | ||
| ActivityExecutionContext context, | ||
| int entryIndex, | ||
| CompressionLevel compressionLevel) | ||
| { | ||
| var binaryContent = await resolver.ResolveAsync(entryContent, context.CancellationToken); | ||
|
|
||
| var entryName = binaryContent.Name?.GetNameAndExtension() | ||
| ?? string.Format(DefaultEntryNameFormat, entryIndex + 1); | ||
|
|
||
| // Get a unique name following Windows convention | ||
| entryName = GetUniqueEntryName(zipArchive, entryName); | ||
|
|
||
| var archiveEntry = zipArchive.CreateEntry(entryName, compressionLevel); | ||
|
|
||
| await using var entryStream = archiveEntry.Open(); | ||
| await binaryContent.Stream.CopyToAsync(entryStream, context.CancellationToken); | ||
| await entryStream.FlushAsync(context.CancellationToken); | ||
|
|
||
| if (entryContent is not Stream) | ||
| { | ||
| await binaryContent.Stream.DisposeAsync(); | ||
| } | ||
| } | ||
|
|
||
| private static string GetUniqueEntryName(ZipArchive zipArchive, string originalName) | ||
| { | ||
| var filenameWithoutExtension = Path.GetFileNameWithoutExtension(originalName); | ||
| var extension = Path.GetExtension(originalName); | ||
|
|
||
| var originalExists = false; | ||
| var highestIndex = 0; | ||
|
|
||
| foreach (var entry in zipArchive.Entries) | ||
| { | ||
| if (!entry.Name.Equals(originalName, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| originalExists = true; | ||
|
|
||
| var entryNameWithoutExtension = Path.GetFileNameWithoutExtension(entry.Name); | ||
| var entryExtension = Path.GetExtension(entry.Name); | ||
|
|
||
| // Only process entries with the same extension | ||
| if (!entryExtension.Equals(extension, StringComparison.OrdinalIgnoreCase)) | ||
| continue; | ||
|
|
||
| // Check if this entry follows our naming pattern | ||
| highestIndex = HighestEntryNameIndex(entryNameWithoutExtension, filenameWithoutExtension, highestIndex); | ||
| } | ||
|
|
||
| if (!originalExists) | ||
| { | ||
| return originalName; | ||
| } | ||
|
|
||
| return $"{filenameWithoutExtension}({highestIndex + 1}){extension}"; | ||
| } | ||
|
|
||
| private static int HighestEntryNameIndex(string entryNameWithoutExtension, string filenameWithoutExtension, | ||
| int highestIndex) | ||
| { | ||
| if (!entryNameWithoutExtension.StartsWith(filenameWithoutExtension, StringComparison.OrdinalIgnoreCase) || | ||
| entryNameWithoutExtension.Length <= filenameWithoutExtension.Length || | ||
| entryNameWithoutExtension[filenameWithoutExtension.Length] != '(') | ||
| { | ||
| return highestIndex; | ||
| } | ||
|
|
||
| // Extract the number between parentheses | ||
| var closingParenIndex = entryNameWithoutExtension.LastIndexOf(')'); | ||
| if (closingParenIndex <= filenameWithoutExtension.Length + 1) | ||
| { | ||
| return highestIndex; | ||
| } | ||
|
|
||
| var indexStr = entryNameWithoutExtension.Substring( | ||
| filenameWithoutExtension.Length + 1, | ||
| closingParenIndex - filenameWithoutExtension.Length - 1); | ||
|
|
||
| if (int.TryParse(indexStr, out var index)) | ||
| { | ||
| highestIndex = Math.Max(highestIndex, index); | ||
| } | ||
|
|
||
| return highestIndex; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,6 @@ | ||||||||||||||
| namespace Elsa.IO.Compression.Common; | ||||||||||||||
|
|
||||||||||||||
| public static class Constants | ||||||||||||||
| { | ||||||||||||||
|
||||||||||||||
| { | |
| { | |
| /// <summary> | |
| /// Represents the priority value for the zip entry strategy. | |
| /// This value determines the precedence of this strategy when handling zip entries. | |
| /// </summary> |
24 changes: 24 additions & 0 deletions
24
src/modules/io/Elsa.IO.Compression/Elsa.IO.Compression.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <Description> | ||
| Provides compression and archiving activities for Elsa Workflows. | ||
| </Description> | ||
| <PackageTags>elsa module compression zip archive workflows</PackageTags> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Http" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> | ||
| <PackageReference Include="Microsoft.Extensions.Options" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Elsa.IO\Elsa.IO.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' != 'true'"> | ||
| <PackageReference Include="Elsa.Workflows.Management" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
20 changes: 20 additions & 0 deletions
20
src/modules/io/Elsa.IO.Compression/Extensions/ModuleExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using Elsa.IO.Compression.Features; | ||
| using Elsa.Features.Services; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace Elsa.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Provides extensions to install the <see cref="CompressionFeature"/> feature. | ||
| /// </summary> | ||
| public static class ModuleExtensions | ||
| { | ||
| /// <summary> | ||
| /// Install the <see cref="CompressionFeature"/> feature. | ||
| /// </summary> | ||
| public static IModule UseCompression(this IModule module, Action<CompressionFeature>? configure = default) | ||
| { | ||
| module.Configure(configure); | ||
| return module; | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/modules/io/Elsa.IO.Compression/Features/CompressionFeature.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using Elsa.Extensions; | ||
| using Elsa.Features.Abstractions; | ||
| using Elsa.Features.Attributes; | ||
| using Elsa.Features.Services; | ||
| using Elsa.IO.Compression.Models; | ||
| using Elsa.IO.Compression.Services.Strategies; | ||
| using Elsa.IO.Features; | ||
| using Elsa.IO.Services.Strategies; | ||
| using JetBrains.Annotations; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Elsa.IO.Compression.Features; | ||
|
|
||
| /// <summary> | ||
| /// Configures compression activities and services. | ||
| /// </summary> | ||
| [UsedImplicitly] | ||
| [DependsOn(typeof(IOFeature))] | ||
| public class CompressionFeature(IModule module) : FeatureBase(module) | ||
| { | ||
| /// <inheritdoc /> | ||
| public override void Configure() | ||
| { | ||
| Module.AddActivitiesFrom<CompressionFeature>(); | ||
| Module.AddVariableTypeAndAlias<ZipEntry>("ZipEntry", "Compression"); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Apply() | ||
| { | ||
| Services.AddScoped<IContentResolverStrategy, ZipEntryContentStrategy>(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> | ||
| <ConfigureAwait /> | ||
| </Weavers> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace Elsa.IO.Compression.Models; | ||
|
|
||
| /// <summary> | ||
| /// Represents a zip entry with content and metadata. | ||
| /// </summary> | ||
| /// <param name="Content">The content of the zip entry. Can be byte[], Stream, file path, file URL, or base64 string.</param> | ||
| /// <param name="EntryName">The name of the entry in the zip archive.</param> | ||
| [UsedImplicitly] | ||
| public record ZipEntry(object Content, string? EntryName = null); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The public static class Constants is missing XML documentation. Consider adding a summary comment to describe the purpose of this constants class.