From 1bfb63fe86aa3b53a15b0430bd1166f2578b119a Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 21 Mar 2017 18:35:13 +1100 Subject: [PATCH] Adds validation for file names --- .../Models/ContentEditing/CodeFileDisplay.cs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs index 178124f044..34ad507836 100644 --- a/src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs +++ b/src/Umbraco.Web/Models/ContentEditing/CodeFileDisplay.cs @@ -1,16 +1,23 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; +using Umbraco.Core; namespace Umbraco.Web.Models.ContentEditing { [DataContract(Name = "scriptFile", Namespace = "")] - public class CodeFileDisplay : INotificationModel + public class CodeFileDisplay : INotificationModel, IValidatableObject { + public CodeFileDisplay() + { + Notifications = new List(); + } + /// /// VirtualPath is the path to the file on disk /// /views/partials/file.cshtml @@ -47,5 +54,27 @@ namespace Umbraco.Web.Models.ContentEditing public string Id { get; set; } public List Notifications { get; private set; } + + /// + /// Some custom validation is required for valid file names + /// + /// + /// + public IEnumerable Validate(ValidationContext validationContext) + { + var illegalChars = System.IO.Path.GetInvalidFileNameChars(); + if (Name.ContainsAny(illegalChars)) + { + yield return new ValidationResult( + "The file name cannot contain illegal characters", + new[] { "Name" }); + } + else if (System.IO.Path.GetFileNameWithoutExtension(Name).IsNullOrWhiteSpace()) + { + yield return new ValidationResult( + "The file name cannot be empty", + new[] { "Name" }); + } + } } }