Another attempt at fixing: U4-4049 Some images fail when uploading multiple media files
This commit is contained in:
@@ -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
|
||||
/// <returns></returns>
|
||||
/// <remarks>
|
||||
/// We cannot validate this request with attributes (nicely) due to the nature of the multi-part for data.
|
||||
///
|
||||
/// </remarks>
|
||||
[FileUploadCleanupFilter(false)]
|
||||
public async Task<HttpResponseMessage> PostAddFile()
|
||||
{
|
||||
if (Request.Content.IsMimeMultipartContent() == false)
|
||||
@@ -387,7 +387,7 @@ namespace Umbraco.Web.Editors
|
||||
return Request.CreateResponse(HttpStatusCode.Unauthorized);
|
||||
}
|
||||
|
||||
var tempFiles = new List<string>();
|
||||
var tempFiles = new PostedFiles();
|
||||
|
||||
//get the files
|
||||
foreach (var file in result.FileData)
|
||||
@@ -416,19 +416,29 @@ namespace Umbraco.Web.Editors
|
||||
LogHelper.Warn<MediaController>("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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
private class PostedFiles : IHaveUploadedFiles
|
||||
{
|
||||
public PostedFiles()
|
||||
{
|
||||
UploadedFiles = new List<ContentItemFile>();
|
||||
}
|
||||
public List<ContentItemFile> UploadedFiles { get; private set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures the item can be moved/copied to the new location
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
internal sealed class FileUploadCleanupFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
private readonly bool _incomingModel;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor specifies if the filter should analyze the incoming or outgoing model
|
||||
/// </summary>
|
||||
/// <param name="incomingModel"></param>
|
||||
public FileUploadCleanupFilterAttribute(bool incomingModel = true)
|
||||
{
|
||||
_incomingModel = incomingModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true so that other filters can execute along with this one
|
||||
/// </summary>
|
||||
@@ -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 = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user