2024-07-29 22:06:57 +02:00

83 lines
1.9 KiB
PHP

<?php
namespace Api\Users\Image;
use Exception;
use Khofmann\Api\Api;
use Khofmann\Input\Input;
use Khofmann\Models\User\User;
use Khofmann\Response\Response;
use Khofmann\ApiError\ApiError;
use Khofmann\Request\Request;
/**
* User image route handlers
*/
class Image extends Api
{
/**
* Image POST handler
*
* Set a new user image.
*
* Returns updated user.
*
* @param mixed $id User ID
*
* @throws 404 User not found
* @throws 500 Failed to update user image
*/
public function post($id): void
{
// Fetch all inputs.
$image = Input::file("image");
$predefined = Input::post("predefined");
// Try and update user image.
// Throw errors according to situation.
try {
Response::json(User::getByID($id)->updateImage($image, $predefined));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw ApiError::notFound("user");
default:
// Due to how the failed field is handled, it's ApiError is inside the models update
throw $err;
}
}
}
/**
* Image POST handler
*
* Set a new user image. User is retrieved using the authentication `token`.
*
* Returns updated user.
*
* @throws 404 User not found
* @throws 500 Failed to update user image
*/
public function postSelf(): void
{
// Fetch all inputs.
$token = Request::token();
$image = Input::file("image");
$predefined = Input::post("predefined");
// Try and update user image.
// Throw errors according to situation.
try {
Response::json(User::getByToken($token)->updateImage($image, $predefined));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw ApiError::notFound("user");
default:
// Due to how the failed field is handled, it's ApiError is inside the models update
throw $err;
}
}
}
}