private (string, string) GetPaths()
        {
            string outputDirectory     = _configuration["output-directory"];
            int    newFilePathSize     = _dialog.ReadValue(sizeof(int), value => BitConverter.ToInt32(value, 0));
            string newRelativeFilePath = _dialog.ReadValue(newFilePathSize, value => Encoding.GetEncoding("UTF-8").GetString(value));

            return(
                _fileSystem.Path.Combine(outputDirectory, _dialog.DialogData.RelativeFilePath),
                _fileSystem.Path.Combine(outputDirectory, newRelativeFilePath));
        }
示例#2
0
        public virtual void ProcessRequest()
        {
            string absolutePath = _fileSystem.Path.Combine(_outputDirectory, _dialog.DialogData.RelativeFilePath);

            if (ClientAndServerFileHashesAreEqual(absolutePath))
            {
                return;
            }
            CreateRelativeDirectory(absolutePath);
            string tmpFilePath = GenerateTempFilePath(absolutePath);

            RemoveExistingFile(tmpFilePath);

            using (var destinationStream = _fileSystem.File.Open(tmpFilePath, FileMode.CreateNew))
            {
                int takenFromCache  = 0;
                int readFromNetwork = 0;
                while (true)
                {
                    string hash = _dialog.ReadValue(_hashSize, ToHex);
                    if (string.IsNullOrWhiteSpace(hash))
                    {
                        break;
                    }

                    string segmentPath = GetSegmentPath(_outputDirectory, hash);
                    CreateRelativeDirectory(segmentPath);

                    byte[] fileSegment;
                    if (_fileSystem.File.Exists(segmentPath))
                    {
                        _dialog.NotifyClient(actionIsNeeded: false);
                        fileSegment = _fileSystem.File.ReadAllBytes(segmentPath);
                        takenFromCache++;
                    }
                    else
                    {
                        _dialog.NotifyClient(actionIsNeeded: true);
                        fileSegment = _dialog.ReadValue(int.Parse(_configuration["segment-size"]), value => value);
                        _fileSystem.File.WriteAllBytes(segmentPath, fileSegment);
                        readFromNetwork++;
                    }

                    destinationStream.Write(fileSegment, 0, fileSegment.Length);
                }
                Log.Information($"For file {absolutePath}, {takenFromCache} segments were taken from cache, {readFromNetwork} were read from network.");
            }

            RemoveExistingFile(absolutePath);
            _fileSystem.File.Move(tmpFilePath, absolutePath);
        }