Skip to content

Commit

Permalink
Merge pull request #16 from bartverdonck/feature/attachfile
Browse files Browse the repository at this point in the history
Feature/attachfile
  • Loading branch information
bartverdonck committed Oct 4, 2018
2 parents 741b7b0 + aca7b31 commit 829d35b
Show file tree
Hide file tree
Showing 24 changed files with 266 additions and 34 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ The new Sitecore Forms module that came with Sitecore 9 is a very promissing too
This module aims to add some functionality to this forms creator.

## What
### 1.6
- *Attachments*: The send email submit action has been extended to allow file uploads to be added to the email as a file attachment.
- *Folder*: The file upload control's storageproviders have been updated to support (dynamic) subfolders. Example: <folder>formuploads/{formName}/{fieldName}/{language}</folder>
- The download package is now also available as scwp for Azure PaaS ARM.

### 1.5
- *ShowFormPage*, custom submit action: With this submit action you set the page in your form that needs to be displayed after a succesfull submit. This is usefull when you don't want to redirect to a seperate thank you page but replace the form with a thank you message after submit. [More info can be found here](http://onelittlespark.bartverdonck.be/inline-thank-you-message-on-sitecore-forms/)
- *RawHTML*, custom field: The content entered in the Raw HTML field is rendered on the page as pure html without escaping. This can be usefull to add small inline javascript snippets or other custom html. In combination with the ShowFormPage, this can be used to trigger a datalayer event to track that the form was submitted.
Expand Down
Binary file not shown.
Binary file not shown.
Binary file added downloads/Sitecore Forms Extensions-1.6.zip
Binary file not shown.
4 changes: 3 additions & 1 deletion sitecorepackagedefinition/Sitecore Forms Extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<PackageName>Sitecore Forms Extensions</PackageName>
<Author>Bart Verdonck</Author>
<Version>1.5</Version>
<Version>1.6</Version>
<Revision />
<License />
<Comment />
Expand Down Expand Up @@ -83,6 +83,8 @@
<x-item>/core/sitecore/client/Applications/FormsBuilder/Components/Layouts/Actions/SendEmailExtended/PageSettings/ValueNotInListText/{9260EB36-1773-4801-9C67-D935402A0F9D}/invariant/0</x-item>
<x-item>/master/sitecore/system/Settings/Forms/Submit Actions/Send Email/{72CCAB13-E73F-4C75-8742-F2FFDF141E31}/invariant/0</x-item>
<x-item>/web/sitecore/system/Settings/Forms/Submit Actions/Send Email/{72CCAB13-E73F-4C75-8742-F2FFDF141E31}/invariant/0</x-item>
<x-item>/core/sitecore/client/Applications/FormsBuilder/Components/Layouts/Actions/SendEmailExtended/PageSettings/SettingsForm/Attachments/{E1FE3EFD-D97A-43C6-9FBD-10C19A2A063B}/invariant/0</x-item>
<x-item>/core/sitecore/client/Applications/FormsBuilder/Components/Layouts/Actions/SendEmailExtended/PageSettings/SettingsForm/Attachments/FileUploadFieldsToAttach/{D874D897-0BDB-4967-ADDA-E0A53AAC6CAA}/invariant/0</x-item>
</Entries>
<SkipVersions>False</SkipVersions>
<Converter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<fileUploadStorageProvider type="Feature.FormsExtensions.Business.FileUpload.AzureBlobStorageFileUploadStorageProvider, Feature.FormsExtensions">
<connectionString></connectionString>
<blobContainer></blobContainer>
<folder></folder>
</fileUploadStorageProvider>
-->
</formExtensions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<SendEmail role:require="Standalone or ContentManagement or DedicatedDispatch">
<processor patch:before="*[@type='Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.FillEmail, Sitecore.EmailCampaign.Cm']"
type="Feature.FormsExtensions.Pipelines.SendEmail.FormDataProcessor, Feature.FormsExtensions" />
<processor patch:before="*[@type='Sitecore.EmailCampaign.Cm.Pipelines.SendEmail.SendEmail, Sitecore.EmailCampaign.Cm']"
type="Feature.FormsExtensions.Pipelines.SendEmail.AttachExternalFileProcessor, Feature.FormsExtensions" resolve="true" />
</SendEmail>

