public string GetSafeFolderSegment(string newNameSegment)
 {
     return(_nameRules.GetCleanFolderName(newNameSegment));
 }
Esempio n. 2
0
        public async Task <OperationResult> CreateFolder(string requestedVirtualPath, string folderName)
        {
            OperationResult result;

            if (string.IsNullOrEmpty(folderName))
            {
                result         = new OperationResult(false);
                result.Message = _sr["Folder name not provided"];
                _log.LogWarning($"CreateFolder: Folder name not provided");
                return(result);
            }

            await EnsureProjectSettings().ConfigureAwait(false);

            if (string.IsNullOrEmpty(requestedVirtualPath))
            {
                requestedVirtualPath = _rootPath.RootVirtualPath;
            }

            var    isRoot = (requestedVirtualPath == _rootPath.RootVirtualPath);
            string requestedFsPath;

            if (!isRoot)
            {
                if (!requestedVirtualPath.StartsWith(_rootPath.RootVirtualPath))
                {
                    result         = new OperationResult(false);
                    result.Message = _sr["Invalid path"];
                    _log.LogWarning($"CreateFolder: {requestedVirtualPath} was not valid for root path {_rootPath.RootVirtualPath}");
                    return(result);
                }

                var virtualSubPath = requestedVirtualPath.Substring(_rootPath.RootVirtualPath.Length);
                var segments       = virtualSubPath.Split('/');
                if (segments.Length > 0)
                {
                    requestedFsPath = Path.Combine(_rootPath.RootFileSystemPath, Path.Combine(segments));
                    if (!Directory.Exists(requestedFsPath))
                    {
                        result         = new OperationResult(false);
                        result.Message = _sr["Invalid path"];
                        _log.LogWarning($"CreateFolder: {requestedVirtualPath} was not valid for root path {_rootPath.RootVirtualPath}");
                        return(result);
                    }
                }
                else
                {
                    requestedFsPath = _rootPath.RootFileSystemPath;
                }
            }
            else
            {
                requestedFsPath = _rootPath.RootFileSystemPath;
            }

            var newFolderFsPath = Path.Combine(requestedFsPath, _nameRules.GetCleanFolderName(folderName));

            if (Directory.Exists(newFolderFsPath))
            {
                result         = new OperationResult(false);
                result.Message = _sr["Folder already exists"];
                _log.LogWarning($"CreateFolder: {requestedVirtualPath} already exists");
                return(result);
            }

            try
            {
                Directory.CreateDirectory(newFolderFsPath);
                result = new OperationResult(true);
                return(result);
            }
            catch (IOException ex)
            {
                _log.LogError(MediaLoggingEvents.FOLDER_CREATION, ex, ex.Message + " " + ex.StackTrace);
                result         = new OperationResult(false);
                result.Message = _sr["Server error"];
                return(result);
            }
        }