Beispiel #1
0
        public Task <bool> CopyTo(Stream stream, CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            return(stream.CanSeek
                ? _errorHandler.HandleErrors(() => CopyToImpl(stream, cancellationToken), _stream.FileInfo.Filename, cancellationToken)
                : CopyToImpl(stream, cancellationToken));
        }
        public Task <bool> CopyTo(Stream stream, CancellationToken cancellationToken)
        {
            if (stream is null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            const int MinBufferSize = 81920;
            const int MaxBufferSize = 1 << 20;
            var       bufferSize    = Math.Min(Math.Max(_stream.FileInfo.ChunkSizeBytes, MinBufferSize), MaxBufferSize);

            return(_errorHandler.HandleErrors(
                       Copy,
                       Filename,
                       () => (stream.CanSeek && _stream.CanSeek) || _stream.Position == 0,
                       cancellationToken));

            async Task <bool> Copy()
            {
                var position = stream.CanSeek ? stream.Position : 0;

                try
                {
                    if (_stream.Position != 0)
                    {
                        _stream.Position = 0;
                    }

                    await _stream.CopyToAsync(stream, bufferSize, cancellationToken);
                }
                catch (GridFSChunkException) when(position == 0 && _stream.Position == 0)
                {
                    return(false);
                }
                catch (GridFSChunkException) when(stream.CanSeek)
                {
                    stream.SetLength(position);
                    return(false);
                }
                catch when(stream.CanSeek)
                    {
                        stream.SetLength(position);
                        throw;
                    }

                return(true);
            }
        }
        public async Task <Components.IFileInfo> FetchFile(string filename, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return(null);
            }

            var stream = await _errorHandler.HandleErrors(
                () => FetchStream(filename, cancellationToken),
                filename,
                cancellationToken);

            if (stream == null)
            {
                return(null);
            }

            return(new GridFSFileInfo(stream, _bucket, _errorHandler));
        }
Beispiel #4
0
 public Task <bool> CopyTo(Stream stream, CancellationToken cancellationToken)
 => stream?.CanSeek == true
         ? _errorHandler.HandleErrors(() => CopyToImpl(stream, cancellationToken), Filename, cancellationToken)
         : CopyToImpl(stream, cancellationToken);