Skip to content
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

VCRM-23: user avatar upload #2

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Default Avatar Images

Image by juicy_fish on Freepik

# NKCS CRM API License

Copyright (c) 2023-present Nathanael Kammermann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Binary file added public/data/user/default_avatar_male.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/data/user/default_bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

namespace App\Controller;

use App\Dto\Base64FileRequest;
use App\Repository\SystemSettingRepository;
use App\Service\User\ClientNavigation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[Route('/api/user')]
class UserController extends AbstractApiController
{
public function __construct(
private string $documentBaseDir,
) {
}

#[Route('/')]
public function getUserInfo(
ClientNavigation $clientNavigation,
Expand All @@ -22,8 +29,38 @@ public function getUserInfo(

$userNavigation = $clientNavigation->getUserClientNavigation($me);

// todo: make this optional by system_setting
$userAvatar = '';
if (file_exists($this->documentBaseDir . '/user/' . $this->getUser()->getId() . '_avatar.png')) {
$userAvatar = base64_encode(file_get_contents(
$this->documentBaseDir . '/user/' . $this->getUser()->getId() . '_avatar.png'
));
} else {
if (file_exists($this->documentBaseDir . '/user/default_avatar_male.png')) {
$userAvatar = base64_encode(file_get_contents(
$this->documentBaseDir . '/user/default_avatar_male.png'
));
}
}

// todo: make this optional by system_setting
$userAvatarBg = '';
if (file_exists($this->documentBaseDir . '/user/' . $this->getUser()->getId() . '_background.jpg')) {
$userAvatarBg = base64_encode(file_get_contents(
$this->documentBaseDir . '/user/' . $this->getUser()->getId() . '_background.jpg'
));
} else {
if (file_exists($this->documentBaseDir . '/user/default_bg.jpg')) {
$userAvatarBg = base64_encode(file_get_contents(
$this->documentBaseDir . '/user/default_bg.jpg'
));
}
}

$userApiData = [
'user' => [
'avatar' => $userAvatar,
'background' => $userAvatarBg,
'name' => $me->getName(),
'firstName' => $me->getFirstName(),
'function' => $me->getFunction(),
Expand Down Expand Up @@ -63,6 +100,7 @@ public function getUserInfo(
];
}
}

return $this->json($userApiData);
}

Expand All @@ -78,4 +116,62 @@ public function referralInfo(
. $this->getParameter('license.holder')
)->getContent()));
}

#[Route(
path: '/avatar',
methods: ['POST']
)]
public function uploadAvatar(
#[MapRequestPayload] Base64FileRequest $base64FileRequest,
): Response {
$templateContent = base64_decode($base64FileRequest->getFile());
$templateSavePath = $this->documentBaseDir . '/user/' . $this->getUser()->getId() . '_avatar.png';
file_put_contents($templateSavePath, $templateContent);

$userAvatar = '';
if (file_exists($templateSavePath)) {
$userAvatar = base64_encode(file_get_contents(
$templateSavePath
));
} else {
if (file_exists($this->documentBaseDir . '/user/default_avatar_male.png')) {
$userAvatar = base64_encode(file_get_contents(
$this->documentBaseDir . '/user/default_avatar_male.png'
));
}
}

return $this->json([
'avatar' => $userAvatar
]);
}

#[Route(
path: '/background',
methods: ['POST']
)]
public function uploadBackground(
#[MapRequestPayload] Base64FileRequest $base64FileRequest,
): Response {
$templateContent = base64_decode($base64FileRequest->getFile());
$templateSavePath = $this->documentBaseDir . '/user/' . $this->getUser()->getId() . '_background.jpg';
file_put_contents($templateSavePath, $templateContent);

$userBackground = '';
if (file_exists($templateSavePath)) {
$userBackground = base64_encode(file_get_contents(
$templateSavePath
));
} else {
if (file_exists($this->documentBaseDir . '/user/default_bg.jpg')) {
$userBackground = base64_encode(file_get_contents(
$this->documentBaseDir . '/user/default_bg.jpg'
));
}
}

return $this->json([
'background' => $userBackground
]);
}
}
36 changes: 36 additions & 0 deletions src/Dto/Base64FileRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Dto;

use DateTimeImmutable;

readonly class Base64FileRequest
{
public function __construct(
private string $file,
private string $name,
private string $extension,
private string $mimeType,
) {
}

public function getFile(): string
{
return $this->file;
}

public function getName(): string
{
return $this->name;
}

public function getExtension(): string
{
return $this->extension;
}

public function getMimeType(): string
{
return $this->mimeType;
}
}
Loading