Fixes: U4-3254 Property level validation not working when no field level validators are used
U4-2683 Ensure all uploaded files are cleaned up when they change for the FileUploadValueEditor U4-3252 File uploader needs to adhere to DisallowedUploadFiles config U4-2774 FileUploadValueEditor needs to remove old files
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -12,6 +14,7 @@ using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.PropertyEditors;
|
||||
using Umbraco.Core.Services;
|
||||
using umbraco;
|
||||
|
||||
namespace Umbraco.Web.PropertyEditors
|
||||
{
|
||||
@@ -37,14 +40,9 @@ namespace Umbraco.Web.PropertyEditors
|
||||
/// <returns></returns>
|
||||
protected override PropertyValueEditor CreateValueEditor()
|
||||
{
|
||||
//TODO: Ensure we assign custom validation for uploaded file types!
|
||||
|
||||
var baseEditor = base.CreateValueEditor();
|
||||
|
||||
return new FileUploadPropertyValueEditor
|
||||
{
|
||||
View = baseEditor.View
|
||||
};
|
||||
var baseEditor = base.CreateValueEditor();
|
||||
baseEditor.Validators.Add(new UploadFileTypeValidator());
|
||||
return new FileUploadPropertyValueEditor(baseEditor);
|
||||
}
|
||||
|
||||
protected override PreValueEditor CreatePreValueEditor()
|
||||
@@ -179,4 +177,39 @@ namespace Umbraco.Web.PropertyEditors
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class UploadFileTypeValidator : IPropertyValidator
|
||||
{
|
||||
public IEnumerable<ValidationResult> Validate(object value, PreValueCollection preValues, PropertyEditor editor)
|
||||
{
|
||||
|
||||
//now check the file type
|
||||
var asJson = value as JObject;
|
||||
if (asJson == null) yield break;
|
||||
if (asJson["selectedFiles"] == null) yield break;
|
||||
var fileNames = asJson["selectedFiles"].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var fileName in fileNames)
|
||||
{
|
||||
if (ValidateFileExtension(fileName) == false)
|
||||
{
|
||||
yield return new ValidationResult(ui.Text("errors", "dissallowedMediaType"),
|
||||
new[]
|
||||
{
|
||||
//we only store a single value for this editor so the 'member' or 'field'
|
||||
// we'll associate this error with will simply be called 'value'
|
||||
"value"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal static bool ValidateFileExtension(string fileName)
|
||||
{
|
||||
if (fileName.IndexOf('.') <= 0) return false;
|
||||
var extension = Path.GetExtension(fileName).TrimStart(".");
|
||||
return UmbracoConfig.For.UmbracoSettings().Content.DisallowedUploadFiles.Any(x => x.InvariantEquals(extension)) == false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,11 @@ namespace Umbraco.Web.PropertyEditors
|
||||
/// <summary>
|
||||
/// The editor for the file upload property editor
|
||||
/// </summary>
|
||||
internal class FileUploadPropertyValueEditor : PropertyValueEditor
|
||||
internal class FileUploadPropertyValueEditor : PropertyValueEditorWrapper
|
||||
{
|
||||
public FileUploadPropertyValueEditor(PropertyValueEditor wrapped) : base(wrapped)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the deserialize value so that we can save the file accordingly
|
||||
@@ -68,9 +71,15 @@ namespace Umbraco.Web.PropertyEditors
|
||||
|
||||
var newValue = new List<string>();
|
||||
|
||||
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
||||
|
||||
if (clear)
|
||||
{
|
||||
//TODO: Need to delete our old files!
|
||||
//Remove any files that are saved for this item
|
||||
foreach (var toRemove in currentPersistedValues)
|
||||
{
|
||||
fs.DeleteFile(fs.GetRelativePath(toRemove), true);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -80,14 +89,20 @@ namespace Umbraco.Web.PropertyEditors
|
||||
var files = editorValue.AdditionalData["files"] as IEnumerable<ContentItemFile>;
|
||||
if (files != null)
|
||||
{
|
||||
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
||||
|
||||
//now we just need to move the files to where they should be
|
||||
var filesAsArray = files.ToArray();
|
||||
//a list of all of the newly saved files so we can compare with the current saved files and remove the old ones
|
||||
var savedFilePaths = new List<string>();
|
||||
for (var i = 0; i < filesAsArray.Length; i++)
|
||||
{
|
||||
var file = filesAsArray[i];
|
||||
|
||||
//don't continue if this is not allowed!
|
||||
if (UploadFileTypeValidator.ValidateFileExtension(file.FileName) == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//TODO: ALl of this naming logic needs to be put into the ImageHelper and then we need to change ContentExtensions to do the same!
|
||||
|
||||
var currentPersistedFile = currentPersistedValues.Length >= (i + 1)
|
||||
@@ -138,13 +153,19 @@ namespace Umbraco.Web.PropertyEditors
|
||||
|
||||
}
|
||||
newValue.Add(umbracoFile.Url);
|
||||
//add to the saved paths
|
||||
savedFilePaths.Add(umbracoFile.Url);
|
||||
}
|
||||
//now remove the temp file
|
||||
File.Delete(file.TempFilePath);
|
||||
File.Delete(file.TempFilePath);
|
||||
}
|
||||
|
||||
//Remove any files that are no longer saved for this item
|
||||
foreach (var toRemove in currentPersistedValues.Except(savedFilePaths))
|
||||
{
|
||||
fs.DeleteFile(fs.GetRelativePath(toRemove), true);
|
||||
}
|
||||
|
||||
//TODO: We need to remove any files that were previously persisted but are no longer persisted. FOr example, if we
|
||||
// uploaded 5 files before and then only uploaded 3, then the last two should be deleted.
|
||||
|
||||
return string.Join(",", newValue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user