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 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); } } }
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); } }
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); }