Commit f418ac21 authored by Will Banfield's avatar Will Banfield Committed by Jeremy Mikola

added 'Abort' to clean up according to spec

parent 6e84d2ff
...@@ -186,10 +186,6 @@ class Bucket ...@@ -186,10 +186,6 @@ class Bucket
'file' => $file 'file' => $file
]; ];
$context = stream_context_create(['gridfs' => $options]); $context = stream_context_create(['gridfs' => $options]);
//db/prefix/(filter criteria as BSON}
// find criteria being MongoDB\BSON\fromPHP(['_id' => $file['_id']])
// stream wrapper can explode('/', 3), which returns array of db, prefix, and BSON blob
// MongoDB\BSON\toPHP(bson blob) yields find() criteria
return fopen(sprintf('gridfs://%s/%s', $this->databaseName, $file->filename), 'r', false, $context); return fopen(sprintf('gridfs://%s/%s', $this->databaseName, $file->filename), 'r', false, $context);
} }
private function findFileRevision($filename, $revision) private function findFileRevision($filename, $revision)
......
...@@ -21,6 +21,7 @@ class GridFsUpload ...@@ -21,6 +21,7 @@ class GridFsUpload
private $chunkSize; private $chunkSize;
private $buffer; private $buffer;
private $file; private $file;
private $isClosed = false;
/** /**
* Constructs a GridFS upload stream * Constructs a GridFS upload stream
* *
...@@ -123,6 +124,9 @@ class GridFsUpload ...@@ -123,6 +124,9 @@ class GridFsUpload
*/ */
public function insertChunks($toWrite) public function insertChunks($toWrite)
{ {
if($this->isClosed){
return;
}
$readBytes = 0; $readBytes = 0;
while($readBytes != strlen($toWrite)) { while($readBytes != strlen($toWrite)) {
$addToBuffer = substr($toWrite, $readBytes, $this->chunkSize - $this->bufferLength); $addToBuffer = substr($toWrite, $readBytes, $this->chunkSize - $this->bufferLength);
...@@ -144,6 +148,9 @@ class GridFsUpload ...@@ -144,6 +148,9 @@ class GridFsUpload
*/ */
public function close() public function close()
{ {
if($this->isClosed){
return;
}
rewind($this->buffer); rewind($this->buffer);
$cached = stream_get_contents($this->buffer); $cached = stream_get_contents($this->buffer);
...@@ -152,6 +159,7 @@ class GridFsUpload ...@@ -152,6 +159,7 @@ class GridFsUpload
} }
fclose($this->buffer); fclose($this->buffer);
$this->fileCollectionInsert(); $this->fileCollectionInsert();
$this->isClosed = true;
} }
public function getSize() public function getSize()
{ {
...@@ -175,22 +183,43 @@ class GridFsUpload ...@@ -175,22 +183,43 @@ class GridFsUpload
} }
public function isEOF() public function isEOF()
{ {
return false; return $this->isClosed;
}
private function abort()
{
$this->collectionsWrapper->getChunksCollection()->deleteMany(["files_id"=> $this->file["_id"]]);
$this->collectionsWrapper->getFilesCollection()->deleteOne(["_id"=> $this->file['_id']]);
$this->isClosed = true;
} }
private function insertChunk($data) private function insertChunk($data)
{ {
if($this->isClosed){
return;
}
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->chunkOffset, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)]; $toUpload = ["files_id" => $this->file['_id'], "n" => $this->chunkOffset, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)];
hash_update($this->ctx, $data); hash_update($this->ctx, $data);
$this->collectionsWrapper->chunkInsert($toUpload); try{
$this->collectionsWrapper->chunkInsert($toUpload);
} catch (\MongoDB\Exception $e){
$this->abort();
throw $e;
}
$this->length += strlen($data); $this->length += strlen($data);
$this->chunkOffset++; $this->chunkOffset++;
} }
private function fileCollectionInsert() private function fileCollectionInsert()
{ {
if($this->isClosed){
return;
}
$md5 = hash_final($this->ctx); $md5 = hash_final($this->ctx);
$this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]); $this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]);
$this->collectionsWrapper->fileInsert($this->file); try{
$this->collectionsWrapper->fileInsert($this->file);
} catch (\MongoDB\Exception $e){
$this->abort();
throw $e;
}
return $this->file['_id']; return $this->file['_id'];
} }
//from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php //from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment