Skip to content

Commit c2a64f4

Browse files
authored
CSHARP-5237: Remove optional MD5 support from GridFS (mongodb#1475)
1 parent 6b550db commit c2a64f4

20 files changed

+7
-360
lines changed

specifications/gridfs/tests/upload.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"length": 0,
3030
"chunkSize": 4,
3131
"uploadDate": "*actual",
32-
"md5": "d41d8cd98f00b204e9800998ecf8427e",
3332
"filename": "filename"
3433
}
3534
]
@@ -62,7 +61,6 @@
6261
"length": 1,
6362
"chunkSize": 4,
6463
"uploadDate": "*actual",
65-
"md5": "47ed733b8d10be225eceba344d533586",
6664
"filename": "filename"
6765
}
6866
]
@@ -108,7 +106,6 @@
108106
"length": 3,
109107
"chunkSize": 4,
110108
"uploadDate": "*actual",
111-
"md5": "bafae3a174ab91fc70db7a6aa50f4f52",
112109
"filename": "filename"
113110
}
114111
]
@@ -154,7 +151,6 @@
154151
"length": 4,
155152
"chunkSize": 4,
156153
"uploadDate": "*actual",
157-
"md5": "7e7c77cff5705d1f7574a25ef6662117",
158154
"filename": "filename"
159155
}
160156
]
@@ -200,7 +196,6 @@
200196
"length": 5,
201197
"chunkSize": 4,
202198
"uploadDate": "*actual",
203-
"md5": "283d4fea5dded59cf837d3047328f5af",
204199
"filename": "filename"
205200
}
206201
]
@@ -254,7 +249,6 @@
254249
"length": 8,
255250
"chunkSize": 4,
256251
"uploadDate": "*actual",
257-
"md5": "dd254cdc958e53abaa67da9f797125f5",
258252
"filename": "filename"
259253
}
260254
]
@@ -309,7 +303,6 @@
309303
"length": 1,
310304
"chunkSize": 4,
311305
"uploadDate": "*actual",
312-
"md5": "47ed733b8d10be225eceba344d533586",
313306
"filename": "filename",
314307
"contentType": "image/jpeg"
315308
}
@@ -359,7 +352,6 @@
359352
"length": 1,
360353
"chunkSize": 4,
361354
"uploadDate": "*actual",
362-
"md5": "47ed733b8d10be225eceba344d533586",
363355
"filename": "filename",
364356
"metadata": {
365357
"x": 1
@@ -394,7 +386,6 @@
394386
},
395387
"options": {
396388
"chunkSizeBytes": 4,
397-
"disableMD5": true
398389
}
399390
}
400391
},
@@ -427,7 +418,6 @@
427418
},
428419
"options": {
429420
"chunkSizeBytes": 4,
430-
"disableMD5": true
431421
}
432422
}
433423
},

src/MongoDB.Driver/GridFS/GridFSBucket.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -579,20 +579,15 @@ private BulkMixedWriteOperation CreateDeleteChunksOperation(TFileId id)
579579

580580
private GridFSDownloadStream<TFileId> CreateDownloadStream(IReadBindingHandle binding, GridFSFileInfo<TFileId> fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
581581
{
582-
var checkMD5 = options.CheckMD5 ?? false;
583582
var seekable = options.Seekable ?? false;
584-
if (checkMD5 && seekable)
585-
{
586-
throw new ArgumentException("CheckMD5 can only be used when Seekable is false.");
587-
}
588583

589584
if (seekable)
590585
{
591586
return new GridFSSeekableDownloadStream<TFileId>(this, binding, fileInfo);
592587
}
593588
else
594589
{
595-
return new GridFSForwardOnlyDownloadStream<TFileId>(this, binding, fileInfo, checkMD5);
590+
return new GridFSForwardOnlyDownloadStream<TFileId>(this, binding, fileInfo);
596591
}
597592
}
598593

@@ -744,8 +739,7 @@ private GridFSUploadStream<TFileId> CreateUploadStream(IReadWriteBindingHandle b
744739
options.Aliases,
745740
options.ContentType,
746741
chunkSizeBytes,
747-
batchSize,
748-
options.DisableMD5);
742+
batchSize);
749743
#pragma warning restore
750744
}
751745

