예제 #1
0
        public Result Process(ValidArguments arguments)
        {
            _defaultUncompressedBlockSize = arguments.DefaultUncompressedBlockSize == 0 ? 1024 * 1024 : arguments.DefaultUncompressedBlockSize;
            _processorCount = arguments.ProcessorCount <= 0 || arguments.ProcessorCount > Environment.ProcessorCount ? Environment.ProcessorCount : arguments.DefaultUncompressedBlockSize;

            try {
                Result result = Result.Fail($"Wrong command {arguments.Command}");
                switch (arguments.Command)
                {
                case CommandEnum.Compress:
                    result = Compress(arguments.SourceFileName, arguments.DestinationFileName, _defaultUncompressedBlockSize);
                    break;

                case CommandEnum.Decompress:
                    result = Decompress(arguments.SourceFileName, arguments.DestinationFileName);
                    break;

                default:
                    break;
                }
                return(result);
            } catch (FileNotFoundException ex) {
                var message = "File not found exception" + ": " + ex.Message;
                message += $"\n {ex}";
                return(Result.Fail(message));
            } catch (FileLoadException ex) {
                var message = "File load exception" + ": " + ex.Message;
                message += $"\n {ex}";
                return(Result.Fail(message));
            } catch (IOException ex) {
                var message = "Input/output exception" + ": " + ex.Message;
                message += $"\n File exception{ex}";
                return(Result.Fail(message));
            }
        }
예제 #2
0
        public static Result <ValidArguments> Check(string[] args)
        {
            var validParameters = new ValidArguments();

            if (args.Length == 0)
            {
                return(Result.Fail <ValidArguments>($"There are no agruments! {UseHelp}"));
            }

            string command = args[0];

            if (args.Length == 1 && command.Equals("help", StringComparison.OrdinalIgnoreCase))
            {
                return(Result.Fail <ValidArguments>($"{Help}"));
            }

            if (args.Length < 3)
            {
                return(Result.Fail <ValidArguments>($"Wrong number of agruments! {UseHelp}"));
            }

            args = args.Select(p => p.ToLower()).ToArray();

            if (!Commands.TryGetValue(command, out var commandEnum))
            {
                return(Result.Fail <ValidArguments>($"Wrong command {command}! {UseHelp}"));
            }

            var sourceFileName = args[1];

            if (!File.Exists(sourceFileName))
            {
                return(Result.Fail <ValidArguments>($"{sourceFileName} - source file doesn't exist!"));
            }

            var destinationFileName = args[2];

            if (String.Equals(sourceFileName, destinationFileName, StringComparison.OrdinalIgnoreCase))
            {
                return(Result.Fail <ValidArguments>($"{sourceFileName} - source and destination files can not be the same!"));
            }

            var sourceFileInfo       = new FileInfo(sourceFileName);
            var destinationFileInfo  = new FileInfo(destinationFileName);
            var destinationDriveName = destinationFileInfo.Directory.Root.FullName;

            var destinationDriveInfo = DriveInfo.GetDrives().FirstOrDefault(p => destinationDriveName.Contains(p.Name, StringComparison.InvariantCultureIgnoreCase));

            if (destinationDriveInfo == null)
            {
                return(Result.Fail <ValidArguments>($"File name {destinationFileName} is not valid!"));
            }

            if (destinationDriveInfo.TotalFreeSpace < sourceFileInfo.Length)
            {
                return(Result.Fail <ValidArguments>($"There are no enough free space! Needs bytes: {String.Format("{0:n0}", sourceFileInfo.Length)} but have got {String.Format("{0:n0}", destinationDriveInfo.TotalFreeSpace)}"));
            }

            if (File.Exists(destinationFileName))
            {
                //return Result.Fail<ValidArguments>($"{destinationFileName} - destination file already exist!");
            }

            validParameters.Command             = commandEnum;
            validParameters.SourceFileName      = sourceFileName;
            validParameters.DestinationFileName = destinationFileName;

            if (args.Length == 4 && Int32.TryParse(args[3], out int defaultSize))
            {
                validParameters.DefaultUncompressedBlockSize = defaultSize;
            }

            if (args.Length == 5 && Int32.TryParse(args[4], out int processorCount))
            {
                validParameters.ProcessorCount = processorCount;
            }

            return(Result.Ok <ValidArguments>(validParameters));
        }