Skip to content

Commit 0fe0d1f

Browse files
committed
PHPLIB-1218 Remove derecated "md5" field from GridFS files
1 parent 6fd6307 commit 0fe0d1f

File tree

6 files changed

+9
-128
lines changed

6 files changed

+9
-128
lines changed

src/GridFS/Bucket.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use function get_resource_type;
4646
use function in_array;
4747
use function is_array;
48-
use function is_bool;
4948
use function is_integer;
5049
use function is_object;
5150
use function is_resource;
@@ -59,11 +58,8 @@
5958
use function stream_copy_to_stream;
6059
use function stream_get_meta_data;
6160
use function stream_get_wrappers;
62-
use function trigger_error;
6361
use function urlencode;
6462

65-
use const E_USER_DEPRECATED;
66-
6763
/**
6864
* Bucket provides a public API for interacting with the GridFS files and chunks
6965
* collections.
@@ -88,8 +84,6 @@ class Bucket
8884

8985
private string $bucketName;
9086

91-
private bool $disableMD5;
92-
9387
private int $chunkSizeBytes;
9488

9589
private ReadConcern $readConcern;
@@ -111,9 +105,6 @@ class Bucket
111105
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
112106
* 261120 (i.e. 255 KiB).
113107
*
114-
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
115-
* each stored file. Defaults to "false".
116-
*
117108
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
118109
*
119110
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
@@ -129,14 +120,9 @@ class Bucket
129120
*/
130121
public function __construct(private Manager $manager, private string $databaseName, array $options = [])
131122
{
132-
if (isset($options['disableMD5']) && $options['disableMD5'] === false) {
133-
@trigger_error('Setting GridFS "disableMD5" option to "false" is deprecated since mongodb/mongodb 1.18 and will not be supported in version 2.0.', E_USER_DEPRECATED);
134-
}
135-
136123
$options += [
137124
'bucketName' => self::DEFAULT_BUCKET_NAME,
138125
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
139-
'disableMD5' => false,
140126
];
141127

142128
if (! is_string($options['bucketName'])) {
@@ -155,10 +141,6 @@ public function __construct(private Manager $manager, private string $databaseNa
155141
throw InvalidArgumentException::invalidType('"codec" option', $options['codec'], DocumentCodec::class);
156142
}
157143

158-
if (! is_bool($options['disableMD5'])) {
159-
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
160-
}
161-
162144
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
163145
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
164146
}
@@ -182,7 +164,6 @@ public function __construct(private Manager $manager, private string $databaseNa
182164
$this->bucketName = $options['bucketName'];
183165
$this->chunkSizeBytes = $options['chunkSizeBytes'];
184166
$this->codec = $options['codec'] ?? null;
185-
$this->disableMD5 = $options['disableMD5'];
186167
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
187168
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
188169
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
@@ -211,7 +192,6 @@ public function __debugInfo()
211192
'bucketName' => $this->bucketName,
212193
'codec' => $this->codec,
213194
'databaseName' => $this->databaseName,
214-
'disableMD5' => $this->disableMD5,
215195
'manager' => $this->manager,
216196
'chunkSizeBytes' => $this->chunkSizeBytes,
217197
'readConcern' => $this->readConcern,
@@ -565,9 +545,6 @@ public function openDownloadStreamByName(string $filename, array $options = [])
565545
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to the
566546
* bucket's chunk size.
567547
*
568-
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
569-
* the stored file. Defaults to "false".
570-
*
571548
* * metadata (document): User data for the "metadata" field of the files
572549
* collection document.
573550
*
@@ -579,7 +556,6 @@ public function openUploadStream(string $filename, array $options = [])
579556
{
580557
$options += [
581558
'chunkSizeBytes' => $this->chunkSizeBytes,
582-
'disableMD5' => $this->disableMD5,
583559
];
584560

585561
$path = $this->createPathForUpload();
@@ -658,9 +634,6 @@ public function rename(mixed $id, string $newFilename)
658634
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to the
659635
* bucket's chunk size.
660636
*
661-
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
662-
* the stored file. Defaults to "false".
663-
*
664637
* * metadata (document): User data for the "metadata" field of the files
665638
* collection document.
666639
*
@@ -792,9 +765,9 @@ private function registerStreamWrapper(): void
792765
*
793766
* @see StreamWrapper::setContextResolver()
794767
*
795-
* @param string $path The full url provided to fopen(). It contains the filename.
796-
* gridfs://database_name/collection_name.files/file_name
797-
* @param array{revision?: int, chunkSizeBytes?: int, disableMD5?: bool} $context The options provided to fopen()
768+
* @param string $path The full url provided to fopen(). It contains the filename.
769+
* gridfs://database_name/collection_name.files/file_name
770+
* @param array{revision?: int, chunkSizeBytes?: int} $context The options provided to fopen()
798771
*
799772
* @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}
800773
*
@@ -825,7 +798,6 @@ private function resolveStreamContext(string $path, string $mode, array $context
825798
'filename' => $filename,
826799
'options' => $context + [
827800
'chunkSizeBytes' => $this->chunkSizeBytes,
828-
'disableMD5' => $this->disableMD5,
829801
],
830802
];
831803
}

src/GridFS/WritableStream.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525
use MongoDB\Exception\InvalidArgumentException;
2626

2727
use function array_intersect_key;
28-
use function hash_final;
29-
use function hash_init;
30-
use function hash_update;
31-
use function is_bool;
3228
use function is_integer;
3329
use function is_string;
3430
use function MongoDB\is_document;
@@ -52,8 +48,6 @@ class WritableStream
5248

5349
private int $chunkSize;
5450

55-
private bool $disableMD5;
56-
5751
private array $file;
5852

5953
private ?HashContext $hashCtx = null;
@@ -76,9 +70,6 @@ class WritableStream
7670
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
7771
* 261120 (i.e. 255 KiB).
7872
*
79-
* * disableMD5 (boolean): When true, no MD5 sum will be generated.
80-
* Defaults to "false".
81-
*
8273
* * contentType (string): DEPRECATED content type to be stored with the
8374
* file. This information should now be added to the metadata.
8475
*
@@ -95,7 +86,6 @@ public function __construct(private CollectionWrapper $collectionWrapper, string
9586
$options += [
9687
'_id' => new ObjectId(),
9788
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
98-
'disableMD5' => false,
9989
];
10090

10191
if (isset($options['aliases']) && ! is_string_array($options['aliases'])) {
@@ -110,10 +100,6 @@ public function __construct(private CollectionWrapper $collectionWrapper, string
110100
throw new InvalidArgumentException(sprintf('Expected "chunkSizeBytes" option to be >= 1, %d given', $options['chunkSizeBytes']));
111101
}
112102

113-
if (! is_bool($options['disableMD5'])) {
114-
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
115-
}
116-
117103
if (isset($options['contentType']) && ! is_string($options['contentType'])) {
118104
throw InvalidArgumentException::invalidType('"contentType" option', $options['contentType'], 'string');
119105
}
@@ -123,12 +109,6 @@ public function __construct(private CollectionWrapper $collectionWrapper, string
123109
}
124110

125111
$this->chunkSize = $options['chunkSizeBytes'];
126-
$this->disableMD5 = $options['disableMD5'];
127-
128-
if (! $this->disableMD5) {
129-
$this->hashCtx = hash_init('md5');
130-
}
131-
132112
$this->file = [
133113
'_id' => $options['_id'],
134114
'chunkSize' => $this->chunkSize,
@@ -248,10 +228,6 @@ private function fileCollectionInsert(): void
248228
$this->file['length'] = $this->length;
249229
$this->file['uploadDate'] = new UTCDateTime();
250230

251-
if (! $this->disableMD5 && $this->hashCtx) {
252-
$this->file['md5'] = hash_final($this->hashCtx);
253-
}
254-
255231
try {
256232
$this->collectionWrapper->insertFile($this->file);
257233
} catch (DriverRuntimeException $e) {
@@ -276,10 +252,6 @@ private function insertChunkFromBuffer(): void
276252
'data' => new Binary($data),
277253
];
278254

279-
if (! $this->disableMD5 && $this->hashCtx) {
280-
hash_update($this->hashCtx, $data);
281-
}
282-
283255
try {
284256
$this->collectionWrapper->insertChunk($chunk);
285257
} catch (DriverRuntimeException $e) {

tests/GridFS/BucketFunctionalTest.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function testValidConstructorOptions(): void
6060
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
6161
'readPreference' => new ReadPreference(ReadPreference::PRIMARY),
6262
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY, 1000),
63-
'disableMD5' => true,
6463
]);
6564
}
6665

@@ -77,7 +76,6 @@ public static function provideInvalidConstructorOptions()
7776
'bucketName' => self::getInvalidStringValues(true),
7877
'chunkSizeBytes' => self::getInvalidIntegerValues(true),
7978
'codec' => self::getInvalidDocumentCodecValues(),
80-
'disableMD5' => self::getInvalidBooleanValues(true),
8179
'readConcern' => self::getInvalidReadConcernValues(),
8280
'readPreference' => self::getInvalidReadPreferenceValues(),
8381
'typeMap' => self::getInvalidArrayValues(),
@@ -768,40 +766,11 @@ public function testUploadingAnEmptyFile(): void
768766
],
769767
);
770768

