2020-08-09 01:10:25 +09:00
< ? php
2021-10-10 17:26:18 +09:00
declare ( strict_types = 1 );
2020-08-09 01:10:25 +09:00
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
2021-04-18 10:17:57 +09:00
namespace Component\Avatar\Controller ;
2020-08-09 01:10:25 +09:00
use App\Core\Controller ;
2022-03-27 23:19:09 +09:00
use App\Core\DB ;
2021-04-30 03:12:32 +09:00
use App\Core\Event ;
2021-04-29 21:55:26 +09:00
use App\Core\Form ;
2021-04-30 03:12:32 +09:00
use App\Core\GSFile ;
2021-04-18 10:17:57 +09:00
use App\Core\GSFile as M ;
2021-04-29 21:55:26 +09:00
use function App\Core\I18n\_m ;
2021-08-05 03:12:37 +09:00
use App\Core\Log ;
2021-04-30 03:12:32 +09:00
use App\Util\Common ;
2021-08-05 03:12:37 +09:00
use App\Util\Exception\ClientException ;
2021-04-29 21:55:26 +09:00
use App\Util\TemporaryFile ;
2021-08-05 03:12:37 +09:00
use Component\Avatar\Entity\Avatar as AvatarEntity ;
2020-08-09 01:10:25 +09:00
use Exception ;
2021-04-29 21:55:26 +09:00
use Symfony\Component\Form\Extension\Core\Type\CheckboxType ;
use Symfony\Component\Form\Extension\Core\Type\FileType ;
use Symfony\Component\Form\Extension\Core\Type\HiddenType ;
use Symfony\Component\Form\Extension\Core\Type\SubmitType ;
2021-04-30 03:12:32 +09:00
use Symfony\Component\Form\FormError ;
2020-08-09 01:10:25 +09:00
use Symfony\Component\HttpFoundation\Request ;
2021-08-05 03:12:37 +09:00
use Symfony\Component\HttpFoundation\Response ;
2020-08-09 01:10:25 +09:00
2021-04-18 10:17:57 +09:00
class Avatar extends Controller
2020-08-09 01:10:25 +09:00
{
2021-09-21 01:01:36 +09:00
public function default_avatar_view ( Request $request , string $size ) : Response
{
return $this -> avatar_view ( $request , 0 , $size );
}
2021-04-18 10:17:57 +09:00
/**
* @ throws Exception
*/
2021-09-18 11:22:27 +09:00
public function avatar_view ( Request $request , int $actor_id , string $size ) : Response
2020-08-09 01:10:25 +09:00
{
2021-12-27 13:47:04 +09:00
$res = \Component\Avatar\Avatar :: getAvatarFileInfo ( $actor_id , $size );
return M :: sendFile ( $res [ 'filepath' ], $res [ 'mimetype' ], $res [ 'title' ]);
2020-08-09 01:10:25 +09:00
}
2021-04-29 21:55:26 +09:00
/**
* Local user avatar panel
*/
2021-08-05 03:12:37 +09:00
public static function settings_avatar ( Request $request ) : array
2021-04-29 21:55:26 +09:00
{
$form = Form :: create ([
2021-07-30 04:20:32 +09:00
[ 'avatar' , FileType :: class , [ 'label' => _m ( 'Avatar' ), 'help' => _m ( 'You can upload your personal avatar. The maximum file size is 2MB.' ), 'multiple' => false , 'required' => false ]],
2021-04-30 03:12:32 +09:00
[ 'remove' , CheckboxType :: class , [ 'label' => _m ( 'Remove avatar' ), 'help' => _m ( 'Remove your avatar and use the default one' ), 'required' => false , 'value' => false ]],
2021-07-30 04:20:32 +09:00
[ 'hidden' , HiddenType :: class , []],
2021-08-19 02:38:54 +09:00
[ 'save_avatar' , SubmitType :: class , [ 'label' => _m ( 'Submit' )]],
2021-04-29 21:55:26 +09:00
]);
$form -> handleRequest ( $request );
if ( $form -> isSubmitted () && $form -> isValid ()) {
2021-09-18 11:22:27 +09:00
$data = $form -> getData ();
$user = Common :: user ();
$actor_id = $user -> getId ();
2021-04-29 21:55:26 +09:00
if ( $data [ 'remove' ] == true ) {
2022-01-02 12:06:07 +09:00
if ( \is_null ( $avatar = DB :: findOneBy ( AvatarEntity :: class , [ 'actor_id' => $actor_id ], return_null : true ))) {
$form -> addError ( new FormError ( _m ( 'No avatar set, so cannot delete.' )));
} else {
2021-04-30 03:12:32 +09:00
$avatar -> delete ();
}
2021-04-29 21:55:26 +09:00
} else {
2021-09-07 04:59:36 +09:00
$attachment = null ;
2022-01-02 12:06:07 +09:00
$title = $data [ 'avatar' ] ? -> getClientOriginalName () ? ? null ;
2021-04-29 21:55:26 +09:00
if ( isset ( $data [ 'hidden' ])) {
// Cropped client side
$matches = [];
if ( ! empty ( preg_match ( '/data:([^;]*)(;(base64))?,(.*)/' , $data [ 'hidden' ], $matches ))) {
2021-10-10 17:26:18 +09:00
[, , , $encoding_user , $data_user ] = $matches ;
2021-07-21 05:17:53 +09:00
if ( $encoding_user === 'base64' ) {
2021-04-29 21:55:26 +09:00
$data_user = base64_decode ( $data_user );
2021-07-21 05:17:53 +09:00
$tempfile = new TemporaryFile ([ 'prefix' => 'gs-avatar' ]);
$tempfile -> write ( $data_user );
2021-09-22 23:04:45 +09:00
$attachment = GSFile :: storeFileAsAttachment ( $tempfile );
2021-04-29 21:55:26 +09:00
} else {
Log :: info ( 'Avatar upload got an invalid encoding, something\'s fishy and/or wrong' );
}
}
} elseif ( isset ( $data [ 'avatar' ])) {
2021-08-05 03:12:37 +09:00
// Cropping failed (e.g. disabled js), use file as uploaded
2021-09-07 04:59:36 +09:00
$file = $data [ 'avatar' ];
2021-09-22 23:04:45 +09:00
$attachment = GSFile :: storeFileAsAttachment ( $file );
2021-04-29 21:55:26 +09:00
} else {
2022-01-02 12:06:07 +09:00
throw new ClientException ( _m ( 'Invalid form.' ));
2021-04-29 21:55:26 +09:00
}
2021-08-05 11:15:07 +09:00
// Delete current avatar if there's one
2022-01-02 12:06:07 +09:00
if ( ! \is_null ( $avatar = DB :: findOneBy ( AvatarEntity :: class , [ 'actor_id' => $actor_id ], return_null : true ))) {
$avatar -> delete ();
}
2021-04-30 03:12:32 +09:00
DB :: persist ( $attachment );
2022-01-02 12:06:07 +09:00
DB :: persist ( AvatarEntity :: create ([
'actor_id' => $actor_id ,
'attachment_id' => $attachment -> getId (),
'title' => $title ,
]));
2021-04-29 21:55:26 +09:00
DB :: flush ();
2021-08-05 11:15:07 +09:00
Event :: handle ( 'AvatarUpdate' , [ $user -> getId ()]);
2021-04-29 21:55:26 +09:00
}
}
2021-12-09 02:53:29 +09:00
return [ '_template' => 'avatar/settings.html.twig' , 'avatar' => $form -> createView ()];
2021-04-29 21:55:26 +09:00
}
2020-08-09 01:10:25 +09:00
}