public void default_constructor_should_return_expected_result()
        {
            var result = new GridFSDownloadOptions();

            result.CheckMD5.Should().NotHaveValue();
            result.Seekable.Should().NotHaveValue();
        }
        public void default_constructor_should_return_expected_result()
        {
            var result = new GridFSDownloadOptions();

            result.CheckMD5.Should().NotHaveValue();
            result.Seekable.Should().NotHaveValue();
        }
        public async Task <GridFSFileInfo> DownloadFileAsync(ObjectId id, GridFSDownloadOptions options = null)
        {
            var gridFsBucket = new GridFSBucket(this.database);

            using (GridFSDownloadStream sourceStream = await gridFsBucket.OpenDownloadStreamAsync(id))
            {
                long filezise = sourceStream.Length;
                //缓冲区大小
                int    maxlenth = 4096;
                byte[] buffer   = new byte[maxlenth];
                using (FileStream destinationStream = File.Open(_confg + "/" + sourceStream.FileInfo.Filename, FileMode.Create))
                {
                    while (filezise > 0)
                    {
                        if (filezise < maxlenth)
                        {
                            maxlenth = (int)filezise;
                        }
                        await sourceStream.ReadAsync(buffer, 0, maxlenth);

                        await destinationStream.WriteAsync(buffer, 0, maxlenth);

                        filezise -= maxlenth;
                    }
                }
                return(sourceStream.FileInfo);
            }
        }
        /// <summary>
        ///下载文件
        /// </summary>
        /// <param name="id">注意这个是files_id的值,而不是_id的值</param>
        /// <param name="destinationStream">文件流或者内存流</param>
        /// <param name="options"></param>
        public void DownloadToStream(ObjectId id, Stream destinationStream, GridFSDownloadOptions options = null)
        {
            MongoClient client = GetClient(m_connectionStr);
            var         db     = client.GetDatabase(DatabaseName);
            var         bucket = new GridFSBucket(db, BucksOptions);

            bucket.DownloadToStream(id, destinationStream, options);
        }
        public void Seekable_get_should_return_expected_result()
        {
            var subject = new GridFSDownloadOptions { Seekable = true };

            var result = subject.Seekable;

            result.Should().Be(true);
        }
Example #6
0
        /// <summary>
        /// 获取文件流
        /// </summary>
        /// <param name="host"></param>
        /// <param name="id">文件id,fs.files中的_id,也对应fs.chunks的files_id</param>
        /// <param name="options">下载选项</param>
        public static Stream DownloadFileToStream(MongodbHost host, BsonValue id, GridFSDownloadOptions options = null)
        {
            Stream destination = new MemoryStream();
            var    bucket      = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            bucket.DownloadToStream(id, destination, options);
            return(destination);
        }
        public void CheckMD5_set_should_have_expected_result()
        {
            var subject = new GridFSDownloadOptions();

            subject.CheckMD5 = true;

            subject.CheckMD5.Should().Be(true);
        }
        public void Seekable_set_should_have_expected_result()
        {
            var subject = new GridFSDownloadOptions();

            subject.Seekable = true;

            subject.Seekable.Should().Be(true);
        }
        public async Task <byte[]> DownloadAsBytesAsync(ObjectId id, GridFSDownloadOptions options = null)
        {
            MongoClient client = GetClient(m_connectionStr);
            var         db     = client.GetDatabase(DatabaseName);
            var         bucket = new GridFSBucket(db, BucksOptions);

            return(await bucket.DownloadAsBytesAsync(id, options));
        }
        public void Seekable_set_should_have_expected_result()
        {
            var subject = new GridFSDownloadOptions();

            subject.Seekable = true;

            subject.Seekable.Should().Be(true);
        }
        public void CheckMD5_set_should_have_expected_result()
        {
            var subject = new GridFSDownloadOptions();

            subject.CheckMD5 = true;

            subject.CheckMD5.Should().Be(true);
        }
        public void CheckMD5_get_should_return_expected_result()
        {
            var subject = new GridFSDownloadOptions { CheckMD5 = true };

            var result = subject.CheckMD5;

            result.Should().Be(true);
        }
Example #13
0
        public async Task <byte[]> DownLoadFileAsync(string fileName)
        {
            //var id = await GetFileId(fileName);
            var options = new GridFSDownloadOptions
            {
            };
            var bytes = await bucket.DownloadAsBytesAsync(fileName);

            return(bytes);
        }
        public void Seekable_get_should_return_expected_result()
        {
            var subject = new GridFSDownloadOptions {
                Seekable = true
            };

            var result = subject.Seekable;

            result.Should().Be(true);
        }
        public void CheckMD5_get_should_return_expected_result()
        {
            var subject = new GridFSDownloadOptions {
                CheckMD5 = true
            };

            var result = subject.CheckMD5;

            result.Should().Be(true);
        }
