2021-04-09 18:06:04 +00:00
|
|
|
using Elsa.ActivityResults;
|
|
|
|
|
using Elsa.Attributes;
|
2021-05-03 14:54:18 +00:00
|
|
|
using Elsa.Design;
|
|
|
|
|
using Elsa.Expressions;
|
2021-04-09 18:06:04 +00:00
|
|
|
using Elsa.Services;
|
|
|
|
|
using Elsa.Services.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Activities.File
|
|
|
|
|
{
|
|
|
|
|
[Action(Category = "File",
|
2021-04-09 22:15:06 +00:00
|
|
|
Description = "Output input value to specified location",
|
|
|
|
|
Outcomes = new[] { OutcomeNames.Done })]
|
2021-04-09 18:06:04 +00:00
|
|
|
public class OutFile : Activity
|
|
|
|
|
{
|
2021-05-03 14:54:18 +00:00
|
|
|
[ActivityProperty(Hint = "Content of the file.", UIHint = ActivityPropertyUIHints.MultiLine, SupportedSyntaxes = new[] { SyntaxNames.JavaScript, SyntaxNames.Liquid })]
|
|
|
|
|
public string Content { get; set; }
|
|
|
|
|
|
|
|
|
|
[ActivityProperty(Hint = "Path to create file at.", UIHint = ActivityPropertyUIHints.SingleLine, SupportedSyntaxes = new[] { SyntaxNames.JavaScript, SyntaxNames.Liquid })]
|
2021-04-09 18:06:04 +00:00
|
|
|
public string Path { get; set; }
|
|
|
|
|
|
2021-05-03 14:54:18 +00:00
|
|
|
[ActivityProperty(Hint = "How the output file should be written to.", UIHint = ActivityPropertyUIHints.Dropdown)]
|
2021-04-09 22:14:03 +00:00
|
|
|
public CopyMode Mode { get; set; } = CopyMode.CreateNew;
|
|
|
|
|
|
2021-04-09 18:06:04 +00:00
|
|
|
protected async override ValueTask<IActivityExecutionResult> OnExecuteAsync(ActivityExecutionContext context)
|
|
|
|
|
{
|
2021-05-03 14:54:18 +00:00
|
|
|
FileMode fileMode;
|
|
|
|
|
FileAccess fileAccess;
|
2021-05-02 23:06:41 +00:00
|
|
|
switch (Mode)
|
2021-04-09 22:14:03 +00:00
|
|
|
{
|
2021-05-02 23:06:41 +00:00
|
|
|
case CopyMode.Append:
|
2021-05-03 14:54:18 +00:00
|
|
|
fileMode = FileMode.Append;
|
|
|
|
|
fileAccess = FileAccess.Write;
|
2021-05-02 23:06:41 +00:00
|
|
|
break;
|
|
|
|
|
case CopyMode.Overwrite:
|
2021-05-03 14:54:18 +00:00
|
|
|
fileMode = FileMode.Create;
|
|
|
|
|
fileAccess = FileAccess.ReadWrite;
|
2021-05-02 23:06:41 +00:00
|
|
|
break;
|
|
|
|
|
case CopyMode.CreateNew:
|
2021-05-03 14:54:18 +00:00
|
|
|
fileMode = FileMode.CreateNew;
|
|
|
|
|
fileAccess = FileAccess.ReadWrite;
|
2021-05-02 23:06:41 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException("Unsupported copy mode");
|
2021-04-09 22:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-21 12:48:27 +00:00
|
|
|
using (var fs = new FileStream(Path, fileMode, fileAccess))
|
|
|
|
|
using (var sw = new StreamWriter(fs))
|
|
|
|
|
{
|
|
|
|
|
await sw.WriteLineAsync(Content);
|
|
|
|
|
await sw.FlushAsync();
|
|
|
|
|
await fs.FlushAsync();
|
|
|
|
|
}
|
2021-04-09 18:06:04 +00:00
|
|
|
|
2021-05-03 14:54:18 +00:00
|
|
|
return Done();
|
2021-04-09 18:06:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|