Skip to content

Instantly share code, notes, and snippets.

@duncanhoggan
Created April 28, 2020 09:37
Show Gist options
  • Save duncanhoggan/173a0aaf1e4c89ba18f84b00db192e75 to your computer and use it in GitHub Desktop.
Save duncanhoggan/173a0aaf1e4c89ba18f84b00db192e75 to your computer and use it in GitHub Desktop.
Disk Writer including Liquid eval
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Display;
using OrchardCore.Workflows.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Basic.Workflows.DiskWriter
{
public class DiskWriterTask : TaskActivity
{
readonly IOptions<ShellOptions> _shellOptions;
protected readonly IStringLocalizer S;
readonly ShellSettings _shellSettings;
private readonly IHttpContextAccessor _http;
private readonly IWorkflowExpressionEvaluator _expressionEvaluator;
public DiskWriterTask(
IWorkflowExpressionEvaluator expressionEvaluator,
IStringLocalizer<DiskWriterTask> localizer,
IOptions<ShellOptions> shellOptions,
ShellSettings shellSettings,
IHttpContextAccessor httpContextAccessor)
{
_shellOptions = shellOptions;
_shellSettings = shellSettings;
_http = httpContextAccessor;
_expressionEvaluator = expressionEvaluator;
public override string Name => nameof(DiskWriterTask);
public override LocalizedString DisplayText => S["Disk Writer Task"];
public override LocalizedString Category => S["UI"];
public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return Outcomes(S["Done"]);
}
public override bool CanExecute(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return _http.HttpContext?.Request?.Form != null;
}
public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
var shell = _shellOptions.Value;
var folder = String.Empty;
if (!String.IsNullOrWhiteSpace(Folder.Expression))
{
folder = await _expressionEvaluator.EvaluateAsync(Folder, workflowContext);
}
var innerPath = PathExtensions.Combine(shell.ShellsContainerName, _shellSettings.Name, folder);
var directory = PathExtensions.Combine(shell.ShellsApplicationDataPath, innerPath);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
foreach (var file in _http.HttpContext.Request.Form.Files)
{
var toSave = PathExtensions.Combine(directory, file.FileName);
using (var stream = System.IO.File.Create(toSave))
{
await file.CopyToAsync(stream);
}
var relative = PathExtensions.Combine(folder, file.FileName);
workflowContext.Properties[file.Name] = relative;
}
return Outcomes("Done");
}
public WorkflowExpression<string> Folder
{
get => GetProperty(() => new WorkflowExpression<string>(""));
set => SetProperty(value);
}
}
public class DiskWriterViewModel
{
public string FolderExpression { get; set; }
}
public class DiskWriterTaskDisplay : ActivityDisplayDriver<DiskWriterTask, DiskWriterViewModel>
{
protected override void EditActivity(DiskWriterTask activity, DiskWriterViewModel model)
{
model.FolderExpression = activity.Folder.Expression;
}
protected override void UpdateActivity(DiskWriterViewModel model, DiskWriterTask activity)
{
activity.Folder = new WorkflowExpression<string>(model.FolderExpression);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment