2020-08-09 01:10:25 +09:00
< ? php
// {{{ 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 ;
2021-04-30 03:12:32 +09:00
use App\Core\DB\DB ;
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-04-30 03:12:32 +09:00
use App\Entity\Avatar as AvatarEntity ;
use App\Util\Common ;
use App\Util\Exception\NotFoundException ;
2021-04-29 21:55:26 +09:00
use App\Util\TemporaryFile ;
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 ;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile ;
2020-08-09 01:10:25 +09:00
use Symfony\Component\HttpFoundation\Request ;
2021-04-18 10:17:57 +09:00
class Avatar extends Controller
2020-08-09 01:10:25 +09:00
{
2021-04-18 10:17:57 +09:00
/**
* @ throws Exception
*/
2021-04-29 21:55:26 +09:00
public function avatar_view ( Request $request , int $gsactor_id , string $size )
2020-08-09 01:10:25 +09:00
{
switch ( $size ) {
2021-04-18 10:17:57 +09:00
case 'full' :
$res = \Component\Avatar\Avatar :: getAvatarFileInfo ( $gsactor_id );
return M :: sendFile ( $res [ 'file_path' ], $res [ 'mimetype' ], $res [ 'title' ]);
default :
throw new Exception ( 'Not implemented' );
2020-08-09 01:10:25 +09:00
}
}
2021-04-29 21:55:26 +09:00
/**
* Local user avatar panel
*/
public function settings_avatar ( Request $request )
{
$form = Form :: create ([
[ '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-04-29 21:55:26 +09:00
[ 'hidden' , HiddenType :: class , []],
[ 'save' , SubmitType :: class , [ 'label' => _m ( 'Submit' )]],
]);
$form -> handleRequest ( $request );
if ( $form -> isSubmitted () && $form -> isValid ()) {
2021-04-30 03:12:32 +09:00
$data = $form -> getData ();
$user = Common :: user ();
$gsactor_id = $user -> getId ();
2021-04-29 21:55:26 +09:00
if ( $data [ 'remove' ] == true ) {
2021-04-30 03:12:32 +09:00
try {
$avatar = DB :: findOneBy ( 'avatar' , [ 'gsactor_id' => $gsactor_id ]);
$avatar -> delete ();
Event :: handle ( 'DeleteCachedAvatar' , [ $user -> getId ()]);
} catch ( NotFoundException ) {
$form -> addError ( new FormError ( _m ( 'No avatar set, so cannot delete' )));
}
2021-04-29 21:55:26 +09:00
} else {
$sfile = null ;
if ( isset ( $data [ 'hidden' ])) {
// Cropped client side
$matches = [];
if ( ! empty ( preg_match ( '/data:([^;]*)(;(base64))?,(.*)/' , $data [ 'hidden' ], $matches ))) {
list (, $mimetype_user , , $encoding_user , $data_user ) = $matches ;
if ( $encoding_user == 'base64' ) {
$data_user = base64_decode ( $data_user );
$tempfile = new TemporaryFile ( 'avatar' );
$path = $tempfile -> getPath ();
file_put_contents ( $path , $data_user );
$sfile = new SymfonyFile ( $path );
} else {
Log :: info ( 'Avatar upload got an invalid encoding, something\'s fishy and/or wrong' );
}
}
} elseif ( isset ( $data [ 'avatar' ])) {
// Cropping failed (e.g. disabled js), have file as uploaded
$sfile = $data [ 'avatar' ];
} else {
throw new ClientException ( 'Invalid form' );
}
2021-04-30 03:12:32 +09:00
$attachment = GSFile :: validateAndStoreAttachment ( $sfile , Common :: config ( 'avatar' , 'dir' ), $title = null , $is_local = true , $use_unique = $gsactor_id );
$old_attachment = null ;
$avatar = DB :: find ( 'avatar' , [ 'gsactor_id' => $gsactor_id ]);
2021-04-29 21:55:26 +09:00
// Must get old id before inserting another one
if ( $avatar != null ) {
2021-04-30 03:12:32 +09:00
$old_attachment = $avatar -> delete ();
2021-04-29 21:55:26 +09:00
}
2021-04-30 03:12:32 +09:00
DB :: persist ( $attachment );
2021-04-29 21:55:26 +09:00
// Can only get new id after inserting
DB :: flush ();
2021-04-30 03:12:32 +09:00
DB :: persist ( AvatarEntity :: create ([ 'gsactor_id' => $gsactor_id , 'attachment_id' => $attachment -> getId ()]));
2021-04-29 21:55:26 +09:00
DB :: flush ();
// Only delete files if the commit went through
2021-04-30 03:12:32 +09:00
if ( $old_attachment != null ) {
@ unlink ( $old_attachment );
2021-04-29 21:55:26 +09:00
}
2021-04-30 03:12:32 +09:00
Event :: handle ( 'DeleteCachedAvatar' , [ $user -> getId ()]);
2021-04-29 21:55:26 +09:00
}
}
return [ '_template' => 'settings/avatar.html.twig' , 'avatar' => $form -> createView ()];
}
2020-08-09 01:10:25 +09:00
}