Skip to content

Commit ae82012

Browse files
author
Will Banfield
committed
redo the way chunks are buffered
1 parent e862316 commit ae82012

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/GridFS/Bucket.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Bucket
4545
*/
4646
public function __construct(Manager $manager, $databaseName, array $options = [])
4747
{
48-
$collectionOptions = array();
48+
$collectionOptions = [];
4949
$options += [
5050
'bucketName' => 'fs',
5151
'chunkSizeBytes' => 261120,
@@ -145,7 +145,7 @@ public function openUploadStream($filename, array $options = [])
145145
'chunkSizeBytes' => $this->getChunkSizeBytes()
146146
];
147147
$context = stream_context_create(['gridfs' => $options]);
148-
return fopen('gridfs://$this->databaseName/$filename', 'w', false, $context);
148+
return fopen(sprintf('gridfs://%s/%s', $this->databaseName, $filename), 'w', false, $context);
149149
}
150150
/**
151151
* Upload a file to this bucket by specifying the source stream file
@@ -202,7 +202,7 @@ private function isFilesCollectionEmpty()
202202
}
203203
public function delete(ObjectId $id)
204204
{
205-
$options = array('writeConcern' => $this->writeConcern);
205+
$options = ['writeConcern' => $this->writeConcern];
206206
$this->chunksCollection->deleteMany(['file_id' => $id], $options);
207207

208208
$this->filesCollection->deleteOne(['_id' => $id], $options);

src/GridFS/GridFsUpload.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use MongoDB\Collection;
55
use MongoDB\Exception\RuntimeException;
6+
use MongoDB\Exception\UnexpectedTypeException;
7+
use MongoDB\Exception\InvalidArgumentException;
68
use MongoDB\BSON\ObjectId;
79
/**
810
* GridFsupload abstracts the processes of inserting into a GridFSBucket
@@ -12,6 +14,7 @@
1214
class GridFsUpload extends GridFsStream
1315
{
1416
private $ctx;
17+
private $bufferLength;
1518
/**
1619
* Constructs a GridFS upload stream
1720
*
@@ -44,6 +47,7 @@ public function __construct(
4447
array $options=[]
4548
)
4649
{
50+
$this->bufferLength = 0;
4751
$this->ctx = hash_init('md5');
4852
$uploadDate = time();
4953
$objectId = new \MongoDB\BSON\ObjectId();
@@ -77,7 +81,6 @@ public function __construct(
7781
throw new InvalidArgumentTypeException('"metadata" option', $options['metadata'], 'object or array');
7882
}
7983
}
80-
8184
$this->file = array_merge($main_file, $fileOptions);
8285
parent::__construct($filesCollection, $chunksCollection, $chunkSizeBytes);
8386
}
@@ -89,6 +92,14 @@ public function __construct(
8992
*/
9093
public function uploadFromStream($source)
9194
{
95+
if (!is_resource($source) || get_resource_type($source) != "stream") {
96+
throw new UnexpectedTypeException('stream', $source);
97+
} else{
98+
$streamMetadata = stream_get_meta_data($source);
99+
} if (!is_readable($streamMetadata['uri'])) {
100+
// throw new InvalidArgumentException("stream not readable");
101+
//issue being that php's is_readable reports native streams as not readable like php://temp
102+
}
92103
while ($data = fread($source, $this->chunkSizeBytes)) {
93104
$this->insertChunk($data);
94105
}
@@ -102,6 +113,20 @@ public function uploadFromStream($source)
102113
*/
103114
public function insertChunks($toWrite)
104115
{
116+
$readBytes = 0;
117+
118+
while($readBytes != strlen($toWrite)) {
119+
$addToBuffer = substr($toWrite, $readBytes, $this->chunkSizeBytes - $this->bufferLength);
120+
fwrite($this->buffer, $addToBuffer);
121+
$readBytes += strlen($addToBuffer);
122+
$this->bufferLength += strlen($addToBuffer);
123+
if($this->bufferLength == $this->chunkSizeBytes) {
124+
$this->insertChunk(fread($this->buffer, $this->chunkSizeBytes));
125+
ftruncate($this->buffer,0);
126+
$this->bufferLength = 0;
127+
}
128+
}
129+
/*
105130
rewind($this->buffer);
106131
$cached = fread($this->buffer, $this->chunkSizeBytes);
107132
$toWrite = $cached . $toWrite;
@@ -117,7 +142,8 @@ public function insertChunks($toWrite)
117142
}
118143
$this->insertChunk($bytes[$i]);
119144
}
120-
return strlen($toWrite);
145+
*/
146+
return $readBytes;
121147
}
122148
/**
123149
* Close an active stream, pushes all buffered data to GridFS
@@ -138,14 +164,6 @@ public function close()
138164

139165
$this->fileCollectionInsert();
140166
}
141-
/*
142-
private function insertChunk($data)
143-
{
144-
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->n, "data" => $data];
145-
hash_update($this->ctx, $data);
146-
$this->chunksCollection->insertOne($toUpload);
147-
$this->n++;
148-
} */
149167
private function insertChunk($data)
150168
{
151169
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->n, "data" => $data];

0 commit comments

Comments
 (0)