@@ -781,10 +775,8 @@ private GridFSUploadStream<TFileId> CreateUploadStream(IReadWriteBindingHandle b
781775

782776
private void DownloadToStreamHelper(IReadBindingHandle binding, GridFSFileInfo<TFileId> fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
783777
{
784-
var checkMD5 = options.CheckMD5 ?? false;
785-
786778
var retryReads = _database.Client.Settings.RetryReads;
787-
using (var source = new GridFSForwardOnlyDownloadStream<TFileId>(this, binding.Fork(), fileInfo, checkMD5) { RetryReads = retryReads })
779+
using (var source = new GridFSForwardOnlyDownloadStream<TFileId>(this, binding.Fork(), fileInfo) { RetryReads = retryReads })
788780
{
789781
var count = source.Length;
790782
var buffer = new byte[fileInfo.ChunkSizeBytes];
@@ -802,10 +794,8 @@ private GridFSUploadStream<TFileId> CreateUploadStream(IReadWriteBindingHandle b
802794

803795
private async Task DownloadToStreamHelperAsync(IReadBindingHandle binding, GridFSFileInfo<TFileId> fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
804796
{
805-
var checkMD5 = options.CheckMD5 ?? false;
806-
807797
var retryReads = _database.Client.Settings.RetryReads;
808-
using (var source = new GridFSForwardOnlyDownloadStream<TFileId>(this, binding.Fork(), fileInfo, checkMD5) { RetryReads = retryReads })
798+
using (var source = new GridFSForwardOnlyDownloadStream<TFileId>(this, binding.Fork(), fileInfo) { RetryReads = retryReads })
809799
{
810800
var count = source.Length;
811801
var buffer = new byte[fileInfo.ChunkSizeBytes];

src/MongoDB.Driver/GridFS/GridFSBucketOptions.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class GridFSBucketOptions
2626
// fields
2727
private string _bucketName;
2828
private int _chunkSizeBytes;
29-
private bool _disableMD5 = false;
3029
private ReadConcern _readConcern;
3130
private ReadPreference _readPreference;
3231
private WriteConcern _writeConcern;
@@ -49,7 +48,6 @@ public GridFSBucketOptions(GridFSBucketOptions other)
4948
Ensure.IsNotNull(other, nameof(other));
5049
_bucketName = other.BucketName;
5150
_chunkSizeBytes = other.ChunkSizeBytes;
52-
_disableMD5 = other.DisableMD5;
5351
_readConcern = other.ReadConcern;
5452
_readPreference = other.ReadPreference;
5553
_writeConcern = other.WriteConcern;
@@ -64,7 +62,6 @@ public GridFSBucketOptions(ImmutableGridFSBucketOptions other)
6462
Ensure.IsNotNull(other, nameof(other));
6563
_bucketName = other.BucketName;
6664
_chunkSizeBytes = other.ChunkSizeBytes;
67-
_disableMD5 = other.DisableMD5;
6865
_readConcern = other.ReadConcern;
6966
_readPreference = other.ReadPreference;
7067
_writeConcern = other.WriteConcern;
@@ -103,18 +100,6 @@ public int ChunkSizeBytes
103100
}
104101
}
105102

106-
/// <summary>
107-
/// Gets or sets whether to disable MD5 checksum computation when uploading a GridFS file.
108-
/// </summary>
109-
/// <value>
110-
/// Whether MD5 checksum computation is disabled when uploading a GridFS file.
111-
/// </value>
112-
public bool DisableMD5
113-
{
114-
get { return _disableMD5; }
115-
set { _disableMD5 = value; }
116-
}
117-
118103
/// <summary>
119104
/// Gets or sets the read concern.
120105
/// </summary>
@@ -177,7 +162,6 @@ public static ImmutableGridFSBucketOptions Defaults
177162
// fields
178163
private readonly string _bucketName;
179164
private readonly int _chunkSizeBytes;
180-
private readonly bool _disableMD5 = false;
181165
private readonly ReadConcern _readConcern;
182166
private readonly ReadPreference _readPreference;
183167
private readonly WriteConcern _writeConcern;
@@ -201,7 +185,6 @@ public ImmutableGridFSBucketOptions(GridFSBucketOptions other)
201185
Ensure.IsNotNull(other, nameof(other));
202186
_bucketName = other.BucketName;
203187
_chunkSizeBytes = other.ChunkSizeBytes;
204-
_disableMD5 = other.DisableMD5;
205188
_readConcern = other.ReadConcern;
206189
_readPreference = other.ReadPreference;
207190
_writeConcern = other.WriteConcern;
@@ -230,18 +213,6 @@ public int ChunkSizeBytes
230213
get { return _chunkSizeBytes; }
231214
}
232215

233-
/// <summary>
234-
/// Gets or sets whether to disable MD5 checksum computation when uploading a GridFS file.
235-
/// </summary>
236-
/// <value>
237-
/// Whether MD5 checksum computation is disabled when uploading a GridFS file.
238-
/// </value>
239-
public bool DisableMD5
240-
{
241-
get { return _disableMD5; }
242-
}
243-
244-
245216
/// <summary>
246217
/// Gets the read concern.
247218
/// </summary>

src/MongoDB.Driver/GridFS/GridFSDownloadOptions.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,8 @@ namespace MongoDB.Driver.GridFS
2121
public class GridFSDownloadOptions
2222
{
2323
// fields
24-
private bool? _checkMD5;
2524
private bool? _seekable;
2625

27-
// properties
28-
/// <summary>
29-
/// Gets or sets a value indicating whether to check the MD5 value.
30-
/// </summary>
31-
/// <value>
32-
/// <c>true</c> if the MD5 value should be checked; otherwise, <c>false</c>.
33-
/// </value>
34-
public bool? CheckMD5
35-
{
36-
get { return _checkMD5; }
37-
set { _checkMD5 = value; }
38-
}
39-
4026
/// <summary>
4127
/// Gets or sets a value indicating whether the returned Stream supports seeking.
4228
/// </summary>

src/MongoDB.Driver/GridFS/GridFSFileInfo.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18-
using System.Linq;
1918
using MongoDB.Bson;
2019
using MongoDB.Bson.Serialization;
2120
using MongoDB.Bson.Serialization.Attributes;
22-
using MongoDB.Bson.Serialization.Serializers;
23-
using MongoDB.Driver.Core.Misc;
2421

2522
namespace MongoDB.Driver.GridFS
2623
{
@@ -123,17 +120,6 @@ public long Length
123120
get { return GetValue<long>("Length"); }
124121
}
125122

126-
/// <summary>
127-
/// Gets the MD5 checksum.
128-
/// </summary>
129-
/// <value>
130-
/// The MD5 checksum.
131-
/// </value>
132-
public string MD5
133-
{
134-
get { return GetValue<string>("MD5", null); }
135-
}
136-
137123
/// <summary>
138124
/// Gets the metadata.
139125
/// </summary>

src/MongoDB.Driver/GridFS/GridFSFileInfoCompat.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,6 @@ public long Length
132132
get { return GetValue<long>("Length"); }
133133
}
134134

135-
/// <summary>
136-
/// Gets the MD5 checksum.
137-
/// </summary>
138-
/// <value>
139-
/// The MD5 checksum.
140-
/// </value>
141-
[Obsolete("MD5 support will be removed soon.")]
142-
public string MD5
143-
{
144-
get { return GetValue<string>("MD5", null); }
145-
}
146-
147135
/// <summary>
148136
/// Gets the metadata.
149137
/// </summary>

src/MongoDB.Driver/GridFS/GridFSFileInfoSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public GridFSFileInfoSerializer(IBsonSerializer<TFileId> idSerializer)
4949
RegisterMember("Filename", "filename", new StringSerializer());
5050
RegisterMember("Id", "_id", idSerializer);
5151
RegisterMember("Length", "length", new Int64Serializer());
52-
RegisterMember("MD5", "md5", new StringSerializer());
5352
RegisterMember("Metadata", "metadata", BsonDocumentSerializer.Instance);
5453
RegisterMember("UploadDateTime", "uploadDate", new DateTimeSerializer());
5554
}

src/MongoDB.Driver/GridFS/GridFSFileInfoSerializerCompat.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public GridFSFileInfoSerializer()
4545
RegisterMember("Filename", "filename", new StringSerializer());
4646
RegisterMember("IdAsBsonValue", "_id", BsonValueSerializer.Instance);
4747
RegisterMember("Length", "length", new Int64Serializer());
48-
RegisterMember("MD5", "md5", new StringSerializer());
4948
RegisterMember("Metadata", "metadata", BsonDocumentSerializer.Instance);
5049
RegisterMember("UploadDateTime", "uploadDate", new DateTimeSerializer());
5150
}

src/MongoDB.Driver/GridFS/GridFSForwardOnlyDownloadStream.cs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ internal class GridFSForwardOnlyDownloadStream<TFileId> : GridFSDownloadStreamBa
3535
// private fields
3636
private List<BsonDocument> _batch;
3737
private long _batchPosition;
38-
private readonly bool _checkMD5;
3938
private IAsyncCursor<BsonDocument> _cursor;
4039
private bool _disposed;
4140
private readonly BsonValue _idAsBsonValue;
4241
private readonly int _lastChunkNumber;
4342
private readonly int _lastChunkSize;
44-
private readonly IncrementalHash _md5;
4543
private int _nextChunkNumber;
4644
private long _position;
4745
private bool _retryReads;
@@ -50,16 +48,9 @@ internal class GridFSForwardOnlyDownloadStream<TFileId> : GridFSDownloadStreamBa
5048
public GridFSForwardOnlyDownloadStream(
5149
GridFSBucket<TFileId> bucket,
5250
IReadBinding binding,
53-
GridFSFileInfo<TFileId> fileInfo,
54-
bool checkMD5)
51+
GridFSFileInfo<TFileId> fileInfo)
5552
: base(bucket, binding, fileInfo)
5653
{
57-
_checkMD5 = checkMD5;
58-
if (_checkMD5)
59-
{
60-
_md5 = IncrementalHash.CreateHash(HashAlgorithmName.MD5);
61-
}
62-
6354
_lastChunkNumber = (int)((fileInfo.Length - 1) / fileInfo.ChunkSizeBytes);
6455
_lastChunkSize = (int)(fileInfo.Length % fileInfo.ChunkSizeBytes);
6556

@@ -152,20 +143,6 @@ public override long Seek(long offset, SeekOrigin origin)
152143
}
153144

154145
// protected methods
155-
protected override void CloseImplementation(CancellationToken cancellationToken)
156-
{
157-
if (_checkMD5 && _position == FileInfo.Length)
158-
{
159-
var md5 = BsonUtils.ToHexString(_md5.GetHashAndReset());
160-
if (!md5.Equals(FileInfo.MD5, StringComparison.OrdinalIgnoreCase))
161-
{
162-
#pragma warning disable 618
163-
throw new GridFSMD5Exception(_idAsBsonValue);
164-
#pragma warning restore
165-
}
166-
}
167-
}
168-
169146
protected override void Dispose(bool disposing)
170147
{
171148
CloseIfNotAlreadyClosedFromDispose(disposing);
@@ -178,10 +155,6 @@ protected override void Dispose(bool disposing)
178155
{
179156
_cursor.Dispose();
180157
}
181-
if (_md5 != null)
182-
{
183-
_md5.Dispose();
184-
}
185158
}
186159

187160
_disposed = true;
@@ -303,11 +276,6 @@ private void ProcessNextBatch(List<BsonDocument> batch)
303276
throw new GridFSChunkException(_idAsBsonValue, _nextChunkNumber, "the wrong size");
304277
#pragma warning restore
305278
}
306-
307-
if (_checkMD5)
308-
{
309-
_md5.AppendData(bytes, 0, bytes.Length);
310-
}
311279
}
312280
}
313281

0 commit comments

Comments
 (0)