public async Task<ObjectId> UploadFromStreamAsync(string gfsname, Stream source,string filename, MediaTypeEnum bucketName) { var bucket = new GridFSBucket(_db, new GridFSBucketOptions { BucketName = bucketName.ToString() }); var options = new GridFSUploadOptions { Metadata = new BsonDocument { { "filename", filename }, { "contentType", MimeMapping.GetMimeMapping(filename) } } }; return await bucket.UploadFromStreamAsync(gfsname, source, options); }
/// <inheritdoc /> public async Task<ObjectId> UploadFromStreamAsync(string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(filename, nameof(filename)); Ensure.IsNotNull(source, nameof(source)); options = options ?? new GridFSUploadOptions(); using (var destination = await OpenUploadStreamAsync(filename, options, cancellationToken).ConfigureAwait(false)) { var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes; var buffer = new byte[chunkSizeBytes]; while (true) { int bytesRead = 0; Exception sourceException = null; try { bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { // cannot await in the body of a catch clause sourceException = ex; } if (sourceException != null) { try { await destination.AbortAsync().ConfigureAwait(false); } catch { // ignore any exceptions because we're going to rethrow the original exception } throw sourceException; } if (bytesRead == 0) { break; } await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); } await destination.CloseAsync(cancellationToken).ConfigureAwait(false); return destination.Id; } }
/// <inheritdoc /> public async Task<ObjectId> UploadFromBytesAsync(string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(filename, nameof(filename)); Ensure.IsNotNull(source, nameof(source)); options = options ?? new GridFSUploadOptions(); using (var sourceStream = new MemoryStream(source)) { return await UploadFromStreamAsync(filename, sourceStream, options, cancellationToken).ConfigureAwait(false); } }
/// <inheritdoc /> public async Task<GridFSUploadStream> OpenUploadStreamAsync(string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(filename, nameof(filename)); options = options ?? new GridFSUploadOptions(); using (var binding = await GetSingleServerReadWriteBindingAsync(cancellationToken).ConfigureAwait(false)) { await EnsureIndexesAsync(binding, cancellationToken).ConfigureAwait(false); #pragma warning disable 618 var id = ObjectId.GenerateNewId(); var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes; var batchSize = options.BatchSize ?? (16 * 1024 * 1024 / chunkSizeBytes); return new GridFSForwardOnlyUploadStream( this, binding.Fork(), id, filename, options.Metadata, options.Aliases, options.ContentType, chunkSizeBytes, batchSize); #pragma warning restore } }
public static bool AddSoundToGridFS(FileStream stream,string imePesme,string format) { try { var client = new MongoClient("mongodb://localhost"); var database = client.GetDatabase("docs"); var fs = new GridFSBucket(database); GridFSUploadOptions opcije = new GridFSUploadOptions(); opcije.ContentType = "audio/"+format; opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length) / 4; int duzina = Convert.ToInt32(stream.Length); byte[] bajtovi = new byte[duzina]; stream.Seek(0, SeekOrigin.Begin); int bytesRead = stream.Read(bajtovi, 0, duzina); fs.UploadFromBytes(imePesme, bajtovi, opcije); return true; } catch(Exception ex) { MessageBox.Show(ex.Message); return false; } }
/// <inheritdoc /> public async Task UploadFromBytesAsync(TFileId id, string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull((object)id, nameof(id)); Ensure.IsNotNull(filename, nameof(filename)); Ensure.IsNotNull(source, nameof(source)); options = options ?? new GridFSUploadOptions(); using (var sourceStream = new MemoryStream(source)) { await UploadFromStreamAsync(id, filename, sourceStream, options, cancellationToken).ConfigureAwait(false); } }
public static bool AddImageToGridFS(Image slika,string imeSlike,string format) { try { //provera da li postoji slika //var client = new MongoClient("mongodb://localhost"); //var database = client.GetDatabase("docs"); //var fs = new GridFSBucket(database); //var test = fs.DownloadAsBytesByName(imeSlike); byte[] data; MemoryStream stream = new MemoryStream(); slika.Save(stream, slika.RawFormat); data = stream.ToArray(); //string host = "localhost"; //int port = 27017; //string databaseName = "docs"; //var _client = new MongoClient(); // var _database = (MongoDatabase)_client.GetDatabase("docs"); var client = new MongoClient("mongodb://localhost"); var database = client.GetDatabase("docs"); var fs = new GridFSBucket(database); GridFSUploadOptions opcije = new GridFSUploadOptions(); opcije.ContentType = "image/"+format; opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length) / 4; fs.UploadFromBytes(imeSlike, data, opcije); //var grid = new MongoGridFS(new MongoServer(new MongoServerSettings { Server = new MongoServerAddress(host, port) }), databaseName, new MongoGridFSSettings()); // grid.Upload(m, imeSlike, new MongoGridFSCreateOptions //{ //Id = 1, // ContentType = "image/"+format // }); return true; } catch(Exception ex) { MessageBox.Show(ex.Message); return false; } }
/// <inheritdoc /> public ObjectId UploadFromBytes(string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(filename, nameof(filename)); Ensure.IsNotNull(source, nameof(source)); options = options ?? new GridFSUploadOptions(); using (var sourceStream = new MemoryStream(source)) { return UploadFromStream(filename, sourceStream, options, cancellationToken); } }
/// <inheritdoc /> public ObjectId UploadFromStream(string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(filename, nameof(filename)); Ensure.IsNotNull(source, nameof(source)); options = options ?? new GridFSUploadOptions(); using (var destination = OpenUploadStream(filename, options, cancellationToken)) { var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes; var buffer = new byte[chunkSizeBytes]; while (true) { int bytesRead = 0; try { bytesRead = source.Read(buffer, 0, buffer.Length); } catch { try { destination.Abort(); } catch { // ignore any exceptions because we're going to rethrow the original exception } throw; } if (bytesRead == 0) { break; } destination.Write(buffer, 0, bytesRead); } destination.Close(cancellationToken); return destination.Id; } }
/// <inheritdoc /> public GridFSUploadStream OpenUploadStream(string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(filename, nameof(filename)); options = options ?? new GridFSUploadOptions(); using (var binding = GetSingleServerReadWriteBinding(cancellationToken)) { EnsureIndexes(binding, cancellationToken); return CreateUploadStream(binding, filename, options); } }
/// <inheritdoc /> public async Task<GridFSUploadStream> OpenUploadStreamAsync(string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(filename, nameof(filename)); options = options ?? new GridFSUploadOptions(); using (var binding = await GetSingleServerReadWriteBindingAsync(cancellationToken).ConfigureAwait(false)) { await EnsureIndexesAsync(binding, cancellationToken).ConfigureAwait(false); return CreateUploadStream(binding, filename, options); } }
/// <inheritdoc /> public GridFSUploadStream <TFileId> OpenUploadStream(TFileId id, string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull((object)id, nameof(id)); Ensure.IsNotNull(filename, nameof(filename)); options = options ?? new GridFSUploadOptions(); using (var binding = GetSingleServerReadWriteBinding(cancellationToken)) { EnsureIndexes(binding, cancellationToken); return(CreateUploadStream(binding, id, filename, options)); } }
private GridFSUploadStream <TFileId> CreateUploadStream(IReadWriteBindingHandle binding, TFileId id, string filename, GridFSUploadOptions options) { #pragma warning disable 618 var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes; var batchSize = options.BatchSize ?? (16 * 1024 * 1024 / chunkSizeBytes); return(new GridFSForwardOnlyUploadStream <TFileId>( this, binding.Fork(), id, filename, options.Metadata, options.Aliases, options.ContentType, chunkSizeBytes, batchSize)); #pragma warning restore }
/// <inheritdoc /> public async Task UploadFromStreamAsync(TFileId id, string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull((object)id, nameof(id)); Ensure.IsNotNull(filename, nameof(filename)); Ensure.IsNotNull(source, nameof(source)); options = options ?? new GridFSUploadOptions(); using (var destination = await OpenUploadStreamAsync(id, filename, options, cancellationToken).ConfigureAwait(false)) { var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes; var buffer = new byte[chunkSizeBytes]; while (true) { int bytesRead = 0; Exception sourceException = null; try { bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { // cannot await in the body of a catch clause sourceException = ex; } if (sourceException != null) { try { await destination.AbortAsync().ConfigureAwait(false); } catch { // ignore any exceptions because we're going to rethrow the original exception } throw sourceException; } if (bytesRead == 0) { break; } await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); } await destination.CloseAsync(cancellationToken).ConfigureAwait(false); } }
private void button8_Click(object sender, EventArgs e) { Image slika; FileStream stream; var client = new MongoClient("mongodb://localhost"); var database = client.GetDatabase("docs"); var fs = new GridFSBucket(database); OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { stream = new System.IO.FileStream(ofd.FileName, FileMode.Open, FileAccess.Read); slika = Image.FromStream(stream); GridFSUploadOptions opcije = new GridFSUploadOptions(); opcije.ContentType = "image/jpg"; opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length); int duzina = Convert.ToInt32(stream.Length); byte[] bajtovi = new byte[duzina]; stream.Seek(0, SeekOrigin.Begin); int bytesRead = stream.Read(bajtovi, 0, duzina); fs.UploadFromBytes("Test", bajtovi, opcije); } MessageBox.Show("done"); }
private GridFSUploadStream CreateUploadStream(IReadWriteBindingHandle binding, string filename, GridFSUploadOptions options) { #pragma warning disable 618 var id = ObjectId.GenerateNewId(); var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes; var batchSize = options.BatchSize ?? (16 * 1024 * 1024 / chunkSizeBytes); return new GridFSForwardOnlyUploadStream( this, binding.Fork(), id, filename, options.Metadata, options.Aliases, options.ContentType, chunkSizeBytes, batchSize); #pragma warning restore }
private void button9_Click(object sender, EventArgs e) { FileStream stream; OpenFileDialog ofd = new OpenFileDialog(); var client = new MongoClient("mongodb://localhost"); var database = client.GetDatabase("docs"); var fs = new GridFSBucket(database); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { stream = new System.IO.FileStream(ofd.FileName, FileMode.Open, FileAccess.Read); //SoundPlayer simpleSound = new SoundPlayer(stream); //simpleSound.Play(); var split = ofd.SafeFileName.Split('.'); if(split[1] != "mp3") { MessageBox.Show("Izaberite mp3 fajl!"); return; } GridFSUploadOptions opcije = new GridFSUploadOptions(); opcije.ContentType = "audio/mp3"; opcije.ChunkSizeBytes = Convert.ToInt32(stream.Length)/4; int duzina = Convert.ToInt32(stream.Length); byte[] bajtovi = new byte[duzina]; stream.Seek(0, SeekOrigin.Begin); int bytesRead = stream.Read(bajtovi, 0, duzina); fs.UploadFromBytes(ofd.SafeFileName, bajtovi, opcije); } }
/// <inheritdoc /> public async Task <GridFSUploadStream <TFileId> > OpenUploadStreamAsync(TFileId id, string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull((object)id, nameof(id)); Ensure.IsNotNull(filename, nameof(filename)); options = options ?? new GridFSUploadOptions(); using (var binding = await GetSingleServerReadWriteBindingAsync(cancellationToken).ConfigureAwait(false)) { await EnsureIndexesAsync(binding, cancellationToken).ConfigureAwait(false); return(CreateUploadStream(binding, id, filename, options)); } }