Skip to content

Commit d364c43

Browse files
authored
Merge pull request #43 from daniel-sc/additional-options-for-mongo-write
feature: allow to add custom options to mongo write request
2 parents 1d7e633 + 5d3fee1 commit d364c43

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: php
2+
dist: precise
23

34
php:
45
- 5.3

src/Flow/Mongo/MongoFile.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ public function checkChunk()
6969

7070
/**
7171
* Save chunk
72+
* @param $additionalUpdateOptions array additional options for the mongo update/upsert operation.
7273
* @return bool
7374
* @throws \Exception if upload size is invalid or some other unexpected error occurred.
7475
*/
75-
public function saveChunk()
76+
public function saveChunk($additionalUpdateOptions = [])
7677
{
7778
try {
7879
$file = $this->request->getFile();
@@ -91,7 +92,7 @@ public function saveChunk()
9192
throw new \Exception("Invalid upload! (size: {$actualChunkSize})");
9293
}
9394
$chunk['data'] = new \MongoBinData($data, 0); // \MongoBinData::GENERIC is not defined for older mongo drivers
94-
$this->config->getGridFs()->chunks->findAndModify($chunkQuery, $chunk, [], ['upsert' => true]);
95+
$this->config->getGridFs()->chunks->update($chunkQuery, $chunk, array_merge(['upsert' => true], $additionalUpdateOptions));
9596
unlink($file['tmp_name']);
9697

9798
$this->ensureIndices();
@@ -111,15 +112,11 @@ public function saveChunk()
111112
*/
112113
public function validateFile()
113114
{
114-
$totalChunks = $this->request->getTotalChunks();
115-
116-
for ($i = 1; $i <= $totalChunks; $i++) {
117-
if (!$this->chunkExists($i)) {
118-
return false;
119-
}
120-
}
121-
122-
return true;
115+
$totalChunks = intval($this->request->getTotalChunks());
116+
$storedChunks = $this->config->getGridFs()->chunks
117+
->find(['files_id' => $this->getGridFsFile()['_id']])
118+
->count();
119+
return $totalChunks === $storedChunks;
123120
}
124121

125122

0 commit comments

Comments
 (0)