-
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 all 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 |
---|---|---|
|
@@ -96,7 +96,8 @@ public static function register(string $protocol = 'gridfs'): void | |
/** | ||
* Rename all revisions of a filename. | ||
* | ||
* @return bool True on success or false on failure. | ||
* @return true | ||
* @throws FileNotFoundException | ||
*/ | ||
public function rename(string $fromPath, string $toPath): bool | ||
{ | ||
|
@@ -105,16 +106,17 @@ public function rename(string $fromPath, string $toPath): bool | |
throw LogicException::renamePathMismatch($fromPath, $toPath); | ||
} | ||
|
||
try { | ||
$this->stream_open($fromPath, 'r', 0, $openedPath); | ||
} catch (FileNotFoundException $e) { | ||
return false; | ||
} | ||
$context = $this->getContext($fromPath, 'w'); | ||
|
||
$newFilename = explode('/', $toPath, 4)[3] ?? ''; | ||
$count = $context['collectionWrapper']->updateFilenameForFilename($context['filename'], $newFilename); | ||
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. Edge case: if Alternatively, you could have 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. Good catch. I updated the code to return the matched count, and added a test for renaming with the same filename (existing and not existing). |
||
|
||
$newName = explode('/', $toPath, 4)[3] ?? ''; | ||
assert($this->stream instanceof ReadableStream); | ||
if ($count === 0) { | ||
throw FileNotFoundException::byFilename($fromPath); | ||
} | ||
|
||
return $this->stream->rename($newName); | ||
// If $count is null, the update is unacknowledged, the operation is considered successful. | ||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -170,41 +172,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 +297,25 @@ public function stream_write(string $data): int | |
return $this->stream->writeBytes($data); | ||
} | ||
|
||
/** | ||
* Remove all revisions of a filename. | ||
* | ||
* @return true | ||
* @throws FileNotFoundException | ||
*/ | ||
public function unlink(string $path): bool | ||
{ | ||
$context = $this->getContext($path, 'w'); | ||
$count = $context['collectionWrapper']->deleteFileAndChunksByFilename($context['filename']); | ||
|
||
if ($count === 0) { | ||
throw FileNotFoundException::byFilename($path); | ||
} | ||
|
||
// If $count is null, the update is unacknowledged, the operation is considered successful. | ||
return true; | ||
} | ||
|
||
/** @return false|array */ | ||
public function url_stat(string $path, int $flags) | ||
{ | ||
|
@@ -338,6 +330,45 @@ 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} | ||
* @psalm-return ($mode == 'r' or $mode == 'rb' ? 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); | ||
} | ||
|
||
/** @see Bucket::resolveStreamContext() */ | ||
$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. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmikola removing or renaming a single revision is not supported. Even if the option
gridfs[revision]
is set.