Skip to content

Commit c787545

Browse files
author
Will Banfield
committed
PHPLIB-148: Implement file deletion
1 parent 623a98b commit c787545

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

src/GridFS/Bucket.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,30 @@ public function getBucketName()
111111
{
112112
return $this->options['bucketName'];
113113
}
114-
public function find($filter, array $options =[])
114+
public function getReadConcern()
115115
{
116-
//add proper validation for the filter and for the options
117-
return $this->filesCollection->find($filter);
116+
if(isset($this->options['readPreference'])) {
117+
return $this->options['readPreference'];
118+
} else{
119+
return null;
120+
}
118121
}
119-
120-
public function chunkInsert($toUpload) {
121-
$this->chunksCollection->insertOne($toUpload);
122+
public function getWriteConcern()
123+
{
124+
if(isset($this->options['writeConcern'])) {
125+
return $this->options['writeConcern'];
126+
} else{
127+
return null;
128+
}
122129
}
123130

124-
public function fileInsert($toUpload) {
125-
$this->filesCollection->insertOne($toUpload);
131+
public function find($filter, array $options =[])
132+
{
133+
//add proper validation for the filter and for the options
134+
return $this->filesCollection->find($filter);
126135
}
127136

128-
public function ensureIndexes()
137+
private function ensureIndexes()
129138
{
130139
if ($this->ensuredIndexes) {
131140
return;
@@ -162,11 +171,4 @@ private function isFilesCollectionEmpty()
162171
'projection' => ['_id' => 1],
163172
]);
164173
}
165-
public function delete(ObjectId $id)
166-
{
167-
$options = ['writeConcern' => $this->writeConcern];
168-
$this->chunksCollection->deleteMany(['file_id' => $id], $options);
169-
170-
$this->filesCollection->deleteOne(['_id' => $id], $options);
171-
}
172174
}

src/GridFS/BucketReadWriter.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,25 @@ public function downloadToStream(\MongoDB\BSON\ObjectId $id, $destination)
6363
$gridFsStream = new GridFsDownload($this->bucket, $id);
6464
$gridFsStream->downloadToStream($destination);
6565
}
66+
/**
67+
* Delete a file from the GridFS bucket. If the file collection entry is not found, still attempts to delete orphaned chunks
68+
*
69+
* @param ObjectId $id file id
70+
* @throws GridFSFileNotFoundException
71+
*/
72+
public function delete(\MongoDB\BSON\ObjectId $id)
73+
{
74+
$options =[];
75+
$writeConcern = $this->bucket->getWriteConcern();
76+
if(!is_null($writeConcern)) {
77+
$options['writeConcern'] = $writeConcern;
78+
}
79+
$file = $this->bucket->getFilesCollection()->findOne(['_id' => $id]);
80+
$this->bucket->getChunksCollection()->deleteMany(['files_id' => $id], $options);
81+
if (is_null($file)) {
82+
throw new \MongoDB\Exception\GridFSFileNotFoundException($id, $this->bucket->getDatabaseName(), $this->bucket->getBucketName());
83+
}
6684

85+
$this->bucket->getFilesCollection()->deleteOne(['_id' => $id], $options);
86+
}
6787
}

