예제 #1
0
        public async Task <string> DownloadFileAsync(GridFSFileInfo file)
        {
            var    gridFsBucket = new GridFSBucket(GetDatabase());
            string filename     = file.Metadata["fileName"].AsString;
            string targetDir    = Path.Combine(ProgramUI.GetWorkspaceDirectory(), MongoConfig.MongoConfigName);

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }

            string extension = ".bin";

            if (file.ContentType.Equals("plain/text", StringComparison.CurrentCultureIgnoreCase))
            {
                extension = ".txt";
            }
            string targetFilename = Path.Combine(targetDir, filename + "_" + file.Id.ToString() + extension);

            Logger.Log($"Saving file to {targetFilename}");
            // using (GridFSDownloadStream<ObjectId> sourceStream = await gridFsBucket.OpenDownloadStreamByNameAsync(filename))
            using (GridFSDownloadStream sourceStream = await gridFsBucket.OpenDownloadStreamAsync(file.Id))
            {
                using (FileStream destinationStream = File.Open(targetFilename, FileMode.Create))
                {
                    await sourceStream.CopyToAsync(destinationStream);
                }
            }
            return(targetFilename);
        }
        public async Task <MemoryStream> downTileData(string fileName)
        {
            GridFSFileInfo fileInfo;
            var            filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, fileName);

            var options = new GridFSFindOptions
            {
                Limit = 1
            };

            using (var cursor = gridFSBucket.Find(filter, options))
            {
                fileInfo = cursor.ToList().FirstOrDefault();
            }
            if (fileInfo.Id != null)
            {
                GridFSDownloadStream gridFSDownloadStream = gridFSBucket.OpenDownloadStream(fileInfo.Id);
            }

            MemoryStream destination = new MemoryStream();

            destination.Seek(0, SeekOrigin.Begin);
            await gridFSBucket.DownloadToStreamByNameAsync(fileName, destination);

            if (destination != null)
            {
                return(destination);
            }
            else
            {
                return(null);
            }
        }
        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);
            }
        }
        public async Task <Tuple <string, MemoryStream> > DownloadFileAsync(string id, string bucketName = "")
        {
            var gridFsBucket = string.IsNullOrEmpty(bucketName)? new GridFSBucket(this.database): new GridFSBucket(this.database, new GridFSBucketOptions
            {
                BucketName = bucketName
            });
            MemoryStream destinationStream = new MemoryStream();

            //await gridFsBucket.DownloadToStreamAsync(id, destinationStream);

            using (GridFSDownloadStream sourceStream = await gridFsBucket.OpenDownloadStreamAsync(new ObjectId(id)))
            {
                long filezise = sourceStream.Length;
                //缓冲区大小
                int    maxlenth = 4096;
                byte[] buffer   = new byte[maxlenth];

                while (filezise > 0)
                {
                    if (filezise < maxlenth)
                    {
                        maxlenth = (int)filezise;
                    }
                    await sourceStream.ReadAsync(buffer, 0, maxlenth);

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

                    filezise -= maxlenth;
                }
                return(new Tuple <string, MemoryStream>(sourceStream.FileInfo.Filename, destinationStream));
            }
        }
        public async Task <GridFSFileInfo> GetFileInfoAsync(string id)
        {
            var gridFsBucket = new GridFSBucket(this.database);

            using (GridFSDownloadStream sourceStream = await gridFsBucket.OpenDownloadStreamAsync(new ObjectId(id)))
            {
                return(sourceStream.FileInfo);
            }
        }
 private async Task <GridFSFileInfo <ObjectId> > ProcessFile(ObjectId fileId)
 {
     using (GridFSDownloadStream <ObjectId> stream = await _dbService.GridFs.OpenDownloadStreamAsync(fileId)) {
         using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) {
             ProcessLog.ProcessingLog(reader);
             return(stream.FileInfo);
         }
     }
 }
예제 #7
0
        public Stream DownloadingFile(string id)
        {
            IGridFSBucket bucket = GetGridFSBucket();

            ObjectId objectId = new ObjectId(id);

            GridFSDownloadStream <ObjectId> stream = bucket.OpenDownloadStream(objectId);

            return(stream);
        }
예제 #8
0
 public ActionResult GetImage(string imageId)
 {
     try
     {
         GridFSDownloadStream stream = _rentalService.GetStream(imageId);
         var contentType             = stream.FileInfo?.Metadata["contentType"].AsString;
         return(File(stream, contentType));
     }
     catch (Exception ex)
     {
         return(HttpNotFound(ex.Message));
     }
 }
예제 #9
0
        public async Task DemoDownloadFileAsync(IMongoDatabase database, string filePath, string fileName)
        {
            var gridFsBucket = new GridFSBucket(database);

            using (
                GridFSDownloadStream <ObjectId> sourceStream = await gridFsBucket.OpenDownloadStreamByNameAsync(fileName)
                )
            {
                using (FileStream destinationStream = File.Open(filePath, FileMode.Create))
                {
                    await sourceStream.CopyToAsync(destinationStream);
                }
            }
        }
예제 #10
0
        public Stream OpenDownToStreamByObjectId(ObjectId obj)
        {
            GridFSDownloadStream stream = null;

            try
            {
                stream = _client._fs.OpenDownloadStream(obj);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(stream);
        }
예제 #11
0
        private async Task <bool> CopyToImpl(Stream stream, CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (_stream.Position != 0)
            {
                var newStream = await _bucket.OpenDownloadStreamAsync(_stream.FileInfo.Id, null, cancellationToken);

                if (newStream == null)
                {
                    return(false);
                }

                using (_stream)
                {
                    _stream = newStream;
                }
            }

            const int MinBufferSize = 81920;
            const int MaxBufferSize = 1 << 20;
            var       bufferSize    = Math.Min(Math.Max(_stream.FileInfo.ChunkSizeBytes, MinBufferSize), MaxBufferSize);
            var       position      = stream.CanSeek ? stream.Position : 0;

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

            return(true);
        }
예제 #12
0
 public GridFSFileInfo(GridFSDownloadStream <BsonValue> stream, IGridFSErrorHandler errorHandler)
 {
     _stream       = stream ?? throw new ArgumentNullException(nameof(stream));
     _errorHandler = errorHandler ?? throw new ArgumentNullException(nameof(errorHandler));
 }
예제 #13
0
 public MongoReadContentProvider(GridFSBucket bucket, ObjectId id)
 {
     this.stream = bucket.OpenDownloadStream(id);
 }
예제 #14
0
 public GridFsBlob(GridFSDownloadStream <Guid> stream)
 {
     _stream   = stream;
     _fileInfo = new GridFsBlobInfo(stream.FileInfo);
 }