public void TestUpdateMD5() { gridFS.Files.RemoveAll(); gridFS.Chunks.RemoveAll(); gridFS.Chunks.ResetIndexCache(); var fileInfo = gridFS.FindOne("test"); Assert.IsNull(fileInfo); using (var stream = gridFS.Create("test")) { var bytes = new byte[] { 1, 2, 3, 4 }; stream.Write(bytes, 0, 4); stream.UpdateMD5 = false; } fileInfo = gridFS.FindOne("test"); Assert.IsTrue(fileInfo.Exists); Assert.AreEqual(4, fileInfo.Length); Assert.IsNull(fileInfo.MD5); using (var stream = gridFS.Open("test", FileMode.Append, FileAccess.Write)) { var bytes = new byte[] { 1, 2, 3, 4 }; stream.Write(bytes, 0, 4); } fileInfo = gridFS.FindOne("test"); Assert.IsTrue(fileInfo.Exists); Assert.AreEqual(8, fileInfo.Length); Assert.IsNotNull(fileInfo.MD5); }
public static void Upload(this MongoGridFS gridFs, string remoteFilename, byte[] data) { using (Stream fileStream = gridFs.Open(remoteFilename, FileMode.Create)) { fileStream.Write(data, 0, data.Length); } }
public void TestUpdateMD5() { _gridFS.Files.RemoveAll(); _gridFS.Chunks.RemoveAll(); _gridFS.Chunks.ResetIndexCache(); var fileInfo = _gridFS.FindOne("test"); Assert.IsNull(fileInfo); using (var stream = _gridFS.Create("test")) { var bytes = new byte[] { 1, 2, 3, 4 }; stream.Write(bytes, 0, 4); #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary stream.UpdateMD5 = false; #pragma warning restore } fileInfo = _gridFS.FindOne("test"); Assert.IsTrue(fileInfo.Exists); Assert.AreEqual(4, fileInfo.Length); Assert.IsNull(fileInfo.MD5); using (var stream = _gridFS.Open("test", FileMode.Append, FileAccess.Write)) { var bytes = new byte[] { 1, 2, 3, 4 }; stream.Write(bytes, 0, 4); } fileInfo = _gridFS.FindOne("test"); Assert.IsTrue(fileInfo.Exists); Assert.AreEqual(8, fileInfo.Length); Assert.IsNotNull(fileInfo.MD5); }
public static byte[] DownloadBytes(this MongoGridFS gridFs, string remoteFilename) { using (Stream fileStream = gridFs.Open(remoteFilename, FileMode.Open)) { byte[] buffer = new byte[fileStream.Length]; int offset = 0; while (offset < buffer.Length) { int read = fileStream.Read(buffer, offset, buffer.Length - offset); if (read == 0) { throw new EndOfStreamException(); } offset += read; } return(buffer); } }