Ejemplo n.º 1
0
 public Task DownloadToStreamAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, nameof(id));
     Ensure.IsNotNull(destination, nameof(destination));
     options = options ?? new GridFSDownloadOptions();
     return(DownloadToStreamHelperAsync(id, destination, options, cancellationToken));
 }
Ejemplo n.º 2
0
        private async Task <byte[]> DownloadAsBytesHelperAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
            {
                var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);

                return(await DownloadAsBytesHelperAsync(binding, fileInfo, options, cancellationToken).ConfigureAwait(false));
            }
        }
Ejemplo n.º 3
0
 /// <inheritdoc />
 public byte[] DownloadAsBytes(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull((object)id, nameof(id));
     options = options ?? new GridFSDownloadOptions();
     using (var binding = GetSingleServerReadBinding(cancellationToken))
     {
         var fileInfo = GetFileInfo(binding, id, cancellationToken);
         return(DownloadAsBytesHelper(binding, fileInfo, options, cancellationToken));
     }
 }
Ejemplo n.º 4
0
 /// <inheritdoc />
 public GridFSDownloadStream <TFileId> OpenDownloadStream(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull((object)id, nameof(id));
     options = options ?? new GridFSDownloadOptions();
     using (var binding = GetSingleServerReadBinding(cancellationToken))
     {
         var fileInfo = GetFileInfo(binding, id, cancellationToken);
         return(CreateDownloadStream(binding.Fork(), fileInfo, options, cancellationToken));
     }
 }
Ejemplo n.º 5
0
 /// <inheritdoc />
 public void DownloadToStream(TFileId id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull((object)id, nameof(id));
     Ensure.IsNotNull(destination, nameof(destination));
     options = options ?? new GridFSDownloadOptions();
     using (var binding = GetSingleServerReadBinding(cancellationToken))
     {
         var fileInfo = GetFileInfo(binding, id, cancellationToken);
         DownloadToStreamHelper(binding, fileInfo, destination, options, cancellationToken);
     }
 }
Ejemplo n.º 6
0
 /// <inheritdoc />
 public async Task DownloadToStreamAsync(TFileId id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull((object)id, nameof(id));
     Ensure.IsNotNull(destination, nameof(destination));
     options = options ?? new GridFSDownloadOptions();
     using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
     {
         var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);
         await DownloadToStreamHelperAsync(binding, fileInfo, destination, options, cancellationToken).ConfigureAwait(false);
     }
 }
Ejemplo n.º 7
0
        /// <inheritdoc />
        public async Task <byte[]> DownloadAsBytesAsync(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull((object)id, nameof(id));
            options = options ?? new GridFSDownloadOptions();
            using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
            {
                var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);

                return(await DownloadAsBytesHelperAsync(binding, fileInfo, options, cancellationToken).ConfigureAwait(false));
            }
        }
Ejemplo n.º 8
0
        private GridFSDownloadStream CreateDownloadStream(IReadBindingHandle binding, GridFSFileInfo fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;
            var seekable = options.Seekable ?? false;
            if (checkMD5 && seekable)
            {
                throw new ArgumentException("CheckMD5 can only be used when Seekable is false.");
            }

            if (seekable)
            {
                return new GridFSSeekableDownloadStream(this, binding, fileInfo);
            }
            else
            {
                return new GridFSForwardOnlyDownloadStream(this, binding, fileInfo, checkMD5);
            }
        }
Ejemplo n.º 9
0
 public Task<GridFSDownloadStream> OpenDownloadStreamAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, nameof(id));
     options = options ?? new GridFSDownloadOptions();
     return OpenDownloadStreamHelperAsync(id, options, cancellationToken);
 }
Ejemplo n.º 10
0
 /// <inheritdoc />
 public Task<byte[]> DownloadAsBytesAsync(ObjectId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     options = options ?? new GridFSDownloadOptions();
     return DownloadAsBytesHelperAsync(new BsonObjectId(id), options, cancellationToken);
 }
Ejemplo n.º 11
0
        private async Task <byte[]> DownloadAsBytesHelperAsync(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (fileInfo.Length > int.MaxValue)
            {
                throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array.");
            }

            var bytes = new byte[(int)fileInfo.Length];

            using (var destination = new MemoryStream(bytes))
            {
                await DownloadToStreamHelperAsync(binding, fileInfo, destination, options, cancellationToken).ConfigureAwait(false);

                return(bytes);
            }
        }