src/GridFS/GridFsDownload.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GridFsDownload extends GridFsStream
1919
private $bufferFresh=true;
2020
private $bufferEmpty=true;
2121
/**
22-
* Constructs a GridFS upload stream
22+
* Constructs a GridFS download stream
2323
*
2424
* Supported options:
2525
*
@@ -45,7 +45,6 @@ public function __construct(
4545
{
4646
$this->file = $bucket->getFilesCollection()->findOne(['_id' => $objectId]);
4747
if (is_null($this->file)) {
48-
//MUST RAISE AN ERROR ! (WHICH ONE I DON'T)
4948
throw new \MongoDB\Exception\GridFSFileNotFoundException($objectId, $bucket->getBucketName(), $bucket->getDatabaseName());
5049
}
5150
if ($this->file->length > 0) {

tests/GridFS/SpecificationTests.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public function testSpecificationTests($testJson)
7575
$test['assert']['result'] = $result;
7676
}
7777
if ($testResult == "void") {
78-
$test['assert']['result'] = null;
78+
$fixedAssert['result'] = null;
7979
}
8080
$this->assertEquals($result, $fixedAssert['result']);
8181
}
8282
if (isset($test['assert']['data'])) {
83-
$this->runCommands($fixedAssert, $result);
83+
$this->runCommands($fixedAssert['data'], $result);
8484
$this->collectionsEqual($this->collections['expected.files'],$this->bucket->getFilesCollection());
8585
if(isset($this->collections['expected.chunks'])) {
8686
$this->collectionsEqual($this->collections['expected.chunks'],$this->bucket->getChunksCollection());
@@ -91,7 +91,7 @@ public function testSpecificationTests($testJson)
9191

9292
public function provideSpecificationTests()
9393
{
94-
$testPath=getcwd().'/tests/GridFS/Specification/tests/download.json';
94+
$testPath=getcwd().'/tests/GridFS/Specification/tests/delete.json';
9595

9696
$testArgs = [];
9797
foreach(glob($testPath) as $filename) {
@@ -148,29 +148,30 @@ public function filterDoc($collection, $ignoreId)
148148

149149
public function runCommands($cmds, $result)
150150
{
151-
$cmds = $this->fixTypes($cmds, true);
152-
foreach($cmds as $cmd) {
151+
foreach($cmds as $cmd){
153152
foreach($cmd as $key => $value) {
154153
if(isset($this->commands[$key])) {
155154
$cmdName = $key;
156155
$collectionName = $value;
157-
158-
foreach($cmd['documents'] as $docIndex => $doc) {
159-
foreach($doc as $docKey => $docVal){
160-
if(is_string($docVal)) {
161-
if($docVal == '*result') {
162-
$doc[$docKey] = $result;
156+
if(isset($cmd['documents'])){
157+
foreach($cmd['documents'] as $docIndex => $doc) {
158+
foreach($doc as $docKey => $docVal){
159+
if(is_string($docVal)) {
160+
if($docVal == '*result') {
161+
$doc[$docKey] = $result;
162+
}
163163
}
164164
}
165+
$cmd['documents'][$docIndex] = $doc;
165166
}
166-
$cmd['documents'][$docIndex] = $doc;
167167
}
168168
$collection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), $collectionName));
169169
$this->commands[$key]($collection, $this->fixTypes($cmd, true));
170170
$this->collections[$collectionName] = $collection;
171171
}
172172
}
173173
}
174+
174175
}
175176

176177
public function initializeDatabases($data, $test)
@@ -186,12 +187,15 @@ public function initializeDatabases($data, $test)
186187
$filesCollection->insertMany($data['files']);
187188
$expectedFilesCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.files"));
188189
$expectedFilesCollection->insertMany($data['files']);
190+
$this->collections['expected.files'] = $expectedFilesCollection;
189191
}
190192
if (isset($data['chunks']) && count($data['chunks']) > 0) {
191193
$chunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "fs.chunks"));
192194
$chunksCollection->insertMany($data['chunks']);
193195
$expectedChunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.chunks"));
194196
$expectedChunksCollection->insertMany($data['chunks']);
197+
$this->collections['expected.chunks'] = $expectedChunksCollection;
198+
195199
}
196200
if(isset($test['arrange'])) {
197201
foreach($test['arrange']['data'] as $cmd) {
@@ -203,9 +207,7 @@ public function initializeDatabases($data, $test)
203207
}
204208
}
205209
}
206-
207210
}
208-
209211
public function uploadCommand($args)
210212
{
211213
$args = $this->fixTypes($args, false);
@@ -230,7 +232,8 @@ function downloadCommand($args)
230232
}
231233
function deleteCommand($args)
232234
{
233-
235+
$args = $this->fixTypes($args, false);
236+
$this->bucketReadWriter->delete($args['id']);
234237
}
235238
function download_by_nameCommand($args)
236239
{

0 commit comments

Comments
 (0)