Ejemplo n.º 1
0
        private static void RemoveUsersWithoutProjects()
        {
            FileInfo[] userDirs    = FileStore.GetFiles(Resources.UserDirectory);
            int        userTotal   = userDirs.Length;
            int        userCurrent = 0;

            Logger.Log("Checking  " + userDirs.Length + " users if they have projects.");

            foreach (FileInfo userDir in userDirs)
            {
                string username = Path.GetFileNameWithoutExtension(userDir.Name);

                userCurrent++;
                Logger.Log(LoggerHelper.FormatProgress(
                               "Checking if user " + LoggerHelper.ForceLength(username, 10) + " has projects",
                               userCurrent, userTotal));

                string userProjects = Resources.ProjectDirectory + "/" + username;
                if (!FileStore.DirectoryExists(userProjects) || FileStore.GetFiles(userProjects).Length == 0)
                {
                    FileStore.RemoveFile(Resources.UserDirectory, username + ".json");
                    FileStore.RemoveFile(Resources.ProjectDirectory, username + ".json");
                    FileStore.RemoveDirectory(Resources.ProjectDirectory + "/" + username);
                }
            }
        }
Ejemplo n.º 2
0
        private static void RemoveUnchangedProjectsFromLists()
        {
            DirectoryInfo[] userDirs    = FileStore.GetDirectories(Resources.ProjectDirectory);
            int             userTotal   = userDirs.Length;
            int             userCurrent = 0;

            Logger.Log("Updating project lists of " + userDirs.Length + " users.");

            foreach (DirectoryInfo userDir in userDirs)
            {
                string username = userDir.Name;

                userCurrent++;
                Logger.Log(LoggerHelper.FormatProgress(
                               "Updating project lists of " + LoggerHelper.ForceLength(username, 10),
                               userCurrent, userTotal));

                FileInfo[] projectLists = userDir.GetFiles().OrderBy(projectList => projectList.Name).ToArray();

                foreach (FileInfo projectList in projectLists)
                {
                    JArray projects;
                    try
                    {
                        projects = JArray.Parse(File.ReadAllText(projectList.FullName));
                    }
                    catch (JsonReaderException e)
                    {
                        Logger.Log("The project metadata list of user `" + userDir.Name + "` could not be parsed.", e);
                        return;
                    }

                    JArray filteredProjects = new JArray();
                    foreach (JToken project in projects)
                    {
                        if (!(project is JObject))
                        {
                            Logger.Log("The metadata of a project of user `" + userDir.Name + "` could not be parsed.");
                            return;
                        }

                        JObject metadata  = (JObject)project;
                        int     projectId = int.Parse(metadata["id"].ToString());

                        if (FileStore.DirectoryExists(Resources.CodeDirectory + "/" + projectId))
                        {
                            filteredProjects.Add(project);
                        }
                    }


                    if (filteredProjects.Count == 0)
                    {
                        File.Delete(projectList.FullName);
                    }
                    else
                    {
                        File.WriteAllText(projectList.FullName, filteredProjects.ToString(Formatting.None));
                    }
                }
            }
        }
Ejemplo n.º 3
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);
        }