Skip to content

Commit 0552779

Browse files
authored
PHPLIB-1218 Remove deprecated fields from GridFS files (#1398)
1 parent 33dec57 commit 0552779

File tree

9 files changed

+42
-151
lines changed

9 files changed

+42
-151
lines changed

UPGRADE-2.0.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
UPGRADE FROM 1.x to 2.0
2+
========================
3+
4+
GridFS
5+
------
6+
7+
* The `md5` is no longer calculated when a file is uploaded to GridFS.
8+
Applications that require a file digest should implement it outside GridFS
9+
and store in metadata.
10+
11+
```php
12+
$hash = hash_file('sha256', $filename);
13+
$bucket->openUploadStream($fileId, ['metadata' => ['hash' => $hash]]);
14+
```
15+
16+
* The fields `contentType` and `aliases` are no longer stored in the `files`
17+
collection. Applications that require this information should store it in
18+
metadata.
19+
20+
**Before:**
21+
```php
22+
$bucket->openUploadStream($fileId, ['contentType' => 'image/png']);
23+
```
24+
25+
**After:**
26+
```php
27+
$bucket->openUploadStream($fileId, ['metadata' => ['contentType' => 'image/png']]);
28+
```

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
],
1212
"require": {
1313
"php": "^8.1",
14-
"ext-hash": "*",
1514
"ext-json": "*",
1615
"ext-mongodb": "^1.20.0",
1716
"composer-runtime-api": "^2.0",

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: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@
1717

1818
namespace MongoDB\GridFS;
1919

20-
use HashContext;
2120
use MongoDB\BSON\Binary;
2221
use MongoDB\BSON\ObjectId;
2322
use MongoDB\BSON\UTCDateTime;
2423
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2524
use MongoDB\Exception\InvalidArgumentException;
2625

2726
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;
3227
use function is_integer;
33-
use function is_string;
3428
use function MongoDB\is_document;
35-
use function MongoDB\is_string_array;
3629
use function sprintf;
3730
use function strlen;
3831
use function substr;
@@ -52,12 +45,8 @@ class WritableStream
5245

5346
private int $chunkSize;
5447

55-
private bool $disableMD5;
56-
5748
private array $file;
5849

59-
private ?HashContext $hashCtx = null;
60-
6150
private bool $isClosed = false;
6251

6352
private int $length = 0;
@@ -69,19 +58,9 @@ class WritableStream
6958
*
7059
* * _id (mixed): File document identifier. Defaults to a new ObjectId.
7160
*
72-
* * aliases (array of strings): DEPRECATED An array of aliases.
73-
* Applications wishing to store aliases should add an aliases field to
74-
* the metadata document instead.
75-
*
7661
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
7762
* 261120 (i.e. 255 KiB).
7863
*
79-
* * disableMD5 (boolean): When true, no MD5 sum will be generated.
80-
* Defaults to "false".
81-
*
82-
* * contentType (string): DEPRECATED content type to be stored with the
83-
* file. This information should now be added to the metadata.
84-
*
8564
* * metadata (document): User data for the "metadata" field of the files
8665
* collection document.
8766
*
@@ -95,13 +74,8 @@ public function __construct(private CollectionWrapper $collectionWrapper, string
9574
$options += [
9675
'_id' => new ObjectId(),
9776
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
98-
'disableMD5' => false,
9977
];
10078

101-
if (isset($options['aliases']) && ! is_string_array($options['aliases'])) {
102-
throw InvalidArgumentException::invalidType('"aliases" option', $options['aliases'], 'array of strings');
103-
}
104-
10579
if (! is_integer($options['chunkSizeBytes'])) {
10680
throw InvalidArgumentException::invalidType('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer');
10781
}
@@ -110,32 +84,18 @@ public function __construct(private CollectionWrapper $collectionWrapper, string
11084
throw new InvalidArgumentException(sprintf('Expected "chunkSizeBytes" option to be >= 1, %d given', $options['chunkSizeBytes']));
11185
}
11286

113-
if (! is_bool($options['disableMD5'])) {
114-
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
115-
}
116-
117-
if (isset($options['contentType']) && ! is_string($options['contentType'])) {
118-
throw InvalidArgumentException::invalidType('"contentType" option', $options['contentType'], 'string');
119-
}
120-
12187
if (isset($options['metadata']) && ! is_document($options['metadata'])) {
12288
throw InvalidArgumentException::expectedDocumentType('"metadata" option', $options['metadata']);
12389
}
12490

12591
$this->chunkSize = $options['chunkSizeBytes'];
126-
$this->disableMD5 = $options['disableMD5'];
127-
128-
if (! $this->disableMD5) {
129-
$this->hashCtx = hash_init('md5');
130-
}
131-
13292
$this->file = [
13393
'_id' => $options['_id'],
13494
'chunkSize' => $this->chunkSize,
13595
'filename' => $filename,
13696
'length' => null,
13797
'uploadDate' => null,
138-
] + array_intersect_key($options, ['aliases' => 1, 'contentType' => 1, 'metadata' => 1]);
98+
] + array_intersect_key($options, ['metadata' => 1]);
13999
}
140100

141101
/**
@@ -248,10 +208,6 @@ private function fileCollectionInsert(): void
248208
$this->file['length'] = $this->length;
249209
$this->file['uploadDate'] = new UTCDateTime();
250210

251-
if (! $this->disableMD5 && $this->hashCtx) {
252-
$this->file['md5'] = hash_final($this->hashCtx);
253-
}
254-
255211
try {
256212
$this->collectionWrapper->insertFile($this->file);
257213
} catch (DriverRuntimeException $e) {
@@ -276,10 +232,6 @@ private function insertChunkFromBuffer(): void
276232
'data' => new Binary($data),
277233
];
278234

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

tests/GridFS/BucketFunctionalTest.php

Lines changed: 3 additions & 35 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(),
@@ -762,46 +760,16 @@ public function testUploadingAnEmptyFile(): void
762760
[
763761
'projection' => [
764762
'length' => 1,
765-
'md5' => 1,
766763
'_id' => 0,
767764
],
768765
],
769766
);
770767

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

776770
$this->assertSameDocument($expected, $fileDocument);
777771
}
778772

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-
805773
public function testUploadingFirstFileCreatesIndexes(): void
806774
{
807775
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
@@ -863,7 +831,7 @@ public function testDanglingOpenWritableStream(): void
863831
$client = MongoDB\Tests\FunctionalTestCase::createTestClient();
864832
$database = $client->selectDatabase(getenv('MONGODB_DATABASE') ?: 'phplib_test');
865833
$gridfs = $database->selectGridFSBucket();
866-
$stream = $gridfs->openUploadStream('hello.txt', ['disableMD5' => true]);
834+
$stream = $gridfs->openUploadStream('hello.txt');
867835
fwrite($stream, 'Hello MongoDB!');
868836
PHP;
869837

@@ -970,7 +938,7 @@ public function testResolveStreamContextForWrite(): void
970938
$this->assertArrayHasKey('filename', $context);
971939
$this->assertSame('filename', $context['filename']);
972940
$this->assertArrayHasKey('options', $context);
973-
$this->assertSame(['chunkSizeBytes' => 261120, 'disableMD5' => false], $context['options']);
941+
$this->assertSame(['chunkSizeBytes' => 261120], $context['options']);
974942
}
975943

976944
/**

0 commit comments

Comments
 (0)