Exemplo n.º 1
0
        private static void ImportHandler(int deep, string parent, string path, bool overwrite, string destination, bool checkIndex = false)
        {
            foreach (DirectoryInfo folder in new DirectoryInfo(path).GetDirectories())
            {
                string dirName  = folder.Name;
                string infoPath = Path.Combine(path, dirName, dirName + ".info");

                if (File.Exists(infoPath))
                {
                    string[] nameSplit = dirName.Split('.');
                    string   oldId     = dirName.Split('.')[0];
                    string   newId;
                    if (overwrite || deep < 2)
                    {
                        newId = oldId;
                    }
                    else
                    {
                        newId = PathHelper.GenerateId(10);
                    }
                    if (nameSplit.Length < 2)
                    {
                        newId = PathHelper.GenerateId(10) + ".Folder" + random.Next(1000, 10000);
                    }

                    bool   copyInfo   = true;
                    string newDirName = dirName.Replace(oldId, newId);
                    if (!string.IsNullOrEmpty(destination))
                    {
                        string[] dest = destination.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        if (dest.Length > 0)
                        {
                            newDirName  = dest[0];
                            newId       = dest[0].Split('.')[0];
                            destination = destination.Replace(dest[0], "");
                            copyInfo    = false;
                        }
                    }

                    if (deep == 2)
                    {
                        tempIds.Add(new IdContainer(oldId, newId));
                    }

                    if (dirName != newDirName)
                    {
                        Directory.Move(Path.Combine(path, dirName), Path.Combine(path, newDirName));
                        infoPath = Path.Combine(path, newDirName, dirName + ".info");
                        if (copyInfo)
                        {
                            if (File.Exists(Path.Combine(path, newDirName, newDirName + ".info")))
                            {
                                File.Delete(Path.Combine(path, newDirName, newDirName + ".info"));
                            }
                            File.Move(infoPath, Path.Combine(path, newDirName, newDirName + ".info"));
                        }
                        else
                        {
                            File.Delete(infoPath);
                        }
                    }
                    ImportHandler(deep + 1, Path.Combine(parent, newDirName), folder.FullName.Replace(dirName, newDirName), overwrite, destination, checkIndex);
                }

                if (dirName == "topics")
                {
                    FileInfo[] folderFiles = folder.GetFiles();
                    int        filesCount  = 0;
                    if (checkIndex)
                    {
                        string repoPath    = Path.Combine(UsersHelper.GetUserFolder(), UsersHelper.GetToolsFolder()).Replace("\\", "/");
                        string newLocation = Path.Combine(repoPath, parent).Replace("\\", "/") + "/topics";

                        if (!Directory.Exists(newLocation))
                        {
                            Directory.CreateDirectory(newLocation);
                        }

                        filesCount = Directory.GetFiles(newLocation, "*", SearchOption.AllDirectories).Length;
                    }
                    int indexId = 0;
                    foreach (FileInfo t in folderFiles)
                    {
                        TopicModel    topic    = JsonConvert.DeserializeObject <TopicModel>(File.ReadAllText(t.FullName));
                        OldTopicModel oldTopic = JsonConvert.DeserializeObject <OldTopicModel>(File.ReadAllText(t.FullName));
                        if (!string.IsNullOrEmpty(oldTopic.pathToZip))
                        {
                            topic.localization = "default";
                            topic.pathToZip    = oldTopic.pathToZip;
                            //Debug.Log("Old import");
                        }
                        if (!string.IsNullOrEmpty(oldTopic.vmpPath))
                        {
                            topic.localization = "default";
                            topic.vmpPath      = oldTopic.vmpPath;
                            //Debug.Log("Old import");
                        }

                        if (string.IsNullOrEmpty(topic.unique_id))
                        {
                            topic.unique_id = PathHelper.GenerateId(6);
                        }

                        if (checkIndex)
                        {
                            topic.index = filesCount;
                        }

                        string oldTopicName = t.Name;
                        string newTopicName = "topic" + PathHelper.GenerateIntId(8) + ".info";

                        topic.localization = "default";
                        string locPath  = Path.Combine(Path.Combine(UsersHelper.GetUserFolder(), UsersHelper.GetToolsFolder()), parent, "default");
                        string tempPath = Path.Combine(t.Directory.Parent.FullName, "default");

                        if (!string.IsNullOrEmpty(topic.pathToZip))
                        {
                            string oldZipName = new FileInfo(topic.pathToZip).Name;
                            string newZipName = "zip" + PathHelper.GenerateIntId(8) + ".zip";
                            if (File.Exists(Path.Combine(tempPath, "pub_in", oldZipName)))
                            {
                                if (File.Exists(Path.Combine(tempPath, "pub_in", newZipName)))
                                {
                                    File.Delete(Path.Combine(tempPath, "pub_in", newZipName));
                                }
                                File.Move(Path.Combine(tempPath, "pub_in", oldZipName), Path.Combine(tempPath, "pub_in", newZipName));
                            }
                            topic.pathToZip        = Path.Combine(locPath, "pub_in", newZipName);
                            topic.pathToZip        = topic.pathToZip.Replace("\\", "/");
                            topic.pathToZipDEFAULT = Path.Combine(locPath, "pub_in", newZipName);
                            topic.pathToZipDEFAULT = topic.pathToZip.Replace("\\", "/");
                        }
                        if (!string.IsNullOrEmpty(topic.vmpPath))
                        {
                            string oldVmpName = new FileInfo(topic.vmpPath).Name;
                            string newVmpName = "obj" + PathHelper.GenerateIntId(8) + ".vmp";
                            if (File.Exists(Path.Combine(tempPath, "projects", oldVmpName)))
                            {
                                if (File.Exists(Path.Combine(tempPath, "projects", newVmpName)))
                                {
                                    File.Delete(Path.Combine(tempPath, "projects", newVmpName));
                                }
                                File.Move(Path.Combine(tempPath, "projects", oldVmpName), Path.Combine(tempPath, "projects", newVmpName));
                            }
                            if (File.Exists(Path.Combine(tempPath, "projects", oldVmpName.Replace(".vmp", ".vmb"))))
                            {
                                if (File.Exists(Path.Combine(tempPath, "projects", newVmpName.Replace(".vmp", ".vmb"))))
                                {
                                    File.Delete(Path.Combine(tempPath, "projects", newVmpName.Replace(".vmp", ".vmb")));
                                }
                                File.Move(Path.Combine(tempPath, "projects", oldVmpName.Replace(".vmp", ".vmb")), Path.Combine(tempPath, "projects", newVmpName.Replace(".vmp", ".vmb")));
                            }
                            topic.vmpPath        = Path.Combine(locPath, "projects", newVmpName);
                            topic.vmpPath        = topic.vmpPath.Replace("\\", "/");
                            topic.vmpPathDEFAULT = Path.Combine(locPath, "projects", newVmpName);
                            topic.vmpPathDEFAULT = topic.vmpPath.Replace("\\", "/");
                        }

                        topic.isOpened = 0;

                        //change path to zip and vmp
                        File.WriteAllText(t.FullName, JsonConvert.SerializeObject(topic));
                        //topic.index = folderFiles.Length;
                        if (File.Exists(t.FullName.Replace(oldTopicName, newTopicName)))
                        {
                            File.Delete(t.FullName.Replace(oldTopicName, newTopicName));
                        }
                        Thread.Sleep(100);
                        File.Move(t.FullName, t.FullName.Replace(oldTopicName, newTopicName));
                    }

                    if (!checkIndex)
                    {
                        List <TopicModel> topicModels = new List <TopicModel>();

                        foreach (FileInfo t in folder.GetFiles())
                        {
                            TopicModel topic = JsonConvert.DeserializeObject <TopicModel>(File.ReadAllText(t.FullName));
                            topic.infoPath = t.FullName;
                            topicModels.Add(topic);
                        }

                        topicModels = topicModels.OrderBy(t => t.index).ToList();
                        for (int i = 0; i < topicModels.Count; i++)
                        {
                            if (topicModels[i].index != i)
                            {
                                topicModels[i].index = i;
                                File.WriteAllText(topicModels[i].infoPath, JsonConvert.SerializeObject(topicModels[i]));
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void Export(string res, string file, ExportType exportType)
        {
            if (string.IsNullOrEmpty(res))
            {
                return;
            }

            string root = file.Replace(PathHelper.GetRepoPath(), "").Replace("\\", "/");

            string[] rootParts = root.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (!file.Contains(PathHelper.GetRepoPath()))
            {
                file = Path.Combine(PathHelper.GetRepoPath(), file);
            }

            TempFolderCreate();

            List <PathContainer> files = new List <PathContainer>();

            if (exportType == ExportType.Topic)
            {
                files = GetFilesTopic(file, Path.Combine(rootParts[0], rootParts[1], rootParts[2])).ToList();
                string categoryInfo = Path.Combine(rootParts[0], rootParts[0] + ".info");
                files.Add(new PathContainer(Path.Combine(PathHelper.GetRepoPath(), categoryInfo), categoryInfo));
                string productInfo = Path.Combine(rootParts[0], rootParts[1], rootParts[1] + ".info");
                files.Add(new PathContainer(Path.Combine(PathHelper.GetRepoPath(), productInfo), productInfo));
                string scenarioInfo = Path.Combine(rootParts[0], rootParts[1], rootParts[2], rootParts[2] + ".info");
                files.Add(new PathContainer(Path.Combine(PathHelper.GetRepoPath(), scenarioInfo), scenarioInfo));
            }
            else if (exportType == ExportType.Scenario)
            {
                files = GetFilesScenario(file, Path.Combine(rootParts[0], rootParts[1])).ToList();
                string categoryInfo = Path.Combine(rootParts[0], rootParts[0] + ".info");
                files.Add(new PathContainer(Path.Combine(PathHelper.GetRepoPath(), categoryInfo), categoryInfo));
                string productInfo = Path.Combine(rootParts[0], rootParts[1], rootParts[1] + ".info");
                files.Add(new PathContainer(Path.Combine(PathHelper.GetRepoPath(), productInfo), productInfo));
            }
            else if (exportType == ExportType.Product)
            {
                files = GetFilesProduct(file, rootParts[0]).ToList();
                string categoryInfo = Path.Combine(rootParts[0], rootParts[0] + ".info");
                files.Add(new PathContainer(Path.Combine(PathHelper.GetRepoPath(), categoryInfo), categoryInfo));
            }
            else if (exportType == ExportType.Category)
            {
                files = GetFilesCategory(file).ToList();
            }
            else if (exportType == ExportType.All)
            {
                files = GetFilesRepository().ToList();
            }

            foreach (PathContainer f in files)
            {
                string tempName = new FileInfo(f.relative).Name;
                string dirName  = f.relative.Replace(tempName, "");
                Directory.CreateDirectory(Path.Combine(GetTempFolderPath(), dirName));
                if (File.Exists(f.source))
                {
                    File.Copy(f.source, Path.Combine(GetTempFolderPath(), f.relative), true);
                }
            }

            string zipPath = res + ".zip";

            if (!Directory.Exists(Path.GetDirectoryName(zipPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(zipPath));
            }

            try
            {
                if (File.Exists(zipPath))
                {
                    File.Delete(zipPath);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error deleting existing folder export ZIP.", ex);
            }

            ZipFile.CreateFromDirectory(GetTempFolderPath(), zipPath);

            PathHelper.DeleteDirectory(GetTempFolderPath());
        }