aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Avatar/AvatarUpload.php
blob: de50fb235a836f98d3496427ee1b433cbc1759d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace JsonApi\Routes\Avatar;

use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\BadRequestException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\Routes\ValidationTrait;
use JsonApi\NonJsonApiController;
use JsonApi\Routes\Files\RoutesHelperTrait as FilesRoutesHelper;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
 * Create an Avatar.
 */
class AvatarUpload extends NonJsonApiController
{
    use ValidationTrait;
    use AvatarHelpers;
    public function __invoke(Request $request, Response $response, $args): Response
    {
        $user = $this->getUser($request);
        $json = $this->validate($request);
        $range_id = self::arrayGet($json, 'data.range-id');
        $range_type = self::arrayGet($json, 'data.range-type');

        ['class' => $class, 'has_perm' => $has_perm] = self::getAvatarClass($range_id, $range_type, $user);

        if (!$has_perm) {
            throw new AuthorizationFailedException();
        }

        $avatar = $class::getAvatar($range_id);
        $imgdata_string = self::arrayGet($json, 'data.image');
        [$type, $imgdata_part] = explode(';', $imgdata_string);
        [$base, $imgdata_base64] = explode(',', $imgdata_part);
        $imgdata = base64_decode($imgdata_base64);
        // Write data to file.
        $filename = $GLOBALS['TMP_PATH'] . '/avatar-' . $range_id . '.webp';
        file_put_contents($filename, $imgdata);

        // Use new image file for avatar creation.
        $avatar->createFrom($filename);


        return $response->withStatus(201);
    }

    protected function validateResourceDocument($json, $data)
    {
        if (!self::arrayHas($json, 'data')) {
            return 'Missing `data` member at document“s top level.';
        }
        if (!self::arrayHas($json, 'data.range-id')) {
            return 'New avatar must have an `range-id`.';
        }
        if (!self::arrayHas($json, 'data.range-type')) {
            return 'New avatar must have a `range-type`.';
        }
        if (!self::arrayHas($json, 'data.image')) {
            return 'New avatar must have a `image`.';
        }
    }
}