Ejemplo n.º 12
0
 private async Task DownloadToStreamAsyncHelper(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
     {
         var filesCollectionDocument = await GetFilesCollectionDocumentAsync(binding, id, cancellationToken).ConfigureAwait(false);
         await DownloadToStreamAsyncHelper(binding, filesCollectionDocument, destination, options, cancellationToken).ConfigureAwait(false);
     }
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Opens a Stream that can be used by the application to read data from a GridFS file.
        /// </summary>
        /// <param name="id">The file id.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A Task whose result is a Stream.</returns>
        public async Task <GridFSDownloadStream> OpenDownloadStreamAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var wrappedStream = await _bsonValueBucket.OpenDownloadStreamAsync(id, options, cancellationToken).ConfigureAwait(false);

            return(new GridFSDownloadStream(wrappedStream));
        }
Ejemplo n.º 14
0
 public Task DownloadToStreamAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, nameof(id));
     Ensure.IsNotNull(destination, nameof(destination));
     return DownloadToStreamHelperAsync(id, destination, options, cancellationToken);
 }
Ejemplo n.º 15
0
        private void DownloadToStreamHelper(IReadBindingHandle binding, GridFSFileInfo fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), fileInfo, checkMD5))
            {
                var count = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    source.ReadBytes(buffer, 0, partialCount, cancellationToken);
                    //((Stream)source).ReadBytes(buffer, 0, partialCount, cancellationToken);
                    destination.Write(buffer, 0, partialCount);
                    count -= partialCount;
                }
            }
        }
Ejemplo n.º 16
0
 private void DownloadToStreamHelper(BsonValue id, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = GetSingleServerReadBinding(cancellationToken))
     {
         var fileInfo = GetFileInfo(binding, id, cancellationToken);
         DownloadToStreamHelper(binding, fileInfo, destination, options, cancellationToken);
     }
 }
Ejemplo n.º 17
0
 private byte[] DownloadAsBytesHelper(BsonValue id, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = GetSingleServerReadBinding(cancellationToken))
     {
         var fileInfo = GetFileInfo(binding, id, cancellationToken);
         return DownloadAsBytesHelper(binding, fileInfo, options, cancellationToken);
     }
 }
Ejemplo n.º 18
0
 public void DownloadToStream(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, nameof(id));
     Ensure.IsNotNull(destination, nameof(destination));
     options = options ?? new GridFSDownloadOptions();
     DownloadToStreamHelper(id, destination, options, cancellationToken);
 }
Ejemplo n.º 19
0
 private GridFSDownloadStream OpenDownloadStreamHelper(BsonValue id, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = GetSingleServerReadBinding(cancellationToken))
     {
         var fileInfo = GetFileInfo(binding, id, cancellationToken);
         return CreateDownloadStream(binding.Fork(), fileInfo, options, cancellationToken);
     }
 }
Ejemplo n.º 20
0
 public Task<byte[]> DownloadAsBytesAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, "id");
     return DownloadAsBytesAsyncHelper(id, options, cancellationToken);
 }
Ejemplo n.º 21
0
 public Task<GridFSDownloadStream> OpenDownloadStreamAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, "id");
     return OpenDownloadStreamAsyncHelper(id, options, cancellationToken);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Downloads a file stored in GridFS and writes the contents to a stream.
 /// </summary>
 /// <param name="id">The file id.</param>
 /// <param name="destination">The destination.</param>
 /// <param name="options">The options.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>A Task.</returns>
 public Task DownloadToStreamAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_bsonValueBucket.DownloadToStreamAsync(id, destination, options, cancellationToken));
 }
Ejemplo n.º 23
0
        private async Task<GridFSDownloadStream> OpenDownloadStreamHelperAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            options = options ?? new GridFSDownloadByNameOptions();

            using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
            {
                var filesCollectionDocument = await GetFilesCollectionDocumentAsync(binding, id, cancellationToken).ConfigureAwait(false);
                return CreateDownloadStream(binding.Fork(), filesCollectionDocument, options, cancellationToken);
            }
        }
