Esempio n. 1
0
        /// <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);
        }