public async Task <IEnumerable <FileFragmentedEvent> > Handle(FragmentTextFileCommand request, CancellationToken cancellationToken)
        {
            if (request.SizeInBytes <= _settings.ChunkSizeInBytes)
            {
                return(new[]
                {
                    new FileFragmentedEvent
                    {
                        Extension = request.Extension,
                        UploadId = request.UploadId,
                        FileName = request.FileName,
                        Offset = 0,
                        Length = request.SizeInBytes
                    }
                });
            }

            var initialOffsets = FindInitialOffsets(request.SizeInBytes, _settings.ChunkSizeInBytes);

            var rightBorders = await Task.WhenAll(initialOffsets.Select(offset =>
                                                                        FindRightBorderAsync(offset, request.SizeInBytes, request.UploadId, cancellationToken)));

            return(ConstructEvents(rightBorders, request));
        }
        private IEnumerable <FileFragmentedEvent> ConstructEvents(long[] rightBorders, FragmentTextFileCommand request)
        {
            var left = 0L;

            foreach (var right in rightBorders.Where(b => b != FailureFlag).OrderBy(x => x))
            {
                yield return(new FileFragmentedEvent
                {
                    Extension = request.Extension,
                    UploadId = request.UploadId,
                    FileName = request.FileName,
                    Offset = left,
                    Length = right - left
                });

                left = right + 1;
            }

            // The last part
            yield return(new FileFragmentedEvent
            {
                Extension = request.Extension,
                UploadId = request.UploadId,
                FileName = request.FileName,
                Offset = left,
                Length = request.SizeInBytes - left
            });
        }