-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from bartverdonck/feature/attachfile
Feature/attachfile
- Loading branch information
Showing
24 changed files
with
266 additions
and
34 deletions.
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
24 changes: 18 additions & 6 deletions
24
src/Feature/FormsExtensions/code/Business/FileUpload/FileSystemFileUploadStorageProvider.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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Feature/FormsExtensions/code/Business/FileUpload/FolderBuilder.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,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; | ||
} | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
src/Feature/FormsExtensions/code/Business/FileUpload/IFileUploadStorageProvider.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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/Feature/FormsExtensions/code/Pipelines/SendEmail/AttachExternalFileProcessor.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,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)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.