コード例 #1
0
ファイル: PagesController.cs プロジェクト: radtek/documentor
 public IActionResult Move(PageMoveCommand moveCommand)
 {
     _pager.MovePage(moveCommand);
     return(Json(new JsonResponse(true))
            .Notify(NotificationType.Success, $"Page \"{moveCommand.VirtualPath.Substring(moveCommand.VirtualPath.LastIndexOf(Separator.Path) + 1)}\" moved"));
 }
コード例 #2
0
ファイル: Pager.cs プロジェクト: radtek/documentor
        public void MovePage(PageMoveCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            command.NewParentVirtualPath = command.NewParentVirtualPath?.Trim(Separator.Path);

            string basePath = _pageIOManager.GetPagesDirectory().FullName;

            Location location = GetLocation(command.VirtualPath);

            if (location == null)
            {
                throw new AppException("Page not found");
            }

            _cacheManager.ClearCache("*" + Cache.NavPostfix);

            string directoryName     = location.GetDestinationFolder().DirectoryName;
            string directoryPath     = Path.GetFullPath(Path.Combine(basePath, location.GetDirectoryPath()));
            int    oldSequenceNumber = int.Parse(directoryName.Split(Separator.Sequence)[0]);

            string newDirectoryName = (command.NewSequenceNumber < 10 ? "0" : "") + command.NewSequenceNumber.ToString() + Separator.Sequence + directoryName.Split(Separator.Sequence)[1];
            string newDirectoryPath = Path.GetFullPath(Path.Combine(basePath, newDirectoryName));

            if (command.NewParentVirtualPath != null)
            {
                if (!command.NewParentVirtualPath.Equals(""))
                {
                    Location parentLocation = GetLocation(command.NewParentVirtualPath);
                    if (parentLocation == null)
                    {
                        throw new AppException("Parent page not found");
                    }
                    newDirectoryPath = Path.GetFullPath(Path.Combine(basePath, parentLocation.GetDirectoryPath(), newDirectoryName));
                }
            }

            // Move directory
            if (!newDirectoryPath.Equals(directoryPath, StringComparison.OrdinalIgnoreCase))
            {
                Directory.Move(directoryPath, newDirectoryPath);
            }

            Regex regex = new Regex($@"^(0*)([1-9]+)\{Separator.Sequence}(.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            // Change sequence number
            // If not move to another parent directory
            if (command.NewParentVirtualPath == null)
            {
                new DirectoryInfo(newDirectoryPath).Parent.GetDirectories()
                .Where(x => regex.IsMatch(x.Name))
                .Where(x => !x.FullName.Equals(newDirectoryPath, StringComparison.OrdinalIgnoreCase))
                .ToList()
                .ForEach(x =>
                {
                    string[] sequenceDirectoryNameParts = x.Name.Split(Separator.Sequence);
                    if (int.TryParse(sequenceDirectoryNameParts[0], out int sequenceNumber) &&
                        ((command.NewSequenceNumber < oldSequenceNumber) && (sequenceNumber >= command.NewSequenceNumber && sequenceNumber < oldSequenceNumber)) ||
                        ((command.NewSequenceNumber > oldSequenceNumber) && (sequenceNumber <= command.NewSequenceNumber && sequenceNumber > oldSequenceNumber)))
                    {
                        sequenceNumber = sequenceNumber + (command.NewSequenceNumber < oldSequenceNumber ? 1 : -1);
                        string sequenceDirectoryName = (sequenceNumber < 10 ? "0" : "") + sequenceNumber.ToString() + Separator.Sequence + sequenceDirectoryNameParts[1];
                        Directory.Move(x.FullName, Path.Combine(x.Parent.FullName, sequenceDirectoryName));
                    }
                });
            }
            // Move to another parent directory
            else
            {
                // New parent
                new DirectoryInfo(newDirectoryPath).Parent.GetDirectories()
                .Where(x => regex.IsMatch(x.Name))
                .Where(x => !x.FullName.Equals(newDirectoryPath, StringComparison.OrdinalIgnoreCase))
                .ToList()
                .ForEach(x =>
                {
                    string[] sequenceDirectoryNameParts = x.Name.Split(Separator.Sequence);
                    if (int.TryParse(sequenceDirectoryNameParts[0], out int sequenceNumber) &&
                        sequenceNumber >= command.NewSequenceNumber)
                    {
                        sequenceNumber++;
                        string sequenceDirectoryName = (sequenceNumber < 10 ? "0" : "") + sequenceNumber.ToString() + Separator.Sequence + sequenceDirectoryNameParts[1];
                        string newPath = Path.Combine(x.Parent.FullName, sequenceDirectoryName);
                        directoryPath  = directoryPath.Replace(x.FullName, newPath);
                        Directory.Move(x.FullName, newPath);
                    }
                });

                // Old parent
                new DirectoryInfo(directoryPath).Parent.GetDirectories()
                .Where(x => regex.IsMatch(x.Name))
                .ToList()
                .ForEach(x =>
                {
                    string[] sequenceDirectoryNameParts = x.Name.Split(Separator.Sequence);
                    if (int.TryParse(sequenceDirectoryNameParts[0], out int sequenceNumber) &&
                        sequenceNumber > oldSequenceNumber)
                    {
                        sequenceNumber--;
                        string sequenceDirectoryName = (sequenceNumber < 10 ? "0" : "") + sequenceNumber.ToString() + Separator.Sequence + sequenceDirectoryNameParts[1];
                        Directory.Move(x.FullName, Path.Combine(x.Parent.FullName, sequenceDirectoryName));
                    }
                });
            }
        }