Example #16
0
        private void ParseOptions(BsonDocument options)
        {
            foreach (var option in options.Elements)
            {
                _options = _options ?? new GridFSDownloadOptions();
                switch (option.Name)
                {
                case "checkMD5":
                    _options.CheckMD5 = option.Value.ToBoolean();
                    break;

                default:
                    throw new ArgumentException(string.Format("Invalid option name: {0}.", option.Name));
                }
            }
        }
        private void ParseOptions(BsonDocument options)
        {
            foreach (var option in options.Elements)
            {
                _options = _options ?? new GridFSDownloadOptions();
                switch (option.Name)
                {
                    case "checkMD5":
                        _options.CheckMD5 = option.Value.ToBoolean();
                        break;

                    default:
                        throw new ArgumentException(string.Format("Invalid option name: {0}.", option.Name));
                }
            }
        }
        public void Read_should_return_expected_result(
            [Values(0.0, 0.5, 1.0, 1.5, 2.0, 2.5)] double fileLengthMultiple,
            [Values(0.0, 0.5)] double positionMultiple,
            [Values(0, 2)] int offset,
            [Values(0.0, 0.5, 1.0)] double countMultiple,
            [Values(false, true)] bool async)
        {
            RequireServer.Check();
            var chunkSize  = 4;
            var bucket     = CreateBucket(chunkSize);
            var fileLength = (int)(chunkSize * fileLengthMultiple);
            var content    = CreateContent(fileLength);
            var id         = CreateGridFSFile(bucket, content);
            var options    = new GridFSDownloadOptions {
                Seekable = true
            };
            var subject  = bucket.OpenDownloadStream(id, options);
            var position = (int)(fileLength * positionMultiple);

            subject.Position = position;
            var count          = (int)(fileLength * countMultiple);
            var buffer         = new byte[offset + count + 1];
            var expectedResult = Math.Min(count, fileLength - position);

            int result;

            if (async)
            {
                result = subject.ReadAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.Read(buffer, offset, count);
            }

            result.Should().Be(expectedResult);
            buffer.Take(offset).Any(b => b != 0).Should().BeFalse();
            buffer.Skip(offset).Take(result).Should().Equal(content.Skip(position).Take(result));
            buffer.Last().Should().Be(0);
        }
Example #19
0
        /// <summary>
        /// Downloads a file stored in GridFS and writes the contents to a stream.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="destination"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public Task DownloadToStreamAsync(ObjectId id, Stream destination, GridFSDownloadOptions options = null)
        {
            var bucket = new GridFSBucket(base.GetDatabase(), BucketOptions);

            return(bucket.DownloadToStreamAsync(id, destination, options));
        }
        public void Read_should_return_expected_result(
            [Values(0.0, 0.5, 1.0, 1.5, 2.0, 2.5)] double fileLengthMultiple,
            [Values(0.0, 0.5)] double positionMultiple,
            [Values(0, 2)] int offset,
            [Values(0.0, 0.5, 1.0)] double countMultiple,
            [Values(false, true)] bool async)
        {
            var chunkSize = 4;
            var bucket = CreateBucket(chunkSize);
            var fileLength = (int)(chunkSize * fileLengthMultiple);
            var content = CreateContent(fileLength);
            var id = CreateGridFSFile(bucket, content);
            var options = new GridFSDownloadOptions { Seekable = true };
            var subject = bucket.OpenDownloadStream(id, options);
            var position = (int)(fileLength * positionMultiple);
            subject.Position = position;
            var count = (int)(fileLength * countMultiple);
            var buffer = new byte[offset + count + 1];
            var expectedResult = Math.Min(count, fileLength - position);

            int result;
            if (async)
            {
                result = subject.ReadAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.Read(buffer, offset, count);
            }

            result.Should().Be(expectedResult);
            buffer.Take(offset).Any(b => b != 0).Should().BeFalse();
            buffer.Skip(offset).Take(result).Should().Equal(content.Skip(position).Take(result));
            buffer.Last().Should().Be(0);
        }
Example #21
0
        /// <summary>
        /// Downloads a file stored in GridFS and returns it as a byte array.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public Task <byte[]> DownloadAsBytesAsync(ObjectId id, GridFSDownloadOptions options = null)
        {
            var bucket = new GridFSBucket(base.GetDatabase(), BucketOptions);

            return(bucket.DownloadAsBytesAsync(id, options));
        }
Example #22
0
 public async Task <GridFSFileInfo> DownloadFileAsync(ObjectId id, GridFSDownloadOptions options = null)
 {
     return(await _repository.DownloadFileAsync(id, options));
 }