Skip to content

Commit 8d7db8b

Browse files
committed
Merge pull request #22 from daniel-sc/master
fix: mongodb - Indices + error handling
2 parents fa811f5 + fe7b5e4 commit 8d7db8b

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/Flow/Mongo/MongoConfig.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Flow\Config;
66

7+
/**
8+
* @codeCoverageIgnore
9+
*/
710
class MongoConfig extends Config implements MongoConfigInterface
811
{
912
private $gridFs;

src/Flow/Mongo/MongoConfigInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Flow\ConfigInterface;
66

77
/**
8-
* @package Flow
8+
* @codeCoverageIgnore
99
*/
1010
interface MongoConfigInterface extends ConfigInterface
1111
{

src/Flow/Mongo/MongoFile.php

+24-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* - Chunk preprocessor not supported (must not modify chunks size)!
1515
* - Must use 'forceChunkSize=true' on client side.
1616
*
17-
* @package Flow
17+
* @codeCoverageIgnore
1818
*/
1919
class MongoFile extends File
2020
{
@@ -44,7 +44,7 @@ protected function getGridFsFile()
4444
$changed = $gridFsFileQuery;
4545
$changed['flowUpdated'] = new \MongoDate();
4646
$this->uploadGridFsFile = $this->config->getGridFs()->findAndModify($gridFsFileQuery, $changed, null,
47-
['upsert' => true]);
47+
['upsert' => true, 'new' => true]);
4848
}
4949

5050
return $this->uploadGridFsFile;
@@ -69,8 +69,8 @@ public function checkChunk()
6969

7070
/**
7171
* Save chunk
72-
*
7372
* @return bool
73+
* @throws \Exception if upload size is invalid or some other unexpected error occurred.
7474
*/
7575
public function saveChunk()
7676
{
@@ -90,13 +90,19 @@ public function saveChunk()
9090
) {
9191
throw new \Exception("Invalid upload! (size: {$actualChunkSize})");
9292
}
93-
$chunk['data'] = new \MongoBinData($data);
93+
$chunk['data'] = new \MongoBinData($data, 0); // \MongoBinData::GENERIC is not defined for older mongo drivers
9494
$this->config->getGridFs()->chunks->findAndModify($chunkQuery, $chunk, [], ['upsert' => true]);
9595
unlink($file['tmp_name']);
9696

97+
$this->ensureIndices();
98+
9799
return true;
98100
} catch (\Exception $e) {
99-
return false;
101+
// try to remove a possibly (partly) stored chunk:
102+
if (isset($chunkQuery)) {
103+
$this->config->getGridFs()->chunks->remove($chunkQuery);
104+
}
105+
throw $e;
100106
}
101107
}
102108

@@ -147,6 +153,18 @@ public function deleteChunks()
147153
// nothing to do, as chunks are directly part of the final file
148154
}
149155

156+
public function ensureIndices()
157+
{
158+
$chunksCollection = $this->config->getGridFs()->chunks;
159+
$indexKeys = ['files_id' => 1, 'n' => 1];
160+
$indexOptions = ['unique' => true, 'background' => true];
161+
if(method_exists($chunksCollection, 'createIndex')) { // only available for PECL mongo >= 1.5.0
162+
$chunksCollection->createIndex($indexKeys, $indexOptions);
163+
} else {
164+
$chunksCollection->ensureIndex($indexKeys, $indexOptions);
165+
}
166+
}
167+
150168
/**
151169
* @return array
152170
*/
@@ -160,4 +178,4 @@ protected function getGridFsFileQuery()
160178
'length' => intval($this->request->getTotalSize())
161179
];
162180
}
163-
}
181+
}

src/Flow/Mongo/MongoUploader.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Flow\FileOpenException;
66

7+
/**
8+
* @codeCoverageIgnore
9+
*/
710
class MongoUploader
811
{
912
/**

0 commit comments

Comments
 (0)