Commit c267c858 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-241: Delete orphan chunks if GridFS upload fails

parent 6676e7ef
...@@ -5,6 +5,7 @@ namespace MongoDB\GridFS; ...@@ -5,6 +5,7 @@ namespace MongoDB\GridFS;
use MongoDB\BSON\Binary; use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectId; use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime; use MongoDB\BSON\UTCDateTime;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\RuntimeException; use MongoDB\Exception\RuntimeException;
...@@ -175,7 +176,12 @@ class WritableStream ...@@ -175,7 +176,12 @@ class WritableStream
private function abort() private function abort()
{ {
$this->collectionWrapper->deleteChunksByFilesId($this->file['_id']); try {
$this->collectionWrapper->deleteChunksByFilesId($this->file['_id']);
} catch (DriverRuntimeException $e) {
// We are already handling an error if abort() is called, so suppress this
}
$this->isClosed = true; $this->isClosed = true;
} }
...@@ -191,7 +197,13 @@ class WritableStream ...@@ -191,7 +197,13 @@ class WritableStream
$this->file['length'] = $this->length; $this->file['length'] = $this->length;
$this->file['md5'] = $md5; $this->file['md5'] = $md5;
$this->collectionWrapper->insertFile($this->file); try {
$this->collectionWrapper->insertFile($this->file);
} catch (DriverRuntimeException $e) {
$this->abort();
throw $e;
}
return $this->file['_id']; return $this->file['_id'];
} }
...@@ -218,7 +230,14 @@ class WritableStream ...@@ -218,7 +230,14 @@ class WritableStream
hash_update($this->ctx, $data); hash_update($this->ctx, $data);
$this->collectionWrapper->insertChunk($chunk); try {
$this->collectionWrapper->insertChunk($chunk);
} catch (DriverRuntimeException $e) {
$this->abort();
throw $e;
}
$this->length += strlen($data); $this->length += strlen($data);
$this->chunkOffset++; $this->chunkOffset++;
} }
......
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