Ejemplo n.º 24
0
        private async Task <byte[]> DownloadAsBytesAsyncHelper(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (filesCollectionDocument.Length > int.MaxValue)
            {
                throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array.");
            }

            using (var destination = new MemoryStream((int)filesCollectionDocument.Length))
            {
                await DownloadToStreamAsyncHelper(binding, filesCollectionDocument, destination, options, cancellationToken).ConfigureAwait(false);

                return(destination.GetBuffer());
            }
        }
Ejemplo n.º 25
0
        /// <inheritdoc />
        public async Task <GridFSDownloadStream <TFileId> > OpenDownloadStreamAsync(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull((object)id, nameof(id));
            options = options ?? new GridFSDownloadOptions();
            using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
            {
                var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);

                return(CreateDownloadStream(binding.Fork(), fileInfo, options, cancellationToken));
            }
        }
Ejemplo n.º 26
0
 /// <inheritdoc />
 public Task <byte[]> DownloadAsBytesAsync(ObjectId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(DownloadAsBytesHelperAsync(new BsonObjectId(id), options, cancellationToken));
 }
Ejemplo n.º 27
0
 private async Task<byte[]> DownloadAsBytesAsyncHelper(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
     {
         var filesCollectionDocument = await GetFilesCollectionDocumentAsync(binding, id, cancellationToken).ConfigureAwait(false);
         return await DownloadAsBytesAsyncHelper(binding, filesCollectionDocument, options, cancellationToken).ConfigureAwait(false);
     }
 }
Ejemplo n.º 28
0
 /// <inheritdoc />
 public Task DownloadToStreamAsync(ObjectId id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(destination, nameof(destination));
     return(DownloadToStreamHelperAsync(new BsonObjectId(id), destination, options, cancellationToken));
 }
Ejemplo n.º 29
0
 public Task <GridFSDownloadStream> OpenDownloadStreamAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, nameof(id));
     return(OpenDownloadStreamHelperAsync(id, options, cancellationToken));
 }
Ejemplo n.º 30
0
        private GridFSDownloadStream <TFileId> CreateDownloadStream(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;
            var seekable = options.Seekable ?? false;

            if (checkMD5 && seekable)
            {
                throw new ArgumentException("CheckMD5 can only be used when Seekable is false.");
            }

            if (seekable)
            {
                return(new GridFSSeekableDownloadStream <TFileId>(this, binding, fileInfo));
            }
            else
            {
                return(new GridFSForwardOnlyDownloadStream <TFileId>(this, binding, fileInfo, checkMD5));
            }
        }
Ejemplo n.º 31
0
 /// <inheritdoc />
 public Task <GridFSDownloadStream> OpenDownloadStreamAsync(ObjectId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(OpenDownloadStreamHelperAsync(new BsonObjectId(id), options, cancellationToken));
 }
Ejemplo n.º 32
0
        private async Task DownloadToStreamHelperAsync(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            var retryReads = _database.Client.Settings.RetryReads;

            using (var source = new GridFSForwardOnlyDownloadStream <TFileId>(this, binding.Fork(), fileInfo, checkMD5)
            {
                RetryReads = retryReads
            })
            {
                var count  = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    count -= partialCount;
                }

                await source.CloseAsync(cancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 33
0
        private async Task<byte[]> DownloadAsBytesHelperAsync(IReadBindingHandle binding, GridFSFileInfo fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (fileInfo.Length > int.MaxValue)
            {
                throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array.");
            }

            using (var destination = new MemoryStream((int)fileInfo.Length))
            {
                await DownloadToStreamHelperAsync(binding, fileInfo, destination, options, cancellationToken).ConfigureAwait(false);
                return destination.GetBuffer();
            }
        }
Ejemplo n.º 34
0
 /// <inheritdoc />
 public Task DownloadToStreamAsync(ObjectId id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(destination, nameof(destination));
     options = options ?? new GridFSDownloadOptions();
     return DownloadToStreamHelperAsync(new BsonObjectId(id), destination, options, cancellationToken);
 }
Ejemplo n.º 35
0
 private async Task DownloadToStreamHelperAsync(BsonValue id, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
     {
         var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);
         await DownloadToStreamHelperAsync(binding, fileInfo, destination, options, cancellationToken).ConfigureAwait(false);
     }
 }
Ejemplo n.º 36
0
 /// <inheritdoc />
 public Task<GridFSDownloadStream> OpenDownloadStreamAsync(ObjectId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     options = options ?? new GridFSDownloadOptions();
     return OpenDownloadStreamHelperAsync(new BsonObjectId(id), options, cancellationToken);
 }
Ejemplo n.º 37
0
 public Task<byte[]> DownloadAsBytesAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, nameof(id));
     options = options ?? new GridFSDownloadOptions();
     return DownloadAsBytesHelperAsync(id, options, cancellationToken);
 }
