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, IValidatableObject { public CodeFileDisplay() { Notifications = new List(); } /// /// VirtualPath is the path to the file on disk /// /views/partials/file.cshtml /// [DataMember(Name = "virtualPath", IsRequired = true)] public string VirtualPath { get; set; } /// /// Path represents the path used by the backoffice tree /// For files stored on disk, this is a urlencoded, comma separated /// path to the file, always starting with -1. /// /// -1,Partials,Parials%2FFolder,Partials%2FFolder%2FFile.cshtml /// [DataMember(Name = "path")] [ReadOnly(true)] public string Path { get; set; } [DataMember(Name = "name", IsRequired = true)] public string Name { get; set; } [DataMember(Name = "content", IsRequired = true)] public string Content { get; set; } [DataMember(Name = "fileType", IsRequired = true)] public string FileType { get; set; } [DataMember(Name = "snippet")] [ReadOnly(true)] public string Snippet { get; set; } [DataMember(Name = "id")] [ReadOnly(true)] 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" }); } } } }