diff --git a/src/Umbraco.Web/Editors/MediaController.cs b/src/Umbraco.Web/Editors/MediaController.cs index 3f0894c428..d807d84a26 100644 --- a/src/Umbraco.Web/Editors/MediaController.cs +++ b/src/Umbraco.Web/Editors/MediaController.cs @@ -331,7 +331,7 @@ namespace Umbraco.Web.Editors } } - [EnsureUserPermissionForMedia("folder.ParentId")] + [EnsureUserPermissionForMedia("folder.ParentId")] public MediaItemDisplay PostAddFolder(EntityBasic folder) { var mediaService = ApplicationContext.Services.MediaService; @@ -347,8 +347,8 @@ namespace Umbraco.Web.Editors /// /// /// We cannot validate this request with attributes (nicely) due to the nature of the multi-part for data. - /// /// + [FileUploadCleanupFilter(false)] public async Task PostAddFile() { if (Request.Content.IsMimeMultipartContent() == false) @@ -387,7 +387,7 @@ namespace Umbraco.Web.Editors return Request.CreateResponse(HttpStatusCode.Unauthorized); } - var tempFiles = new List(); + var tempFiles = new PostedFiles(); //get the files foreach (var file in result.FileData) @@ -416,19 +416,29 @@ namespace Umbraco.Web.Editors LogHelper.Warn("Cannot upload file " + file + ", it is not an approved file type"); } - tempFiles.Add(file.LocalFileName); + tempFiles.UploadedFiles.Add(new ContentItemFile + { + FileName = fileName, + PropertyAlias = Constants.Conventions.Media.File, + TempFilePath = file.LocalFileName + }); } - //now we can remove the temp files - foreach (var tempFile in tempFiles) - { - System.IO.File.Delete(tempFile); - } - - - return Request.CreateResponse(HttpStatusCode.OK); + return Request.CreateResponse(HttpStatusCode.OK, tempFiles); } + /// + /// This is used for the response of PostAddFile so that we can analyze the response in a filter and remove the + /// temporary files that were created. + /// + private class PostedFiles : IHaveUploadedFiles + { + public PostedFiles() + { + UploadedFiles = new List(); + } + public List UploadedFiles { get; private set; } + } /// /// Ensures the item can be moved/copied to the new location diff --git a/src/Umbraco.Web/WebApi/Filters/FileUploadCleanupFilterAttribute.cs b/src/Umbraco.Web/WebApi/Filters/FileUploadCleanupFilterAttribute.cs index 28f5a831f5..9a7b22ee73 100644 --- a/src/Umbraco.Web/WebApi/Filters/FileUploadCleanupFilterAttribute.cs +++ b/src/Umbraco.Web/WebApi/Filters/FileUploadCleanupFilterAttribute.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Net.Http; using System.Web.Http.Filters; using Umbraco.Core.Models; using Umbraco.Web.Models.ContentEditing; @@ -11,6 +12,17 @@ namespace Umbraco.Web.WebApi.Filters /// internal sealed class FileUploadCleanupFilterAttribute : ActionFilterAttribute { + private readonly bool _incomingModel; + + /// + /// Constructor specifies if the filter should analyze the incoming or outgoing model + /// + /// + public FileUploadCleanupFilterAttribute(bool incomingModel = true) + { + _incomingModel = incomingModel; + } + /// /// Returns true so that other filters can execute along with this one /// @@ -23,18 +35,40 @@ namespace Umbraco.Web.WebApi.Filters { base.OnActionExecuted(actionExecutedContext); - if (actionExecutedContext.ActionContext.ActionArguments.Any()) + if (_incomingModel) { - var contentItem = actionExecutedContext.ActionContext.ActionArguments.First().Value as IHaveUploadedFiles; - if (contentItem != null) + if (actionExecutedContext.ActionContext.ActionArguments.Any()) { - //cleanup any files associated - foreach (var f in contentItem.UploadedFiles) + var contentItem = actionExecutedContext.ActionContext.ActionArguments.First().Value as IHaveUploadedFiles; + if (contentItem != null) { - File.Delete(f.TempFilePath); + //cleanup any files associated + foreach (var f in contentItem.UploadedFiles) + { + File.Delete(f.TempFilePath); + } } } } + else + { + var objectContent = actionExecutedContext.Response.Content as ObjectContent; + if (objectContent != null) + { + var uploadedFiles = objectContent.Value as IHaveUploadedFiles; + if (uploadedFiles != null) + { + //cleanup any files associated + foreach (var f in uploadedFiles.UploadedFiles) + { + File.Delete(f.TempFilePath); + //clear out the temp path so it's not returned in the response + f.TempFilePath = ""; + } + } + } + } + } } } \ No newline at end of file