File tree 3 files changed +36
-18
lines changed
3 files changed +36
-18
lines changed Original file line number Diff line number Diff line change 85
85
<code >$context</code >
86
86
</MixedArgumentTypeCoercion >
87
87
</file >
88
+ <file src =" src/GridFS/CollectionWrapper.php" >
89
+ <MixedAssignment >
90
+ <code >$ids[]</code >
91
+ </MixedAssignment >
92
+ </file >
88
93
<file src =" src/GridFS/ReadableStream.php" >
89
94
<MixedArgument >
90
95
<code ><![CDATA[ $currentChunk->n]]> </code >
100
105
<code >$context</code >
101
106
</MixedAssignment >
102
107
</file >
108
+ <file src =" src/GridFS/WritableStream.php" >
109
+ <MixedArgument >
110
+ <code ><![CDATA[ $this->file['filename']]]> </code >
111
+ </MixedArgument >
112
+ </file >
103
113
<file src =" src/Model/BSONArray.php" >
104
114
<MixedAssignment >
105
115
<code >$this[$key]</code >
Original file line number Diff line number Diff line change @@ -91,6 +91,27 @@ public function deleteFileAndChunksById($id): void
91
91
$ this ->chunksCollection ->deleteMany (['files_id ' => $ id ]);
92
92
}
93
93
94
+ public function deleteFileAndChunksByFilename (string $ filename ): int
95
+ {
96
+ /** @var iterable<array{_id: mixed}> $files */
97
+ $ files = $ this ->findFiles (['filename ' => $ filename ], [
98
+ 'codec ' => null ,
99
+ 'typeMap ' => ['root ' => 'array ' ],
100
+ 'projection ' => ['_id ' => 1 ],
101
+ ]);
102
+
103
+ /** @var list<mixed> $ids */
104
+ $ ids = [];
105
+ foreach ($ files as $ file ) {
106
+ $ ids [] = $ file ['_id ' ];
107
+ }
108
+
109
+ $ count = $ this ->filesCollection ->deleteMany (['_id ' => ['$in ' => $ ids ]])->getDeletedCount ();
110
+ $ this ->chunksCollection ->deleteMany (['files_id ' => ['$in ' => $ ids ]]);
111
+
112
+ return $ count ?? 0 ;
113
+ }
114
+
94
115
/**
95
116
* Drops the GridFS files and chunks collections.
96
117
*/
Original file line number Diff line number Diff line change 19
19
20
20
use HashContext ;
21
21
use MongoDB \BSON \Binary ;
22
- use MongoDB \BSON \Document ;
23
22
use MongoDB \BSON \ObjectId ;
24
23
use MongoDB \BSON \UTCDateTime ;
25
24
use MongoDB \Driver \Exception \RuntimeException as DriverRuntimeException ;
@@ -181,24 +180,12 @@ public function close(): void
181
180
*/
182
181
public function delete (): int
183
182
{
184
- /** @var Document[] $revisions */
185
- $ revisions = $ this ->collectionWrapper ->findFiles (['filename ' => $ this ->file ['filename ' ]], [
186
- 'codec ' => null ,
187
- 'typeMap ' => ['root ' => 'bson ' ],
188
- 'projection ' => ['_id ' => 1 ],
189
- ]);
190
-
191
- $ count = 0 ;
192
-
193
- foreach ($ revisions as $ file ) {
194
- $ this ->collectionWrapper ->findFileById ($ file ->get ('_id ' ));
195
- $ this ->collectionWrapper ->deleteFileAndChunksById ($ file ->get ('_id ' ));
196
- $ count ++;
183
+ try {
184
+ return $ this ->collectionWrapper ->deleteFileAndChunksByFilename ($ this ->file ['filename ' ]);
185
+ } finally {
186
+ // Prevent further operations on this stream
187
+ $ this ->abort ();
197
188
}
198
-
199
- $ this ->abort ();
200
-
201
- return $ count ;
202
189
}
203
190
204
191
/**
You can’t perform that action at this time.
0 commit comments