Skip to content

Commit f418ac2

Browse files
Will Banfieldjmikola
authored andcommitted
added 'Abort' to clean up according to spec
1 parent 6e84d2f commit f418ac2

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/GridFS/Bucket.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ private function openDownloadStreamByFile($file)
186186
'file' => $file
187187
];
188188
$context = stream_context_create(['gridfs' => $options]);
189-
//db/prefix/(filter criteria as BSON}
190-
// find criteria being MongoDB\BSON\fromPHP(['_id' => $file['_id']])
191-
// stream wrapper can explode('/', 3), which returns array of db, prefix, and BSON blob
192-
// MongoDB\BSON\toPHP(bson blob) yields find() criteria
193189
return fopen(sprintf('gridfs://%s/%s', $this->databaseName, $file->filename), 'r', false, $context);
194190
}
195191
private function findFileRevision($filename, $revision)

src/GridFS/GridFsUpload.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class GridFsUpload
2121
private $chunkSize;
2222
private $buffer;
2323
private $file;
24+
private $isClosed = false;
2425
/**
2526
* Constructs a GridFS upload stream
2627
*
@@ -123,6 +124,9 @@ public function uploadFromStream($source)
123124
*/
124125
public function insertChunks($toWrite)
125126
{
127+
if($this->isClosed){
128+
return;
129+
}
126130
$readBytes = 0;
127131
while($readBytes != strlen($toWrite)) {
128132
$addToBuffer = substr($toWrite, $readBytes, $this->chunkSize - $this->bufferLength);
@@ -144,6 +148,9 @@ public function insertChunks($toWrite)
144148
*/
145149
public function close()
146150
{
151+
if($this->isClosed){
152+
return;
153+
}
147154
rewind($this->buffer);
148155
$cached = stream_get_contents($this->buffer);
149156

@@ -152,6 +159,7 @@ public function close()
152159
}
153160
fclose($this->buffer);
154161
$this->fileCollectionInsert();
162+
$this->isClosed = true;
155163
}
156164
public function getSize()
157165
{
@@ -175,22 +183,43 @@ public function getFile()
175183
}
176184
public function isEOF()
177185
{
178-
return false;
186+
return $this->isClosed;
187+
}
188+
private function abort()
189+
{
190+
$this->collectionsWrapper->getChunksCollection()->deleteMany(["files_id"=> $this->file["_id"]]);
191+
$this->collectionsWrapper->getFilesCollection()->deleteOne(["_id"=> $this->file['_id']]);
192+
$this->isClosed = true;
179193
}
180194
private function insertChunk($data)
181195
{
196+
if($this->isClosed){
197+
return;
198+
}
182199
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->chunkOffset, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)];
183200
hash_update($this->ctx, $data);
184-
$this->collectionsWrapper->chunkInsert($toUpload);
201+
try{
202+
$this->collectionsWrapper->chunkInsert($toUpload);
203+
} catch (\MongoDB\Exception $e){
204+
$this->abort();
205+
throw $e;
206+
}
185207
$this->length += strlen($data);
186208
$this->chunkOffset++;
187209
}
188-
189210
private function fileCollectionInsert()
190211
{
212+
if($this->isClosed){
213+
return;
214+
}
191215
$md5 = hash_final($this->ctx);
192216
$this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]);
193-
$this->collectionsWrapper->fileInsert($this->file);
217+
try{
218+
$this->collectionsWrapper->fileInsert($this->file);
219+
} catch (\MongoDB\Exception $e){
220+
$this->abort();
221+
throw $e;
222+
}
194223
return $this->file['_id'];
195224
}
196225
//from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php

0 commit comments

Comments
 (0)