Skip to content

Commit 71d76b1

Browse files
committed
Merged pull request #541
2 parents baf85f4 + 331c3fb commit 71d76b1

10 files changed

+161
-7
lines changed

docs/includes/apiargs-MongoDBDatabase-method-selectGridFSBucket-option.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ interface: phpmethod
1717
operation: ~
1818
optional: true
1919
---
20+
source:
21+
file: apiargs-MongoDBGridFSBucket-common-option.yaml
22+
ref: disableMD5
23+
post: |
24+
.. versionadded: 1.4
25+
---
2026
source:
2127
file: apiargs-common-option.yaml
2228
ref: readConcern

docs/includes/apiargs-MongoDBGridFSBucket-common-option.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ operation: ~
1818
optional: true
1919
---
2020
arg_name: option
21+
name: disableMD5
22+
type: boolean
23+
description: |
24+
Whether to disable automatic MD5 generation when storing files.
25+
26+
Defaults to ``false``.
27+
interface: phpmethod
28+
operation: ~
29+
optional: true
30+
---
31+
arg_name: option
2132
name: metadata
2233
type: array|object
2334
description: |

docs/includes/apiargs-MongoDBGridFSBucket-method-construct-option.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ interface: phpmethod
1717
operation: ~
1818
optional: true
1919
---
20+
source:
21+
file: apiargs-MongoDBGridFSBucket-common-option.yaml
22+
ref: disableMD5
23+
post: |
24+
.. versionadded: 1.4
25+
---
2026
source:
2127
file: apiargs-common-option.yaml
2228
ref: readConcern

docs/includes/apiargs-MongoDBGridFSBucket-method-openUploadStream-option.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ source:
66
file: apiargs-MongoDBGridFSBucket-common-option.yaml
77
ref: chunkSizeBytes
88
---
9+
source:
10+
file: apiargs-MongoDBGridFSBucket-common-option.yaml
11+
ref: disableMD5
12+
post: |
13+
.. versionadded: 1.4
14+
---
915
source:
1016
file: apiargs-MongoDBGridFSBucket-common-option.yaml
1117
ref: metadata

docs/includes/apiargs-MongoDBGridFSBucket-method-uploadFromStream-option.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ source:
66
file: apiargs-MongoDBGridFSBucket-common-option.yaml
77
ref: chunkSizeBytes
88
---
9+
source:
10+
file: apiargs-MongoDBGridFSBucket-common-option.yaml
11+
ref: disableMD5
12+
post: |
13+
.. versionadded: 1.4
14+
---
915
source:
1016
file: apiargs-MongoDBGridFSBucket-common-option.yaml
1117
ref: metadata

