-
Notifications
You must be signed in to change notification settings - Fork 265
PHPLIB-1323 Implement unlink
for GridFS stream wrapper
#1206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
abc4bca
6da793c
fc32d0a
f8231a7
c2bcab1
981ede2
70c71e8
1fa8976
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,30 @@ public function deleteChunksByFilesId($id): void | |
$this->chunksCollection->deleteMany(['files_id' => $id]); | ||
} | ||
|
||
/** | ||
* Delete all GridFS files and chunks for a given filename. | ||
*/ | ||
public function deleteFileAndChunksByFilename(string $filename): int | ||
{ | ||
/** @var iterable<array{_id: mixed}> $files */ | ||
$files = $this->findFiles(['filename' => $filename], [ | ||
'codec' => null, | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'typeMap' => ['root' => 'array'], | ||
'projection' => ['_id' => 1], | ||
]); | ||
|
||
/** @var list<mixed> $ids */ | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$ids = []; | ||
foreach ($files as $file) { | ||
$ids[] = $file['_id']; | ||
} | ||
|
||
$count = $this->filesCollection->deleteMany(['_id' => ['$in' => $ids]])->getDeletedCount(); | ||
$this->chunksCollection->deleteMany(['files_id' => ['$in' => $ids]]); | ||
|
||
return $count ?? 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is zero in case of WriteConcern=0 or not file to update. |
||
} | ||
|
||
/** | ||
* Deletes a GridFS file and related chunks by ID. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,6 +173,21 @@ public function close(): void | |
$this->isClosed = true; | ||
} | ||
|
||
/** | ||
* Delete all files and chunks associated with this stream filename. | ||
* | ||
* @return int The number of deleted files | ||
*/ | ||
public function delete(): int | ||
{ | ||
try { | ||
return $this->collectionWrapper->deleteFileAndChunksByFilename($this->file['filename']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realize this is an internal class, so we're not actually introducing a public API here, but a In the PR description, you said:
If we look at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I felt deep down that using |
||
} finally { | ||
// Prevent further operations on this stream | ||
$this->abort(); | ||
} | ||
} | ||
|
||
/** | ||
* Return the stream's file document. | ||
*/ | ||
|
Uh oh!
There was an error while loading. Please reload this page.