Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

@@ -10,26 +10,34 @@
*/
namespace IPS\core\api\GraphQL\Mutations;
use GraphQL\Type\Definition\ObjectType;
use Exception;
use GraphQL\Type\Definition\UnionType;
use InvalidArgumentException;
use IPS\Api\GraphQL\SafeException;
use IPS\Api\GraphQL\TypeRegistry;
use IPS\File;
use IPS\Helpers\Form\Editor;
use IPS\Image;
use IPS\Member;
use function defined;
use function in_array;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Upload attachment mutation for GraphQL API
*/
class _UploadAttachment
class UploadAttachment
{
/*
* @brief Query description
*/
public static $description = "Upload a file to be an attachment";
public static string $description = "Upload a file to be an attachment";
/*
* Mutation arguments
@@ -49,7 +57,7 @@ class _UploadAttachment
/**
* Return the mutation return type
*/
public function type()
public function type() : UnionType
{
return new UnionType([
'name' => 'core_UploadAttachment',
@@ -73,23 +81,24 @@ class _UploadAttachment
/**
* Resolves this mutation
*
* @param mixed Value passed into this resolver
* @param array Arguments
* @param array Context values
* @param mixed $val Value passed into this resolver
* @param array $args Arguments
* @param array $context Context values
* @param array $info
* @return array
*/
public function resolve($val, $args, $context, $info)
public function resolve( mixed $val, array $args, array $context, array $info ) : array
{
$storageClass = \IPS\File::getClass( 'core_Attachment' );
$storageClass = File::getClass( 'core_Attachment' );
$contents = base64_decode( $args['contents'] );
/* Check allowed types */
$ext = mb_substr( $args['name'], mb_strrpos( $args['name'], '.' ) + 1 );
if( $allowedFileTypes = \IPS\Helpers\Form\Editor::allowedFileExtensions() )
if( $allowedFileTypes = Editor::allowedFileExtensions() )
{
if( !\in_array( mb_strtolower( $ext ), array_map( 'mb_strtolower', $allowedFileTypes ) ) )
if( !in_array( mb_strtolower( $ext ), array_map( 'mb_strtolower', $allowedFileTypes ) ) )
{
throw new \IPS\Api\GraphQL\SafeException( 'FILE_TYPE_NOT_ALLOWED', '2C399/1_graphql', 403 );
throw new SafeException( 'FILE_TYPE_NOT_ALLOWED', '2C399/1_graphql', 403 );
}
}
@@ -115,15 +124,15 @@ class _UploadAttachment
$file = $storageClass->chunkFinish( $ref, 'core_Attachment' );
/* If it's got an image extension, check it's actually a valid image */
if ( \in_array( $ext, \IPS\Image::supportedExtensions() ) )
if ( in_array( $ext, Image::supportedExtensions() ) )
{
try
{
$file->getImageDimensions();
}
catch ( \Exception $e )
catch ( Exception $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'NOT_VALID_IMAGE', '2C399/2_graphql', 403 );
throw new SafeException( 'NOT_VALID_IMAGE', '2C399/2_graphql', 403 );
}
}
}
@@ -141,31 +150,30 @@ class _UploadAttachment
else
{
/* If it's got an image extension, check it's actually a valid image */
if ( \in_array( $ext, \IPS\Image::supportedExtensions() ) )
if ( in_array( $ext, Image::supportedExtensions() ) )
{
try
{
$image = \IPS\Image::create( $contents );
$image = Image::create( $contents );
}
catch ( \InvalidArgumentException $e )
catch ( InvalidArgumentException $e )
{
throw new \IPS\Api\GraphQL\SafeException( $e->getMessage(), '2C399/2_graphql', 403 );
throw new SafeException( $e->getMessage(), '2C399/2_graphql', 403 );
}
}
/* Create the file */
try
{
$file = \IPS\File::create( 'core_Attachment', $args['name'], $contents );
$file = File::create( 'core_Attachment', $args['name'], $contents );
}
catch ( \Exception $e )
catch ( Exception $e )
{
throw new \IPS\Api\GraphQL\SafeException( 'FILE_CREATION_FAILED', '2C399/3_graphql', 403 );
throw new SafeException( 'FILE_CREATION_FAILED', '2C399/3_graphql', 403 );
}
}
/* Make it into an attachment */
$attachment = $file->makeAttachment( $args['postKey'], \IPS\Member::loggedIn() );
return $attachment;
return $file->makeAttachment( $args['postKey'], Member::loggedIn() );
}
}