@@ -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 );
0 commit comments