* field name.
*
* @template T
- * @param array<string, string> $variables
* @param string-string<T> $className
* @return T
* @throws UserInputException
* @since 6.1
*/
- public static function fetchObjectFromRequestParameter(array $variables, string $key, string $className): object
+ public static function fetchObjectFromRequestParameter(int|string $objectID, string $className): object
{
if (!\is_subclass_of($className, DatabaseObject::class)) {
throw new ParentClassException($className, DatabaseObject::class);
}
- if (!isset($variables[$key])) {
- throw new \RuntimeException(
- "The variable '{$key}' does not appear in the request variables, please check its spelling and if it appears in the route definition.",
- );
- }
-
- $dbo = new $className($variables[$key]);
+ $dbo = new $className($objectID);
\assert($dbo instanceof DatabaseObject);
if (!$dbo->getObjectID()) {
use Psr\Http\Message\ServerRequestInterface;
use wcf\data\file\File;
use wcf\data\file\FileAction;
+use wcf\http\Helper;
use wcf\system\endpoint\DeleteRequest;
use wcf\system\endpoint\IController;
use wcf\system\exception\PermissionDeniedException;
-use wcf\system\exception\UserInputException;
#[DeleteRequest('/core/files/{id}')]
final class DeleteFile implements IController
#[\Override]
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
{
- $file = new File($variables['id']);
- if (!$file->fileID) {
- throw new UserInputException('id');
- }
-
+ $file = Helper::fetchObjectFromRequestParameter($variables['id'], File::class);
if (!$file->canDelete()) {
throw new PermissionDeniedException();
}
- // TODO: How do we handle the cleanup of files?
$fileAction = new FileAction([$file], 'delete');
$fileAction->executeAction();
use Psr\Http\Message\ResponseInterface;
use wcf\data\file\File;
use wcf\data\file\thumbnail\FileThumbnailList;
+use wcf\http\Helper;
use wcf\system\endpoint\IController;
use wcf\system\endpoint\PostRequest;
use wcf\system\exception\UserInputException;
{
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
{
- $file = new File($variables['id']);
- if (!$file->fileID) {
- throw new UserInputException('id');
- }
+ $file = Helper::fetchObjectFromRequestParameter($variables['id'], File::class);
FileProcessor::getInstance()->generateWebpVariant($file);
FileProcessor::getInstance()->generateThumbnails($file);
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
{
- $fileTemporary = Helper::fetchObjectFromRequestParameter($variables, 'identifier', FileTemporary::class);
+ $fileTemporary = Helper::fetchObjectFromRequestParameter($variables['identifier'], FileTemporary::class);
$checksum = $request->getHeaderLine('chunk-checksum-sha256');
if ($checksum === '' || \str_contains($checksum, ',')) {