771-
$expected = [
772-
'length' => 0,
773-
'md5' => 'd41d8cd98f00b204e9800998ecf8427e',
774-
];
769+
$expected = ['length' => 0];
775770

776771
$this->assertSameDocument($expected, $fileDocument);
777772
}
778773

779-
public function testDisableMD5(): void
780-
{
781-
$options = ['disableMD5' => true];
782-
$id = $this->bucket->uploadFromStream('filename', self::createStream('data'), $options);
783-
784-
$fileDocument = $this->filesCollection->findOne(
785-
['_id' => $id],
786-
);
787-
788-
$this->assertArrayNotHasKey('md5', $fileDocument);
789-
}
790-
791-
public function testDisableMD5OptionInConstructor(): void
792-
{
793-
$options = ['disableMD5' => true];
794-
795-
$this->bucket = new Bucket($this->manager, $this->getDatabaseName(), $options);
796-
$id = $this->bucket->uploadFromStream('filename', self::createStream('data'));
797-
798-
$fileDocument = $this->filesCollection->findOne(
799-
['_id' => $id],
800-
);
801-
802-
$this->assertArrayNotHasKey('md5', $fileDocument);
803-
}
804-
805774
public function testUploadingFirstFileCreatesIndexes(): void
806775
{
807776
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
@@ -863,7 +832,7 @@ public function testDanglingOpenWritableStream(): void
863832
$client = MongoDB\Tests\FunctionalTestCase::createTestClient();
864833
$database = $client->selectDatabase(getenv('MONGODB_DATABASE') ?: 'phplib_test');
865834
$gridfs = $database->selectGridFSBucket();
866-
$stream = $gridfs->openUploadStream('hello.txt', ['disableMD5' => true]);
835+
$stream = $gridfs->openUploadStream('hello.txt');
867836
fwrite($stream, 'Hello MongoDB!');
868837
PHP;
869838

@@ -970,7 +939,7 @@ public function testResolveStreamContextForWrite(): void
970939
$this->assertArrayHasKey('filename', $context);
971940
$this->assertSame('filename', $context['filename']);
972941
$this->assertArrayHasKey('options', $context);
973-
$this->assertSame(['chunkSizeBytes' => 261120, 'disableMD5' => false], $context['options']);
942+
$this->assertSame(['chunkSizeBytes' => 261120], $context['options']);
974943
}
975944

