/// <summary> /// Gets the controller for the given content model. /// </summary> /// <param name="model">The model.</param> /// <param name="defaultControllerName">Default name of the controller.</param> /// <returns></returns> public static Type GetController(this IContent model, string defaultControllerName = "") { var contentAttr = model.GetType().GetCustomAttribute <ContentAttribute>(); var controllerName = (contentAttr != null && contentAttr.ControllerType != null) ? contentAttr.ControllerType.Name : string.Format("{0}Controller", model.TypeName); var controllers = TypeFinder.FindTypes <IController>().ToList(); var controller = controllers.SingleOrDefault(x => x.Name == controllerName); if (controller != null) { return(controller); } return(!string.IsNullOrEmpty(defaultControllerName) ? controllers.SingleOrDefault(x => x.Name == defaultControllerName) : null); }
/// <summary> /// Initializes a new instance of the <see cref="KarbonTextParser"/> class. /// </summary> public KarbonTextParser() { _tags = TypeFinder.FindTypes <AbstractKarbonTextTag>() .Where(x => x.GetCustomAttribute <KarbonTextTagAttribute>() != null) .ToDictionary(x => x.GetCustomAttribute <KarbonTextTagAttribute>().Name, x => x); }
/// <summary> /// Gets a content item by relative file path. /// </summary> /// <param name="path">The path.</param> /// <returns></returns> private IContent GetByPath(string path) { if (path == null) { return(null); } // Check directory exists if (!_fileStore.DirectoryExists(path)) { return(null); } // Parse directory name var directoryNameInfo = ParseDirectoryName(_fileStore.GetName(path)); // Grab a files var filePaths = _fileStore.GetFiles(path).ToList(); // Find the content file var contentFilePath = filePaths .FirstOrDefault(x => x.Count(y => y == '.') == 1 && x.EndsWith("." + _dataSerializer.FileExtension)); // Create model object based on file name var fileName = contentFilePath != null ? _fileStore.GetNameWithoutExtension(contentFilePath) : "Content"; var type = TypeFinder.FindTypes <Content>() .SingleOrDefault(x => x.Name == fileName) ?? typeof(Content); var model = Activator.CreateInstance(type) as IContent; if (model == null) { return(null); } // Deserialize data var data = contentFilePath != null ? _dataSerializer.Deserialize(_fileStore.OpenFile(contentFilePath)) : new Dictionary <string, string>(); // Map data to model model.RelativePath = path; model.TypeName = fileName; model.Slug = directoryNameInfo.Name; model.Name = GetNameFromSlug(directoryNameInfo.Name); model.RelativeUrl = GetUrlFromPath(path); model.SortOrder = directoryNameInfo.SortOrder; model.Created = _fileStore.GetCreated(contentFilePath ?? path); model.Modified = _fileStore.GetLastModified(contentFilePath ?? path); model.Depth = model.RelativeUrl == "~/" ? 1 : model.RelativeUrl.Count(x => x == '/') + 1; //model.Data = data; model = (IContent)_dataMapper.Map(model.GetType(), model, data); // Parse files model.AllFiles = LoadFiles(filePaths.Where(x => x != contentFilePath), model.RelativeUrl); // Return model return(model); }
/// <summary> /// Loads the files from the file paths list provided. /// </summary> /// <param name="filePaths">The file paths.</param> /// <param name="contentUrl">The content relative URL.</param> /// <returns></returns> private IEnumerable <IFile> LoadFiles(IEnumerable <string> filePaths, string contentUrl) { var files = new List <IFile>(); var noneContentFilePaths = filePaths.Where(x => !x.EndsWith("." + _dataSerializer.FileExtension)); foreach (var noneContentFilePath in noneContentFilePaths) { // Parse file nae info var fileNameInfo = ParseFileName(_fileStore.GetName(noneContentFilePath)); // See if there is a meta data file var contentFilePath = filePaths.SingleOrDefault(x => x == noneContentFilePath + "." + _dataSerializer.FileExtension); // Find type for the file var type = TypeFinder.FindTypes <File>() .SingleOrDefault(x => x.Name == fileNameInfo.TypeName) ?? typeof(File); // Create the file var model = Activator.CreateInstance(type) as IFile; if (model == null) { continue; } // Map data to the file model.RelativePath = noneContentFilePath; model.TypeName = fileNameInfo.TypeName; model.Slug = fileNameInfo.Name; model.Name = GetNameFromSlug(_fileStore.GetNameWithoutExtension(fileNameInfo.Name)); model.RelativeUrl = "~/media/" + contentUrl.TrimStart("~/") + "/" + model.Slug; model.ContentRelativeUrl = contentUrl; model.SortOrder = fileNameInfo.SortOrder; model.Extension = fileNameInfo.Extension; model.Created = _fileStore.GetCreated(noneContentFilePath); model.Modified = _fileStore.GetLastModified(noneContentFilePath); model.Size = _fileStore.GetSize(noneContentFilePath); var data = contentFilePath != null ? _dataSerializer.Deserialize(_fileStore.OpenFile(contentFilePath)) : new Dictionary <string, string>(); // TODO: Make this bit provider driven so people can retreive their own data from a file if (model.IsImage()) { // Parse width height var imageSize = GetImageSize(noneContentFilePath); if (imageSize != Size.Empty) { data.Add("Width", imageSize.Width.ToString()); data.Add("Height", imageSize.Height.ToString()); } } //model.Data = data; model = (IFile)_dataMapper.Map(model.GetType(), model, data); // Return the file files.Add(model); } return(files.OrderBy(x => x.SortOrder).ThenBy(x => x.Slug)); }
/// <summary> /// Initializes a new instance of the <see cref="EmbedProviderFactory"/> class. /// </summary> private EmbedProviderFactory() { _providers = TypeFinder.FindTypes <AbstractEmbedProvider>() .Where(x => x.GetCustomAttribute <EmbedProviderAttribute>() != null) .ToDictionary(x => x.GetCustomAttribute <EmbedProviderAttribute>().UrlSchemeRegex, x => x); }