Move avatars to panel assets

This commit is contained in:
Giuseppe Criscione 2023-07-02 17:30:47 +02:00
parent 4f153f80c5
commit 8b15b31d41
16 changed files with 55 additions and 58 deletions

2
.gitignore vendored
View File

@ -11,7 +11,7 @@ php_error.log
/vendor/*
/panel/accounts/*
/panel/avatars/*
/panel/assets/images/users/*
/panel/logs/*
!.gitkeep

View File

@ -9,7 +9,7 @@ return [
'loginResetTime' => 300,
'logoutRedirect' => 'login',
'sessionTimeout' => 20,
'avatarSize' => 512,
'userImageSize' => 512,
'colorScheme' => 'light',
'paths' => [
'accounts' => PANEL_PATH . 'accounts' . DS,

View File

@ -90,7 +90,7 @@ class UsersController extends AbstractController
);
}
FileSystem::delete(Formwork::instance()->config()->get('panel.paths.accounts') . $user->username() . '.yml');
$this->deleteAvatar($user);
$this->deleteImage($user);
} catch (TranslatedException $e) {
$this->panel()->notify($e->getTranslatedMessage(), 'error');
return $this->redirectToReferer(302, '/users/');
@ -185,8 +185,8 @@ class UsersController extends AbstractController
}
// Handle incoming files
if (HTTPRequest::hasFiles() && ($avatar = $this->uploadAvatar($user)) !== null) {
$data['avatar'] = $avatar;
if (HTTPRequest::hasFiles() && ($image = $this->uploadImage($user)) !== null) {
$data['image'] = $image;
}
// Filter empty items from $data and merge them with $user ones
@ -196,14 +196,14 @@ class UsersController extends AbstractController
}
/**
* Upload a new avatar for a user
* Upload a new image for a user
*/
protected function uploadAvatar(User $user): ?string
protected function uploadImage(User $user): ?string
{
$avatarsPath = PANEL_PATH . 'avatars' . DS;
$imagesPath = PANEL_PATH . 'assets' . DS . 'images' . DS . 'users' . DS;
$uploader = new Uploader(
$avatarsPath,
$imagesPath,
[
'allowedMimeTypes' => ['image/gif', 'image/jpeg', 'image/png', 'image/webp'],
]
@ -212,28 +212,28 @@ class UsersController extends AbstractController
$hasUploaded = $uploader->upload(FileSystem::randomName());
if ($hasUploaded) {
$avatarSize = Formwork::instance()->config()->get('panel.avatarSize');
$userImageSize = Formwork::instance()->config()->get('panel.userImageSize');
// Square off uploaded avatar
$image = new Image($avatarsPath . $uploader->uploadedFiles()[0]);
$image->square($avatarSize)->save();
// Square off uploaded image
$image = new Image($imagesPath . $uploader->uploadedFiles()[0]);
$image->square($userImageSize)->save();
// Delete old avatar
$this->deleteAvatar($user);
// Delete old image
$this->deleteImage($user);
$this->panel()->notify($this->translate('panel.user.avatar.uploaded'), 'success');
$this->panel()->notify($this->translate('panel.user.image.uploaded'), 'success');
return $uploader->uploadedFiles()[0];
}
}
/**
* Delete the avatar of a given user
* Delete the image of a given user
*/
protected function deleteAvatar(User $user): void
protected function deleteImage(User $user): void
{
$avatar = $user->avatar()->path();
if ($avatar !== null && FileSystem::exists($avatar)) {
FileSystem::delete($avatar);
$image = $user->image()->path();
if ($image !== null && FileSystem::exists($image)) {
FileSystem::delete($image);
}
}
}

View File

@ -96,7 +96,7 @@ class Updater
'cleanupAfterInstall' => false,
'ignore' => [
'panel/accounts/*',
'panel/avatars/*',
'panel/assets/images/users/*',
'panel/logs/*',
'assets/*',
'backup/*',

View File

@ -25,7 +25,7 @@ class User implements Arrayable
'email' => null,
'language' => 'en',
'role' => 'user',
'avatar' => null,
'image' => null,
'colorScheme' => 'auto',
];
@ -60,9 +60,9 @@ class User implements Arrayable
protected string $role;
/**
* User avatar
* User image
*/
protected Avatar $avatar;
protected UserImage $image;
/**
* User permissions
@ -136,15 +136,15 @@ class User implements Arrayable
}
/**
* Return user avatar
* Return user image
*/
public function avatar(): Avatar
public function image(): UserImage
{
if (isset($this->avatar)) {
return $this->avatar;
if (isset($this->image)) {
return $this->image;
}
return $this->avatar = new Avatar($this->data['avatar']);
return $this->image = new UserImage($this->data['image']);
}
/**

View File

@ -5,39 +5,36 @@ namespace Formwork\Panel\Users;
use Formwork\Formwork;
use Formwork\Utils\FileSystem;
class Avatar
class UserImage
{
/**
* Default avatar URI
* Default image URI
*/
protected const DEFAULT_AVATAR_URI = '/assets/images/avatar.svg';
protected const DEFAULT_IMAGE_URI = '/assets/images/user-image.svg';
/**
* Avatar URI
* Image URI
*/
protected string $uri;
/**
* Avatar file path
* Image file path
*/
protected ?string $path = null;
/**
* Create a new Avatar instance
*/
public function __construct(?string $filename)
{
$path = PANEL_PATH . 'avatars/' . $filename;
$path = PANEL_PATH . 'assets' . DS . 'images' . DS . 'users' . DS . $filename;
if ($filename !== null && FileSystem::exists($path)) {
$this->uri = Formwork::instance()->panel()->realUri('/avatars/' . basename($path));
$this->uri = Formwork::instance()->panel()->realUri('/assets/images/users/' . basename($path));
$this->path = $path;
} else {
$this->uri = Formwork::instance()->panel()->realUri(self::DEFAULT_AVATAR_URI);
$this->uri = Formwork::instance()->panel()->realUri(self::DEFAULT_IMAGE_URI);
}
}
/**
* Return avatar URI
* Return image URI
*/
public function uri(): string
{
@ -45,7 +42,7 @@ class Avatar
}
/**
* Return avatar path
* Return image path
*/
public function path(): ?string
{

View File

Before

Width:  |  Height:  |  Size: 482 B

After

Width:  |  Height:  |  Size: 482 B

View File

@ -5,7 +5,7 @@ layout:
sections:
user:
label: '{{panel.users.user}}'
fields: [fullname, email, password, language, role, colorScheme, avatar]
fields: [fullname, email, password, language, role, colorScheme, image]
fields:
fullname:
@ -49,7 +49,7 @@ fields:
dark: '{{panel.user.colorScheme.dark}}'
auto: '{{panel.user.colorScheme.auto}}'
avatar:
image:
type: file
label: '{{panel.user.avatar}}'
label: '{{panel.user.image}}'
accept: .jpg, .jpeg, .png, .gif

View File

@ -229,8 +229,8 @@ panel.uploader.error.phpExtension: File upload stopped by extension
panel.uploader.error.size: The uploaded file exceeds the maximum file size
panel.uploader.uploaded: File uploaded
panel.user.actions: Actions
panel.user.avatar: Avatar
panel.user.avatar.uploaded: Avatar uploaded
panel.user.image: Image
panel.user.image.uploaded: User image uploaded
panel.user.colorScheme: Color Scheme
panel.user.colorScheme.auto: Auto
panel.user.colorScheme.dark: Dark

View File

@ -214,8 +214,8 @@ panel.uploader.error.phpExtension: Téléversement du fichier arrêté par exten
panel.uploader.error.size: Le fichier téléversé dépasse la taille de fichier maximale
panel.uploader.uploaded: Fichier envoyé !
panel.user.actions: Actions
panel.user.avatar: Photo de profil
panel.user.avatar.uploaded: Photo de profil téléversée avec succès.
panel.user.image: Photo de profil
panel.user.image.uploaded: Photo de profil téléversée avec succès.
panel.user.email: Adresse de messagerie
panel.user.fullname: Nom complet
panel.user.language: Langue

View File

@ -229,8 +229,8 @@ panel.uploader.error.phpExtension: Il caricamento è stato interrotto da unes
panel.uploader.error.size: Il file caricato supera la dimensione massima consentita
panel.uploader.uploaded: File caricato
panel.user.actions: Azioni
panel.user.avatar: Immagine
panel.user.avatar.uploaded: Immagine caricata
panel.user.image: Immagine
panel.user.image.uploaded: Immagine caricata
panel.user.colorScheme: Combinazione di colori
panel.user.colorScheme.auto: Automatica
panel.user.colorScheme.dark: Scura

View File

@ -223,8 +223,8 @@ panel.uploader.error.phpExtension: Envio de ficheiro interrompido por extensão
panel.uploader.error.size: O ficheiro enviado excede o tamanho máximo permitido.
panel.uploader.uploaded: Ficheiro enviado
panel.user.actions: Acções
panel.user.avatar: Avatar
panel.user.avatar.uploaded: Avatar enviado
panel.user.image: Imagem
panel.user.image.uploaded: Imagem do utilizador enviada
panel.user.colorScheme: Esquema de cores
panel.user.colorScheme.auto: Auto
panel.user.colorScheme.dark: Escuro

View File

@ -206,8 +206,8 @@ panel.uploader.error.phpExtension: Загрузка файла останавл
panel.uploader.error.size: Загруженный файл превышает максимальный размер файла
panel.uploader.uploaded: Файл загружен
panel.user.actions: Действия
panel.user.avatar: Аватар
panel.user.avatar.uploaded: Аватар закачанный
panel.user.image: Аватар
panel.user.image.uploaded: Аватар закачанный
panel.user.email: Email
panel.user.fullname: Полное имя
panel.user.language: Язык

View File

@ -4,7 +4,7 @@
<a href="<?= $panel->uri('/users/' . $panel->user()->username() . '/profile/') ?>">
<div class="panel-user-card">
<div class="panel-user-avatar">
<img src="<?= $panel->user()->avatar()->uri() ?>" alt="">
<img src="<?= $panel->user()->image()->uri() ?>" alt="">
</div>
<div class="panel-user-details">
<div class="panel-user-fullname"><?= $this->escape($panel->user()->fullname()) ?></div>

View File

@ -12,7 +12,7 @@
<div class="component">
<div class="user-summary">
<div class="user-summary-avatar">
<img src="<?= $user->avatar()->uri() ?>">
<img src="<?= $user->image()->uri() ?>">
</div>
<div class="user-summary-data">
<h3><?= $this->escape($user->fullname()) ?></h3>