976945
/**

tests/GridFS/WritableStreamFunctionalTest.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public static function provideInvalidConstructorOptions()
4343
{
4444
return self::createOptionDataProvider([
4545
'chunkSizeBytes' => self::getInvalidIntegerValues(true),
46-
'disableMD5' => self::getInvalidBooleanValues(true),
4746
'metadata' => self::getInvalidDocumentValues(),
4847
]);
4948
}
@@ -70,31 +69,4 @@ public function testWriteBytesAlwaysUpdatesFileSize(): void
7069
$stream->close();
7170
$this->assertSame(1536, $stream->getSize());
7271
}
73-
74-
/** @dataProvider provideInputDataAndExpectedMD5 */
75-
public function testWriteBytesCalculatesMD5($input, $expectedMD5): void
76-
{
77-
$stream = new WritableStream($this->collectionWrapper, 'filename');
78-
$stream->writeBytes($input);
79-
$stream->close();
80-
81-
$fileDocument = $this->filesCollection->findOne(
82-
['_id' => $stream->getFile()->_id],
83-
['projection' => ['md5' => 1, '_id' => 0]],
84-
);
85-
86-
$this->assertSameDocument(['md5' => $expectedMD5], $fileDocument);
87-
}
88-
89-
public static function provideInputDataAndExpectedMD5()
90-
{
91-
return [
92-
['', 'd41d8cd98f00b204e9800998ecf8427e'],
93-
['foobar', '3858f62230ac3c915f300c664312c63f'],
94-
[str_repeat('foobar', 43520), '88ff0e5fcb0acb27947d736b5d69cb73'],
95-
[str_repeat('foobar', 43521), '8ff86511c95a06a611842ceb555d8454'],
96-
[str_repeat('foobar', 87040), '45bfa1a9ec36728ee7338d15c5a30c13'],
97-
[str_repeat('foobar', 87041), '95e78f624f8e745bcfd2d11691fa601e'],
98-
];
99-
}
10072
}

tests/UnifiedSpecTests/Context.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ private static function prepareCollectionOrDatabaseOptions(array $options): arra
497497

498498
private static function prepareBucketOptions(array $options): array
499499
{
500-
Util::assertHasOnlyKeys($options, ['bucketName', 'chunkSizeBytes', 'disableMD5', 'readConcern', 'readPreference', 'writeConcern']);
500+
Util::assertHasOnlyKeys($options, ['bucketName', 'chunkSizeBytes', 'readConcern', 'readPreference', 'writeConcern']);
501501

502502
if (array_key_exists('bucketName', $options)) {
503503
assertIsString($options['bucketName']);
@@ -507,10 +507,6 @@ private static function prepareBucketOptions(array $options): array
507507
assertIsInt($options['chunkSizeBytes']);
508508
}
509509

510-
if (array_key_exists('disableMD5', $options)) {
511-
assertIsBool($options['disableMD5']);
512-
}
513-
514510
return Util::prepareCommonOptions($options);
515511
}
516512

tests/UnifiedSpecTests/Util.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ final class Util
134134
'delete' => ['id'],
135135
'downloadByName' => ['filename', 'revision'],
136136
'download' => ['id'],
137-
'uploadWithId' => ['id', 'filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
138-
'upload' => ['filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
137+
'uploadWithId' => ['id', 'filename', 'source', 'chunkSizeBytes', 'contentType', 'metadata'],
138+
'upload' => ['filename', 'source', 'chunkSizeBytes', 'contentType', 'metadata'],
139139
],
140140
];
141141

0 commit comments

Comments
 (0)