Пример #1
0
 private void ValidateMaxUploadFileSize(long size)
 {
     if (MaxUploadFileSize > 0 && size > MaxUploadFileSize)
     {
         FileManagementExceptionExecutor.ThrowMaxFileSizeExceeded();
     }
 }
Пример #2
0
        private static string PreparePath(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(string.Empty);
            }
            List <string> list = path.Split(PossibleDirectorySeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
            int           num  = 0;

            while (num < list.Count)
            {
                if (list[num] == ".." && num > 0)
                {
                    list.RemoveAt(num);
                    list.RemoveAt(num - 1);
                    num--;
                }
                else
                {
                    num++;
                }
            }
            if (list.Any() && list[0] == "..")
            {
                FileManagementExceptionExecutor.ThrowNoAccess();
            }
            return(Path.Combine(list.ToArray()));
        }
Пример #3
0
 private void ValidateFileExtension(string name)
 {
     if (!IsValidFileExtension(name))
     {
         FileManagementExceptionExecutor.ThrowWrongFileExtension();
     }
 }
Пример #4
0
        public void Rename(string key, string newName)
        {
            CheckThatDirOrFileNameIsNotEmpty(key, "key");
            CheckThatDirOrFileNameIsNotEmpty(newName, "newName");
            ValidateFileItemNameSymbols(newName);
            string text = Path.Combine(RootDirectoryPath, PreparePath(key));
            string path = Path.GetDirectoryName(text) ?? string.Empty;

            if (FileSystemService.DirectoryExists(text))
            {
                if (string.Equals(Path.GetFullPath(RootDirectoryPath), Path.GetFullPath(text), StringComparison.OrdinalIgnoreCase))
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                string destDirName = Path.Combine(path, newName);
                FileSystemService.MoveDirectory(text, destDirName);
            }
            else if (FileSystemService.FileExists(text))
            {
                string extension = Path.GetExtension(text);
                if (!newName.Contains("."))
                {
                    newName += extension;
                }
                string destFileName = Path.Combine(path, newName);
                FileSystemService.MoveFile(text, destFileName);
            }
            else
            {
                FileManagementExceptionExecutor.ThrowFileNotFound(text);
            }
        }
Пример #5
0
        public void Move(string sourcePath, string destinationPath)
        {
            string text  = Path.Combine(RootDirectoryPath, PreparePath(sourcePath));
            string text2 = Path.Combine(RootDirectoryPath, PreparePath(destinationPath), Path.GetFileName(text));

            if (text == text2)
            {
                throw new Exception("Source and destination paths should be different.");
            }
            if (text2.StartsWith(text))
            {
                throw new Exception("Incorrect destination path for the move operation.");
            }
            if (FileSystemService.DirectoryExists(text))
            {
                FileSystemService.MoveDirectory(text, text2);
            }
            else if (FileSystemService.FileExists(text))
            {
                FileSystemService.MoveFile(text, text2);
            }
            else
            {
                FileManagementExceptionExecutor.ThrowFileNotFound(text);
            }
        }
Пример #6
0
        public void Copy(FileItemPathInfo sourcePathInfo, FileItemPathInfo destinationPathInfo)
        {
            if (!IsFileItemExists(sourcePathInfo))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(sourcePathInfo.GetPath());
            }
            if (!IsFileItemExists(destinationPathInfo))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(sourcePathInfo.GetPath());
            }
            if (!AllowCopyOrMove(sourcePathInfo, destinationPathInfo))
            {
                FileManagementExceptionExecutor.ThrowNoAccess();
            }

            var sourceFileItem = GetFileItem(sourcePathInfo);
            var copyFileItem   = CreateCopy(sourceFileItem);

            copyFileItem.ParentId = destinationPathInfo.GetFileItemKey <int>();
            copyFileItem.Name     = GenerateCopiedFileItemName(destinationPathInfo.GetFileItemKey <int>(), copyFileItem.Name, copyFileItem.IsDirectory);
            FileManagementDbContext.FileItems.Add(copyFileItem);

            if (copyFileItem.IsDirectory)
            {
                CopyDirectoryContentRecursive(sourceFileItem, copyFileItem);
            }
            FileManagementDbContext.SaveChanges();
        }
Пример #7
0
 private void ValidateFileItemNameSymbols(string fileItemName)
 {
     char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
     if (fileItemName.IndexOfAny(invalidFileNameChars) > -1)
     {
         FileManagementExceptionExecutor.ThrowInvalidSymbols();
     }
 }
Пример #8
0
        public void CreateDirectory(string rootKey, string name)
        {
            CheckThatDirOrFileNameIsNotEmpty(name, "name");
            ValidateFileItemNameSymbols(name);
            string path = Path.Combine(GetFullDirPathWithCheckOnExistence(rootKey), name);

            if (FileSystemService.DirectoryExists(path))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(Path.Combine(PreparePath(rootKey), name));
            }
            FileSystemService.CreateDirectory(path);
        }