Ejemplo n.º 38
0
 private async Task<byte[]> DownloadAsBytesHelperAsync(BsonValue id, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
     {
         var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);
         return await DownloadAsBytesHelperAsync(binding, fileInfo, options, cancellationToken).ConfigureAwait(false);
     }
 }
Ejemplo n.º 39
0
        private async Task DownloadToStreamAsyncHelper(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), filesCollectionDocument, checkMD5))
            {
                var count  = source.Length;
                var buffer = new byte[filesCollectionDocument.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    count -= partialCount;
                }

                await source.CloseAsync(cancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 40
0
        private async Task DownloadToStreamHelperAsync(IReadBindingHandle binding, GridFSFileInfo fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), fileInfo, checkMD5))
            {
                var count = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);
                    await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);
                    count -= partialCount;
                }

                await source.CloseAsync(cancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 41
0
 private async Task DownloadToStreamHelperAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
     {
         var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);
         await DownloadToStreamHelperAsync(binding, fileInfo, destination, options, cancellationToken).ConfigureAwait(false);
     }
 }
Ejemplo n.º 42
0
 private async Task<GridFSDownloadStream> OpenDownloadStreamHelperAsync(BsonValue id, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
     {
         var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);
         return CreateDownloadStream(binding.Fork(), fileInfo, options, cancellationToken);
     }
 }
Ejemplo n.º 43
0
        private async Task <GridFSDownloadStream> OpenDownloadStreamHelperAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            options = options ?? new GridFSDownloadByNameOptions();

            using (var binding = await GetSingleServerReadBindingAsync(cancellationToken).ConfigureAwait(false))
            {
                var fileInfo = await GetFileInfoAsync(binding, id, cancellationToken).ConfigureAwait(false);

                return(CreateDownloadStream(binding.Fork(), fileInfo, options, cancellationToken));
            }
        }
Ejemplo n.º 44
0
 /// <summary>
 /// Downloads a file stored in GridFS and writes the contents to a stream.
 /// </summary>
 /// <param name="id">The file id.</param>
 /// <param name="destination">The destination.</param>
 /// <param name="options">The options.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 public void DownloadToStream(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     _bsonValueBucket.DownloadToStream(id, destination, options, cancellationToken);
 }
Ejemplo n.º 45
0
 public Task <byte[]> DownloadAsBytesAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, nameof(id));
     return(DownloadAsBytesHelperAsync(id, options, cancellationToken));
 }
Ejemplo n.º 46
0
        /// <summary>
        /// Opens a Stream that can be used by the application to read data from a GridFS file.
        /// </summary>
        /// <param name="id">The file id.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A Stream.</returns>
        public GridFSDownloadStream OpenDownloadStream(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var wrappedStream = _bsonValueBucket.OpenDownloadStream(id, options, cancellationToken);

            return(new GridFSDownloadStream(wrappedStream));
        }
Ejemplo n.º 47
0
        private void DownloadToStreamHelper(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream <TFileId>(this, binding.Fork(), fileInfo, checkMD5))
            {
                var count  = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    source.ReadBytes(buffer, 0, partialCount, cancellationToken);
                    //((Stream)source).ReadBytes(buffer, 0, partialCount, cancellationToken);
                    destination.Write(buffer, 0, partialCount);
                    count -= partialCount;
                }
            }
        }
Ejemplo n.º 48
0
 /// <summary>
 /// Downloads a file stored in GridFS and returns it as a byte array.
 /// </summary>
 /// <param name="id">The file id.</param>
 /// <param name="options">The options.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>A Task whose result is a byte array containing the contents of the file stored in GridFS.</returns>
 public Task <byte[]> DownloadAsBytesAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_bsonValueBucket.DownloadAsBytesAsync(id, options, cancellationToken));
 }
Ejemplo n.º 49
0
 public Task DownloadToStreamAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     Ensure.IsNotNull(id, "id");
     Ensure.IsNotNull(destination, "destination");
     return(DownloadToStreamAsyncHelper(id, destination, options, cancellationToken));
 }