src/GridFS/Bucket.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Bucket
5151
private $databaseName;
5252
private $manager;
5353
private $bucketName;
54+
private $disableMD5;
5455
private $chunkSizeBytes;
5556
private $readConcern;
5657
private $readPreference;
@@ -68,6 +69,9 @@ class Bucket
6869
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
6970
* 261120 (i.e. 255 KiB).
7071
*
72+
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
73+
* each stored file. Defaults to "false".
74+
*
7175
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
7276
*
7377
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
@@ -86,6 +90,7 @@ public function __construct(Manager $manager, $databaseName, array $options = []
8690
$options += [
8791
'bucketName' => self::$defaultBucketName,
8892
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
93+
'disableMD5' => false,
8994
];
9095

9196
if (isset($options['bucketName']) && ! is_string($options['bucketName'])) {
@@ -100,6 +105,10 @@ public function __construct(Manager $manager, $databaseName, array $options = []
100105
throw new InvalidArgumentException(sprintf('Expected "chunkSizeBytes" option to be >= 1, %d given', $options['chunkSizeBytes']));
101106
}
102107

108+
if (isset($options['disableMD5']) && ! is_bool($options['disableMD5'])) {
109+
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
110+
}
111+
103112
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
104113
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
105114
}
@@ -120,6 +129,7 @@ public function __construct(Manager $manager, $databaseName, array $options = []
120129
$this->databaseName = (string) $databaseName;
121130
$this->bucketName = $options['bucketName'];
122131
$this->chunkSizeBytes = $options['chunkSizeBytes'];
132+
$this->disableMD5 = $options['disableMD5'];
123133
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
124134
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
125135
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : self::$defaultTypeMap;
@@ -470,6 +480,9 @@ public function openDownloadStreamByName($filename, array $options = [])
470480
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to the
471481
* bucket's chunk size.
472482
*
483+
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
484+
* the stored file. Defaults to "false".
485+
*
473486
* * metadata (document): User data for the "metadata" field of the files
474487
* collection document.
475488
*
@@ -533,6 +546,9 @@ public function rename($id, $newFilename)
533546
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to the
534547
* bucket's chunk size.
535548
*
549+
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
550+
* the stored file. Defaults to "false".
551+
*
536552
* * metadata (document): User data for the "metadata" field of the files
537553
* collection document.
538554
*

src/GridFS/WritableStream.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ class WritableStream
3636
private $buffer = '';
3737
private $chunkOffset = 0;
3838
private $chunkSize;
39+
private $disableMD5;
3940
private $collectionWrapper;
40-
private $ctx;
4141
private $file;
42+
private $hashCtx;
4243
private $isClosed = false;
4344
private $length = 0;
4445

@@ -56,6 +57,9 @@ class WritableStream
5657
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
5758
* 261120 (i.e. 255 KiB).
5859
*
60+
* * disableMD5 (boolean): When true, no MD5 sum will be generated.
61+
* Defaults to "false".
62+
*
5963
* * contentType (string): DEPRECATED content type to be stored with the
6064
* file. This information should now be added to the metadata.
6165
*
@@ -72,6 +76,7 @@ public function __construct(CollectionWrapper $collectionWrapper, $filename, arr
7276
$options += [
7377
'_id' => new ObjectId,
7478
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
79+
'disableMD5' => false,
7580
];
7681

7782
if (isset($options['aliases']) && ! \MongoDB\is_string_array($options['aliases'])) {
@@ -86,6 +91,10 @@ public function __construct(CollectionWrapper $collectionWrapper, $filename, arr
8691
throw new InvalidArgumentException(sprintf('Expected "chunkSizeBytes" option to be >= 1, %d given', $options['chunkSizeBytes']));
8792
}
8893

94+
if (isset($options['disableMD5']) && ! is_bool($options['disableMD5'])) {
95+
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
96+
}
97+
8998
if (isset($options['contentType']) && ! is_string($options['contentType'])) {
9099
throw InvalidArgumentException::invalidType('"contentType" option', $options['contentType'], 'string');
91100
}
@@ -96,7 +105,11 @@ public function __construct(CollectionWrapper $collectionWrapper, $filename, arr
96105

97106
$this->chunkSize = $options['chunkSizeBytes'];
98107
$this->collectionWrapper = $collectionWrapper;
99-
$this->ctx = hash_init('md5');
108+
$this->disableMD5 = $options['disableMD5'];
109+
110+
if ( ! $this->disableMD5) {
111+
$this->hashCtx = hash_init('md5');
112+
}
100113

101114
$this->file = [
102115
'_id' => $options['_id'],
@@ -167,7 +180,7 @@ public function getSize()
167180
* written. Since seeking is not supported and writes are appended, this is
168181
* always the end of the stream.
169182
*
170-
* @see WriteableStream::getSize()
183+
* @see WritableStream::getSize()
171184
* @return integer
172185
*/
173186
public function tell()
@@ -219,12 +232,13 @@ private function abort()
219232

220233
private function fileCollectionInsert()
221234
{
222-
$md5 = hash_final($this->ctx);
223-
224235
$this->file['length'] = $this->length;
225-
$this->file['md5'] = $md5;
226236
$this->file['uploadDate'] = new UTCDateTime;
227237

238+
if ( ! $this->disableMD5) {
239+
$this->file['md5'] = hash_final($this->hashCtx);
240+
}
241+
228242
try {
229243
$this->collectionWrapper->insertFile($this->file);
230244
} catch (DriverRuntimeException $e) {
@@ -251,7 +265,9 @@ private function insertChunkFromBuffer()
251265
'data' => new Binary($data, Binary::TYPE_GENERIC),
252266
];
253267

254-
hash_update($this->ctx, $data);
268+
if ( ! $this->disableMD5) {
269+
hash_update($this->hashCtx, $data);
270+
}
255271

256272
try {
257273
$this->collectionWrapper->insertChunk($chunk);

tests/GridFS/BucketFunctionalTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function provideInvalidConstructorOptions()
5454
$options[][] = ['chunkSizeBytes' => $value];
5555
}
5656

57+
foreach ($this->getInvalidBooleanValues() as $value) {
58+
$options[][] = ['disableMD5' => $value];
59+
}
60+
5761
foreach ($this->getInvalidReadConcernValues() as $value) {
5862
$options[][] = ['readConcern' => $value];
5963
}

tests/GridFS/WritableStreamFunctionalTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public function provideInvalidConstructorOptions()
4949
$options[][] = ['chunkSizeBytes' => $value];
5050
}
5151

52+
foreach ($this->getInvalidBooleanValues() as $value) {
53+
$options[][] = ['disableMD5' => $value];
54+
}
55+
5256
foreach ($this->getInvalidDocumentValues() as $value) {
5357
$options[][] = ['metadata' => $value];
5458
}

tests/GridFS/spec-tests/upload.json

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,85 @@
382382
}
383383
]
384384
}
385+
},
386+
{
387+
"description": "Upload when length is 0 sans MD5",
388+
"act": {
389+
"operation": "upload",
390+
"arguments": {
391+
"filename": "filename",
392+
"source": {
393+
"$hex": ""
394+
},
395+
"options": {
396+
"chunkSizeBytes": 4,
397+
"disableMD5": true
398+
}
399+
}
400+
},
401+
"assert": {
402+
"result": "&result",
403+
"data": [
404+
{
405+
"insert": "expected.files",
406+
"documents": [
407+
{
408+
"_id": "*result",
409+
"length": 0,
410+
"chunkSize": 4,
411+
"uploadDate": "*actual",
412+
"filename": "filename"
413+
}
414+
]
415+
}
416+
]
417+
}
418+
},
419+
{
420+
"description": "Upload when length is 1 sans MD5",
421+
"act": {
422+
"operation": "upload",
423+
"arguments": {
424+
"filename": "filename",
425+
"source": {
426+
"$hex": "11"
427+
},
428+
"options": {
429+
"chunkSizeBytes": 4,
430+
"disableMD5": true
431+
}
432+
}
433+
},
434+
"assert": {
435+
"result": "&result",
436+
"data": [
437+
{
438+
"insert": "expected.files",
439+
"documents": [
440+
{
441+
"_id": "*result",
442+
"length": 1,
443+
"chunkSize": 4,
444+
"uploadDate": "*actual",
445+
"filename": "filename"
446+
}
447+
]
448+
},
449+
{
450+
"insert": "expected.chunks",
451+
"documents": [
452+
{
453+
"_id": "*actual",
454+
"files_id": "*result",
455+
"n": 0,
456+
"data": {
457+
"$hex": "11"
458+
}
459+
}
460+
]
461+
}
462+
]
463+
}
385464
}
386465
]
387466
}

0 commit comments

Comments
 (0)