-
Notifications
You must be signed in to change notification settings - Fork 264
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 4 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,29 @@ 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], [ | ||
'typeMap' => ['root' => 'array'], | ||
'projection' => ['_id' => 1], | ||
]); | ||
|
||
/** @var list<mixed> $ids */ | ||
$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 |
---|---|---|
|
@@ -106,15 +106,15 @@ public function rename(string $fromPath, string $toPath): bool | |
} | ||
|
||
try { | ||
$this->stream_open($fromPath, 'r', 0, $openedPath); | ||
$context = $this->getContext($fromPath, 'r'); | ||
} catch (FileNotFoundException $e) { | ||
return false; | ||
} | ||
|
||
$newName = explode('/', $toPath, 4)[3] ?? ''; | ||
assert($this->stream instanceof ReadableStream); | ||
$newFilename = explode('/', $toPath, 4)[3] ?? ''; | ||
$context['collectionWrapper']->updateFilenameForFilename($context['file']->filename, $newFilename); | ||
|
||
return $this->stream->rename($newName); | ||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -170,41 +170,12 @@ public function stream_eof(): bool | |
*/ | ||
public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool | ||
{ | ||
$context = []; | ||
|
||
/** | ||
* The Bucket methods { @see Bucket::openUploadStream() } and { @see Bucket::openDownloadStreamByFile() } | ||
* always set an internal context. But the context can also be set by the user. | ||
*/ | ||
if (is_resource($this->context)) { | ||
$context = stream_context_get_options($this->context)['gridfs'] ?? []; | ||
|
||
if (! is_array($context)) { | ||
throw LogicException::invalidContext($context); | ||
} | ||
} | ||
|
||
// When the stream is opened using fopen(), the context is not required, it can contain only options. | ||
if (! isset($context['collectionWrapper'])) { | ||
$bucketAlias = explode('/', $path, 4)[2] ?? ''; | ||
|
||
if (! isset(self::$contextResolvers[$bucketAlias])) { | ||
throw LogicException::bucketAliasNotRegistered($bucketAlias); | ||
} | ||
|
||
$context = self::$contextResolvers[$bucketAlias]($path, $mode, $context); | ||
} | ||
|
||
if (! $context['collectionWrapper'] instanceof CollectionWrapper) { | ||
throw LogicException::invalidContextCollectionWrapper($context['collectionWrapper']); | ||
} | ||
|
||
if ($mode === 'r' || $mode === 'rb') { | ||
return $this->initReadableStream($context); | ||
return $this->initReadableStream($this->getContext($path, $mode)); | ||
} | ||
|
||
if ($mode === 'w' || $mode === 'wb') { | ||
return $this->initWritableStream($context); | ||
return $this->initWritableStream($this->getContext($path, $mode)); | ||
} | ||
|
||
throw LogicException::openModeNotSupported($mode); | ||
|
@@ -324,6 +295,14 @@ public function stream_write(string $data): int | |
return $this->stream->writeBytes($data); | ||
} | ||
|
||
public function unlink(string $path): bool | ||
{ | ||
$context = $this->getContext($path, 'r'); | ||
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 understand 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. The idea of using 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 reverted to throw a |
||
$count = $context['collectionWrapper']->deleteFileAndChunksByFilename($context['file']->filename); | ||
|
||
return $count > 0; | ||
} | ||
|
||
/** @return false|array */ | ||
public function url_stat(string $path, int $flags) | ||
{ | ||
|
@@ -338,6 +317,41 @@ public function url_stat(string $path, int $flags) | |
return $this->stream_stat(); | ||
} | ||
|
||
/** @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array */ | ||
private function getContext(string $path, string $mode): array | ||
{ | ||
$context = []; | ||
|
||
/** | ||
* The Bucket methods { @see Bucket::openUploadStream() } and { @see Bucket::openDownloadStreamByFile() } | ||
* always set an internal context. But the context can also be set by the user. | ||
*/ | ||
if (is_resource($this->context)) { | ||
$context = stream_context_get_options($this->context)['gridfs'] ?? []; | ||
|
||
if (! is_array($context)) { | ||
throw LogicException::invalidContext($context); | ||
} | ||
} | ||
|
||
// When the stream is opened using fopen(), the context is not required, it can contain only options. | ||
if (! isset($context['collectionWrapper'])) { | ||
$bucketAlias = explode('/', $path, 4)[2] ?? ''; | ||
|
||
if (! isset(self::$contextResolvers[$bucketAlias])) { | ||
throw LogicException::bucketAliasNotRegistered($bucketAlias); | ||
} | ||
|
||
$context = self::$contextResolvers[$bucketAlias]($path, $mode, $context); | ||
} | ||
|
||
if (! $context['collectionWrapper'] instanceof CollectionWrapper) { | ||
throw LogicException::invalidContextCollectionWrapper($context['collectionWrapper']); | ||
} | ||
|
||
return $context; | ||
} | ||
|
||
/** | ||
* Returns a stat template with default values. | ||
*/ | ||
|
Uh oh!
There was an error while loading. Please reload this page.