Пример #9
0
        public void Rename(FileItemPathInfo pathInfo, string newName)
        {
            if (!IsFileItemExists(pathInfo))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath());
            }

            var fileItem = GetFileItem(pathInfo);

            fileItem.Name         = newName;
            fileItem.ModifiedById = GuestPersonId;
            fileItem.Modified     = DateTime.Now;
            FileManagementDbContext.SaveChanges();
        }
Пример #10
0
        public Stream GetFileContent(FileItemPathInfo pathInfo)
        {
            string path = pathInfo?.GetPath();
            string text = Path.Combine(RootDirectoryPath, PreparePath(path));

            if (Path.GetDirectoryName(text) == null)
            {
                _ = string.Empty;
            }
            if (FileSystemService.FileExists(text))
            {
                return(FileSystemService.GetFileContent(text));
            }
            FileManagementExceptionExecutor.ThrowFileNotFound(text);
            return(null);
        }
Пример #11
0
        public void Remove(FileItemPathInfo pathInfo)
        {
            if (!IsFileItemExists(pathInfo))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath());
            }

            var fileItem = GetFileItem(pathInfo);

            FileManagementDbContext.FileItems.Remove(fileItem);

            if (fileItem.IsDirectory)
            {
                RemoveDirectoryContentRecursive(fileItem.Id);
            }

            FileManagementDbContext.SaveChanges();
        }
Пример #12
0
        public void CreateDirectory(FileItemPathInfo pathInfo, string name)
        {
            if (!IsFileItemExists(pathInfo))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath());
            }

            var directory = new FileItem {
                Name         = name,
                Modified     = DateTime.Now,
                Created      = DateTime.Now,
                IsDirectory  = true,
                ParentId     = pathInfo.GetFileItemKey <int>(),
                ModifiedById = GuestPersonId
            };

            FileManagementDbContext.FileItems.Add(directory);
            FileManagementDbContext.SaveChanges();
        }
Пример #13
0
        public void Copy(string sourcePath, string destinationPath)
        {
            string text = Path.Combine(RootDirectoryPath, PreparePath(sourcePath));
            string path = Path.Combine(RootDirectoryPath, PreparePath(destinationPath));

            if (FileSystemService.DirectoryExists(text))
            {
                string destinationPath2 = GenerateCopiedFileItemPath(path, Path.GetFileName(text), isDirectory: true);
                FileSystemService.CopyDirectory(text, destinationPath2);
            }
            else if (FileSystemService.FileExists(text))
            {
                string destinationFilePath = GenerateCopiedFileItemPath(path, Path.GetFileName(text), isDirectory: false);
                FileSystemService.CopyFile(text, destinationFilePath);
            }
            else
            {
                FileManagementExceptionExecutor.ThrowFileNotFound(destinationPath);
            }
        }
Пример #14
0
        public void Move(FileItemPathInfo pathInfo, FileItemPathInfo destinationPathInfo)
        {
            if (!IsFileItemExists(pathInfo))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath());
            }
            if (!IsFileItemExists(destinationPathInfo))
            {
                FileManagementExceptionExecutor.ThrowDirectoryExists(pathInfo.GetPath());
            }
            if (!AllowCopyOrMove(pathInfo, destinationPathInfo))
            {
                FileManagementExceptionExecutor.ThrowNoAccess();
            }

            var fileItem = GetFileItem(pathInfo);

            fileItem.ParentId     = destinationPathInfo.GetFileItemKey <int>();
            fileItem.Modified     = DateTime.Now;
            fileItem.ModifiedById = GuestPersonId;
            FileManagementDbContext.SaveChanges();
        }
Пример #15
0
        private object ExecuteUnsafe(FileSystemCommand command, IDictionary <string, object> arguments)
        {
            switch (command)
            {
            case FileSystemCommand.GetDirContents:
                return(ExecuteGetDirContentCommand(arguments));

            case FileSystemCommand.CreateDir:
                if (!FileSystemConfiguration.AllowCreate)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteCreateDirCommand(arguments));

            case FileSystemCommand.Rename:
                if (!FileSystemConfiguration.AllowRename)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteRenameCommand(arguments));

            case FileSystemCommand.Move:
                if (!FileSystemConfiguration.AllowMove)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteMoveCommand(arguments));

            case FileSystemCommand.Copy:
                if (!FileSystemConfiguration.AllowCopy)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteCopyCommand(arguments));

            case FileSystemCommand.Remove:
                if (!FileSystemConfiguration.AllowRemove)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteRemoveCommand(arguments));

            case FileSystemCommand.UploadChunk:
                if (!FileSystemConfiguration.AllowUpload)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteUploadChunkCommand(arguments));

            case FileSystemCommand.AbortUpload:
                if (!FileSystemConfiguration.AllowUpload)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteAbortUploadCommand(arguments));

            case FileSystemCommand.Download:
                if (!FileSystemConfiguration.AllowDownload)
                {
                    FileManagementExceptionExecutor.ThrowNoAccess();
                }
                return(ExecuteDownloadCommand(arguments));

            default:
                throw new NotSupportedException();
            }
        }