<forms.renderForm>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Web;
using Feature.FormsExtensions.Fields.FileUpload;
using Microsoft.WindowsAzure.Storage;

namespace Feature.FormsExtensions.Business.FileUpload
Expand All @@ -9,22 +10,27 @@ public class AzureBlobStorageFileUploadStorageProvider : IFileUploadStorageProvi
{
public string ConnectionString { get; set; }
public string BlobContainer { get; set; }
public string Folder { get; set; }

public IStoredFile StoreFile(HttpPostedFileBase fileBase)
public IStoredFile StoreFile(FileUploadModel fileUploadModel, Guid formId)
{
var fileBase = fileUploadModel.File;
var filePath = FolderBuilder.BuildFolder(Folder,fileUploadModel, formId);
var fileName = $"{Guid.NewGuid().ToString()}{Path.GetExtension(fileBase.FileName)}";
var file = StoreFile(fileBase, fileName);
var file = UploadFile(fileBase, Path.Combine(filePath,fileName));
var storedFile = new StoredFile
{
Url = file.ToString(),
OriginalFileName = fileBase.FileName,
ContentType = fileBase.ContentType,
ContentLength = fileBase.ContentLength
ContentLength = fileBase.ContentLength,
StoredFileName = fileName,
StoredFilePath = filePath
};
return storedFile;
}

private Uri StoreFile(HttpPostedFileBase fileBase, string fileName)
private Uri UploadFile(HttpPostedFileBase fileBase, string fileName)
{
var storageAccount = CloudStorageAccount.Parse(ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
Expand All @@ -33,5 +39,22 @@ private Uri StoreFile(HttpPostedFileBase fileBase, string fileName)
blockBlob.UploadFromStream(fileBase.InputStream);
return blockBlob.Uri;
}

public byte[] GetFileAsBytes(IStoredFile storedFile)
{
var storageAccount = CloudStorageAccount.Parse(ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference(BlobContainer);
var blockBlob = blobContainer.GetBlockBlobReference(Path.Combine(storedFile.StoredFilePath,storedFile.StoredFileName));
blockBlob.FetchAttributes();
var fileByteLength = blockBlob.Properties.Length;
var fileContent = new byte[fileByteLength];
for (var i = 0; i < fileByteLength; i++)
{
fileContent[i] = 0x20;
}
blockBlob.DownloadToByteArray(fileContent, 0);
return fileContent;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
using System;
using System.IO;
using System.Web;
using Feature.FormsExtensions.Fields.FileUpload;

namespace Feature.FormsExtensions.Business.FileUpload
{
public class FileSystemFileUploadStorageProvider : IFileUploadStorageProvider
{
public string RootStoragePath { get; set; }
public string FileDownloadUrlBase { get; set; }

public IStoredFile StoreFile(HttpPostedFileBase fileBase)
public string Folder { get; set; }

public IStoredFile StoreFile(FileUploadModel fileUploadModel, Guid formId)
{
var folder = FolderBuilder.BuildFolder(Folder, fileUploadModel, formId);
Directory.CreateDirectory(Path.Combine(RootStoragePath, folder));
var fileBase = fileUploadModel.File;
var fileName = $"{Guid.NewGuid().ToString()}{Path.GetExtension(fileBase.FileName)}";
var path = Path.Combine(RootStoragePath, fileName);
var path = Path.Combine(RootStoragePath, folder, fileName);
fileBase.SaveAs(path);
var storedFile = new StoredFile
{
Url = string.Format(FileDownloadUrlBase,fileName),
Url = string.Format(FileDownloadUrlBase, fileName),
OriginalFileName = fileBase.FileName,
ContentType = fileBase.ContentType,
ContentLength = fileBase.ContentLength
ContentLength = fileBase.ContentLength,
StoredFilePath = folder,
StoredFileName = fileName
};
return storedFile;
}

public byte[] GetFileAsBytes(IStoredFile storedFile)
{
var path = Path.Combine(RootStoragePath, storedFile.StoredFilePath, storedFile.StoredFileName);
return File.ReadAllBytes(path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Feature.FormsExtensions.Fields.FileUpload;
using Sitecore.Data;

namespace Feature.FormsExtensions.Business.FileUpload
{
public class FolderBuilder
{
public static string BuildFolder(string rawFolder, FileUploadModel fileUploadModel, Guid formId)
{
if (rawFolder == null)
return "";
var folder = rawFolder;
if (folder.Contains("{formName}"))
{
var formItem = Sitecore.Context.Database.GetItem(new ID(formId));
folder = folder.Replace("{formName}", formItem.Name);
}
if (folder.Contains("{fieldName}"))
{
folder = folder.Replace("{fieldName}", fileUploadModel.Name);
}
if (folder.Contains("{language}"))
{
folder = folder.Replace("{language}", Sitecore.Context.Language.Name);
}
return folder;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Web;
using System;
using System.Web;
using Feature.FormsExtensions.Fields.FileUpload;

namespace Feature.FormsExtensions.Business.FileUpload
{
public interface IFileUploadStorageProvider
{
IStoredFile StoreFile(HttpPostedFileBase fileBase);
IStoredFile StoreFile(FileUploadModel fileUploadModel, Guid formId);
byte[] GetFileAsBytes(IStoredFile storedFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ public interface IStoredFile
{
string Url { get; set; }
string OriginalFileName { get; set; }
string StoredFileName { get; set; }
string ContentType { get; set; }
int ContentLength { get; set; }
string StoredFilePath { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class StoredFile : IStoredFile
public string OriginalFileName { get; set; }
public string ContentType { get; set; }
public int ContentLength { get; set; }
public string StoredFileName { get; set; }
public string StoredFilePath { get; set; }

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<Reference Include="Sitecore.Analytics.XConnect, Version=11.18.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\..\packages\Sitecore.Analytics.XConnect.NoReferences.9.0.171219\lib\NET462\Sitecore.Analytics.XConnect.dll</HintPath>
</Reference>
<Reference Include="Sitecore.EDS.Core, Version=6.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\..\packages\Sitecore.EDS.Core.NoReferences.9.0.171219\lib\NET462\Sitecore.EDS.Core.dll</HintPath>
</Reference>
<Reference Include="Sitecore.EmailCampaign, Version=6.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\..\packages\Sitecore.EmailCampaign.NoReferences.9.0.171219\lib\NET462\Sitecore.EmailCampaign.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -197,6 +200,7 @@
<Compile Include="Business\FieldBindings\FieldBindingTokenKeyAlpabetSorter.cs" />
<Compile Include="Business\FileUpload\AzureBlobStorageFileUploadStorageProvider.cs" />
<Compile Include="Business\FileUpload\FileUploadStorageProviderFactory.cs" />
<Compile Include="Business\FileUpload\FolderBuilder.cs" />
<Compile Include="Business\FormField.cs" />
<Compile Include="Business\FormFieldConverter.cs" />
<Compile Include="Business\FormFieldValue.cs" />
Expand Down Expand Up @@ -257,6 +261,7 @@
<Compile Include="Business\FieldBindings\xDbBindingHandlers\ContactPersonalInfo\XDbFirstNameBindingHandler.cs" />
<Compile Include="Pipelines\LoadFieldBindingHandlers\XDbFieldBindingHandlerLoader.cs" />
<Compile Include="Pipelines\RenderField\ReplaceFieldBindingToken.cs" />
<Compile Include="Pipelines\SendEmail\AttachExternalFileProcessor.cs" />
<Compile Include="Pipelines\SendEmail\FormDataProcessor.cs" />
<Compile Include="Business\FileUpload\FileSystemFileUploadStorageProvider.cs" />
<Compile Include="Fields\FileUpload\FileUploadModel.cs" />
Expand All @@ -275,6 +280,7 @@
<Compile Include="Pipelines\ShowFormPage\ShowFormPageNavigationLoader.cs" />
<Compile Include="SubmitActions\IdentifyContact\IdentifyContactAction.cs" />
<Compile Include="SubmitActions\IdentifyContact\IdentifyContactData.cs" />
<Compile Include="SubmitActions\SendEmail\FileAttachmentTokenBuilder.cs" />
<Compile Include="SubmitActions\SendEmail\SendEmailBase.cs" />
<Compile Include="SubmitActions\SendEmail\CurrentContactContactIdentierHandler.cs" />
<Compile Include="SubmitActions\SendEmail\FieldValueContactIdentierHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void Configure(IServiceCollection serviceCollection)
serviceCollection.AddSingleton<FieldValueContactIdentierHandler, FieldValueContactIdentierHandler>();
serviceCollection.AddSingleton<FixedAddressContactIdentierHandler, FixedAddressContactIdentierHandler>();
serviceCollection.AddSingleton<IFieldBindingMapFactory, FieldBindingMapFactory>();
serviceCollection.AddSingleton<FileAttachmentTokenBuilder, FileAttachmentTokenBuilder>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Feature.FormsExtensions.Business.FileUpload;
using System;
using Feature.FormsExtensions.Business.FileUpload;
using Feature.FormsExtensions.Fields.FileUpload;
using Sitecore.ExperienceForms.Mvc.Pipelines.ExecuteSubmit;
using Sitecore.Mvc.Pipelines;
Expand All @@ -21,19 +22,19 @@ public override void Process(ExecuteSubmitActionsEventArgs args)
var uploadField = field as FileUploadModel;
if (uploadField == null)
continue;
HandleUploadField(uploadField);
HandleUploadField(uploadField,args.FormSubmitContext.FormId);
}
}

private void HandleUploadField(FileUploadModel uploadField)
private void HandleUploadField(FileUploadModel uploadField, Guid formId)
{
if (uploadField.File == null)
return;

if (uploadField.File.InputStream.Position > 0)
return;

var storedFile = fileUploadStorageProviderFactory.GetDefaultFileUploadStorageProvider().StoreFile(uploadField.File);
var storedFile = fileUploadStorageProviderFactory.GetDefaultFileUploadStorageProvider().StoreFile(uploadField, formId);
uploadField.Value = storedFile;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Linq;
using Feature.FormsExtensions.Business.FileUpload;
using Newtonsoft.Json;
using Sitecore.EDS.Core.Dispatch;
using Sitecore.EmailCampaign.Cm.Pipelines.SendEmail;
using Sitecore.Modules.EmailCampaign.Messages;

namespace Feature.FormsExtensions.Pipelines.SendEmail
{
public class AttachExternalFileProcessor
{

private readonly IFileUploadStorageProviderFactory fileUploadStorageProviderFactory;

public AttachExternalFileProcessor(IFileUploadStorageProviderFactory fileUploadStorageProviderFactory)
{
this.fileUploadStorageProviderFactory = fileUploadStorageProviderFactory;
}

public void Process(SendMessageArgs args)
{
if(!(args.EcmMessage is MessageItem ecmmessage))
return;
if (!(args.CustomData["EmailMessage"] is EmailMessage message))
{
args.AddMessage("Missing EmailMessage from arguments.");
return;
}
foreach (var attachmentReference in ecmmessage.CustomPersonTokens.Keys.Where(k =>
k.StartsWith("attachment_")))
{
var storedFileJson = ecmmessage.CustomPersonTokens[attachmentReference].ToString();
var storedFile = JsonConvert.DeserializeObject<StoredFile>(storedFileJson);
var fileUploadStorageProvider = fileUploadStorageProviderFactory.GetDefaultFileUploadStorageProvider();
var fileContent = fileUploadStorageProvider.GetFileAsBytes(storedFile);
message.Attachments.Add(new FileResource(storedFile.OriginalFileName, fileContent));
}
}
}
}

0 comments on commit 829d35b

Please sign in to comment.