private GridFSDownloadStream CreateDownloadStream(FileDocument fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken) { var checkMD5 = options.CheckMD5 ?? false; var seekable = options.Seekable ?? false; return(new GridFSForwardOnlyDownloadStream(this, fileInfo, checkMD5)); }
public async Task DownloadToStreamAsync(Guid fileId, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { options = options ?? new GridFSDownloadOptions(); var fileInfo = GetFileInfo(fileId, cancellationToken); await DownloadToStreamHelperAsync(fileInfo, destination, options, cancellationToken).ConfigureAwait(false); }
public GridFSDownloadStream OpenDownloadStream(Guid fileId, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { options = options ?? new GridFSDownloadOptions(); var fileInfo = GetFileInfo(fileId, cancellationToken); return(CreateDownloadStream(fileInfo, options, cancellationToken)); }
private async Task DownloadToStreamHelperAsync(FileDocument fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken) { var checkMD5 = options.CheckMD5 ?? false; using (var source = new GridFSForwardOnlyDownloadStream(this, fileInfo, checkMD5)) { var count = source.Length; var buffer = new byte[fileInfo.ChunkSize]; while (count > 0) { var partialCount = (int)Math.Min(buffer.Length, count); source.Read(buffer, 0, partialCount); await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false); count -= partialCount; } await source.CloseAsync(cancellationToken).ConfigureAwait(false); } }