-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Form & Email attachments #12218
Closed
MikeKry
wants to merge
2
commits into
OrchardCMS:main
from
MikeKry:feature/form-file-upload-with-email
Closed
Form & Email attachments #12218
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
99 changes: 99 additions & 0 deletions
99
src/OrchardCore.Modules/OrchardCore.Workflows/Activities/SaveFormAttachmentsTask.cs
This file contains 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,99 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Localization; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Environment.Shell; | ||
using OrchardCore.FileStorage; | ||
using OrchardCore.FileStorage.FileSystem; | ||
using OrchardCore.Media; | ||
using OrchardCore.Workflows.Abstractions.Models; | ||
using OrchardCore.Workflows.Models; | ||
using OrchardCore.Workflows.Services; | ||
|
||
namespace OrchardCore.Workflows.Activities | ||
{ | ||
public class SaveFormAttachmentsTask : TaskActivity | ||
{ | ||
readonly IOptions<ShellOptions> _shellOptions; | ||
protected readonly IStringLocalizer S; | ||
readonly ShellSettings _shellSettings; | ||
private readonly IFileStore _fileStore; | ||
private readonly IHttpContextAccessor _http; | ||
private readonly IWorkflowExpressionEvaluator _expressionEvaluator; | ||
|
||
public SaveFormAttachmentsTask( | ||
IWorkflowExpressionEvaluator expressionEvaluator, | ||
IStringLocalizer<SaveFormAttachmentsTask> localizer, | ||
IOptions<ShellOptions> shellOptions, | ||
ShellSettings shellSettings, | ||
IMediaFileStore mediaFileStore, | ||
IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_shellOptions = shellOptions; | ||
_shellSettings = shellSettings; | ||
_fileStore = mediaFileStore; | ||
_http = httpContextAccessor; | ||
S = localizer; | ||
_expressionEvaluator = expressionEvaluator; | ||
} | ||
|
||
public override string Name => nameof(SaveFormAttachmentsTask); | ||
|
||
public override LocalizedString DisplayText => S["Save Form Attachments Task"]; | ||
|
||
public override LocalizedString Category => S["Media"]; | ||
|
||
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) | ||
{ | ||
// use either configured mediaFileStore or create new FileSystemStore --> TODO: mediaFileStore does not resolve permissions, everything is in public folders and can be accessible. | ||
// to put files in different folder, we would need either to register some private MediaFileStore, or allow to specify folder outside base path (?) | ||
var fileStore = UseMediaFileStore ? _fileStore : CreateDefaultFileStore(); | ||
var filePaths = new List<string>(); | ||
foreach (var file in _http.HttpContext.Request.Form.Files) | ||
{ | ||
var filePath = fileStore.Combine(Folder, $"{workflowContext.WorkflowId}-{file.FileName}"); | ||
filePaths.Add(await fileStore.CreateFileFromStreamAsync(filePath, file.OpenReadStream())); | ||
} | ||
|
||
workflowContext.Properties["FormAttachments"] = filePaths; | ||
workflowContext.Properties["FormAttachmentsUseMediaFileStore"] = UseMediaFileStore; | ||
return Outcomes("Done"); | ||
} | ||
|
||
public string Folder | ||
{ | ||
get => GetProperty<string>(() => string.Empty); | ||
set => SetProperty(value); | ||
} | ||
|
||
public bool UseMediaFileStore | ||
{ | ||
get => GetProperty<bool>(); | ||
set => SetProperty(value); | ||
} | ||
|
||
private IFileStore CreateDefaultFileStore() | ||
{ | ||
var shell = _shellOptions.Value; | ||
var innerPath = PathExtensions.Combine(shell.ShellsContainerName, _shellSettings.Name); | ||
var directory = PathExtensions.Combine(shell.ShellsApplicationDataPath, innerPath); | ||
|
||
// do not include Folder in FileSystemStore path, it is included in file path. | ||
var destPath = PathExtensions.Combine(directory, Folder); | ||
Directory.CreateDirectory(destPath); | ||
return new FileSystemStore(directory); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...OrchardCore.Modules/OrchardCore.Workflows/Drivers/SaveFormAttachmentsTaskDisplayDriver.cs
This file contains 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,22 @@ | ||
using OrchardCore.Workflows.Activities; | ||
using OrchardCore.Workflows.Display; | ||
using OrchardCore.Workflows.Models; | ||
using OrchardCore.Workflows.ViewModels; | ||
|
||
namespace OrchardCore.Workflows.Drivers | ||
{ | ||
public class SaveFormAttachmentsTaskDisplayDriver : ActivityDisplayDriver<SaveFormAttachmentsTask, SaveFormAttachmentsTaskViewModel> | ||
{ | ||
protected override void EditActivity(SaveFormAttachmentsTask activity, SaveFormAttachmentsTaskViewModel model) | ||
{ | ||
model.Folder = activity.Folder; | ||
model.UseMediaFileStore = activity.UseMediaFileStore; | ||
} | ||
|
||
protected override void UpdateActivity(SaveFormAttachmentsTaskViewModel model, SaveFormAttachmentsTask activity) | ||
{ | ||
activity.Folder = model.Folder; | ||
activity.UseMediaFileStore = model.UseMediaFileStore; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
13 changes: 13 additions & 0 deletions
13
src/OrchardCore.Modules/OrchardCore.Workflows/ViewModels/SaveFormAttachmentsTaskViewModel.cs
This file contains 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,13 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace OrchardCore.Workflows.ViewModels | ||
{ | ||
public class SaveFormAttachmentsTaskViewModel | ||
{ | ||
[Required] | ||
public string Folder { get; set; } | ||
|
||
[Required] | ||
public bool UseMediaFileStore { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...re.Modules/OrchardCore.Workflows/Views/Items/SaveFormAttachmentsTask.Fields.Design.cshtml
This file contains 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,9 @@ | ||
@model OrchardCore.Workflows.ViewModels.ActivityViewModel<SaveFormAttachmentsTask> | ||
|
||
|
||
<header> | ||
<h4><i class="fa fa-floppy-o"></i>@Model.Activity.GetTitleOrDefault(() => T["Save form attachments"])</h4> | ||
</header> | ||
|
||
|
||
@Model.Activity.Folder |
14 changes: 14 additions & 0 deletions
14
...Core.Modules/OrchardCore.Workflows/Views/Items/SaveFormAttachmentsTask.Fields.Edit.cshtml
This file contains 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,14 @@ | ||
@model OrchardCore.Workflows.ViewModels.SaveFormAttachmentsTaskViewModel | ||
|
||
<div class="form-group" asp-validation-class-for="Folder"> | ||
<label asp-for="Folder">@T["Folder name"]</label> | ||
<input type="text" asp-for="Folder" class="form-control code" /> | ||
<span asp-validation-for="Folder"></span> | ||
<span class="hint">@T["The folder name. In case of not using media file storage, folder is created under tenant folder."]</span> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<input type="checkbox" asp-for="UseMediaFileStore" /> | ||
<label asp-for="UseMediaFileStore">@T["Use Media File Store"]</label> | ||
<span class="hint">@T["If checked, configured media file storage is used. Beware, currently not secured, so your files would be visible."]</span> | ||
</div> |
2 changes: 2 additions & 0 deletions
2
...Modules/OrchardCore.Workflows/Views/Items/SaveFormAttachmentsTask.Fields.Thumbnail.cshtml
This file contains 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,2 @@ | ||
<h4 class="card-title"><i class="fa fa-floppy-o"></i>@T["Save form attachments"]</h4> | ||
<p>@T["Stores attachments from POST request into file system or predefined media file storage. Request has to be of multipart/form-data enctype. Attachments can be consumed by Send Email Task."]</p> |
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.
How does that work in the context that the form is not set to
enctype="multipart/form-data"
???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.
@Skrypt
It does not work without that - does not break but does nothing. I put it in description of task so everyone sees that it needs multipart formdata