예제 #1
0
        public MediaFolderInfo CreateFolder(string path)
        {
            Guard.NotEmpty(path, nameof(path));

            path = FolderService.NormalizePath(path, false);
            ValidateFolderPath(path, "CreateFolder", nameof(path));

            var dupe = _folderService.GetNodeByPath(path);

            if (dupe != null)
            {
                throw _exceptionFactory.DuplicateFolder(path, dupe.Value);
            }

            var  sep         = "/";
            var  folderNames = path.Split(new[] { sep }, StringSplitOptions.RemoveEmptyEntries);
            bool flag        = false;
            int  folderId    = 0;

            path = string.Empty;

            using (_folderService.BeginScope(true))
            {
                for (int i = 0; i < folderNames.Length; i++)
                {
                    var folderName = MediaHelper.NormalizeFolderName(folderNames[i]);
                    path += (i > 0 ? sep : string.Empty) + folderName;

                    if (!flag)
                    {
                        // Find the last existing node in path trail
                        var currentNode = _folderService.GetNodeByPath(path)?.Value;
                        if (currentNode != null)
                        {
                            folderId = currentNode.Id;
                        }
                        else
                        {
                            if (i == 0)
                            {
                                throw new NotSupportedException(T("Admin.Media.Exception.TopLevelAlbum", path));
                            }
                            flag = true;
                        }
                    }

                    if (flag)
                    {
                        // Create missing folders in trail
                        using (new DbContextScope(autoCommit: true))
                        {
                            var mediaFolder = new MediaFolder {
                                Name = folderName, ParentId = folderId
                            };
                            _folderService.InsertFolder(mediaFolder);
                            folderId = mediaFolder.Id;
                        }
                    }
                }
            }

            return(new MediaFolderInfo(_folderService.GetNodeById(folderId)));
        }