Skip to content

Commit fd9ccf7

Browse files
author
Will Banfield
committed
more tweaks
1 parent 5d5ed35 commit fd9ccf7

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

src/GridFS/Bucket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function getChunkSizeBytes()
132132
}
133133
public function getDatabaseName()
134134
{
135-
return $this->options['chunkSizeBytes'];
135+
return $this->databaseName;
136136
}
137137
public function find($filter, array $options =[])
138138
{

src/GridFS/BucketReadWriter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function openUploadStream($filename, array $options = [])
2020
{
2121
$options = [
2222
'bucket' => $this->bucket,
23+
'uploadOptions' => $options
2324
];
2425
$context = stream_context_create(['gridfs' => $options]);
2526
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $filename), 'w', false, $context);

src/GridFS/GridFsUpload.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use MongoDB\Exception\RuntimeException;
66
use MongoDB\Exception\UnexpectedTypeException;
77
use MongoDB\Exception\InvalidArgumentException;
8-
use MongoDB\BSON\ObjectId;
8+
use MongoDB\BSON;
99
/**
1010
* GridFsupload abstracts the processes of inserting into a GridFSBucket
1111
*
@@ -16,6 +16,7 @@ class GridFsUpload extends GridFsStream
1616
private $ctx;
1717
private $bufferLength;
1818
private $indexChecker;
19+
private $length;
1920
/**
2021
* Constructs a GridFS upload stream
2122
*
@@ -142,9 +143,7 @@ public function close()
142143
$cached = fread($this->buffer, $this->bucket->getChunkSizeBytes());
143144

144145
if(strlen($cached) > 0) {
145-
$toUpload = ["files_id" => $this->file["_id"], "n" => $this->n, "data" => $cached];
146-
$this->bucket->chunkInsert($toUpload);
147-
$this->n++;
146+
insertChunk($cached);
148147
}
149148

150149
fclose($this->buffer);
@@ -153,16 +152,17 @@ public function close()
153152
}
154153
private function insertChunk($data)
155154
{
156-
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->n, "data" => $data];
155+
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->n, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)];
157156
hash_update($this->ctx, $data);
158157
$this->bucket->chunkInsert($toUpload);
158+
$this->length += strlen($data);
159159
$this->n++;
160160
}
161161

162162
private function fileCollectionInsert()
163163
{
164164
$md5 = hash_final($this->ctx);
165-
$this->file = array_merge($this->file, ['length' => $this->n, 'md5' => $md5]);
165+
$this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]);
166166
$this->bucket->fileInsert($this->file);
167167
return $this->file['_id'];
168168
}

src/GridFS/StreamWrapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public function stream_open($path, $mode, $options, &$openedPath)
101101
}
102102

103103
public function openWriteStream() {
104-
$this->gridFsStream = new GridFsUpload($this->bucket, $this->identifier, []);
104+
$context = stream_context_get_options($this->context);
105+
$options =$context['gridfs']['uploadOptions'];
106+
$this->gridFsStream = new GridFsUpload($this->bucket, $this->identifier, $options);
105107
return true;
106108
}
107109

0 commit comments

Comments
 (0)