[UserPanel] Fix upload of avatar

This commit is contained in:
Hugo Sales 2020-08-14 22:37:45 +00:00 committed by Hugo Sales
parent 30deeaf4ef
commit 96aa98cbcf

View File

@ -1,6 +1,7 @@
<?php <?php
// {{{ License // {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social // 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 // GNU social is free software: you can redistribute it and/or modify
@ -15,6 +16,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // 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/>. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}} // }}}
/** /**
@ -33,6 +35,7 @@ namespace App\Controller;
// {{{ Imports // {{{ Imports
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
@ -139,22 +142,25 @@ class UserPanel extends AbstractController
} else { } else {
throw new ClientException('Invalid form'); throw new ClientException('Invalid form');
} }
$actor_id = Common::actor()->getId(); $actor = Common::actor();
$file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $file_title); $actor_id = $actor->getId();
$avatar = null; $file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $file_title, $is_local = true, $use_unique = $actor_id);
try { $old_file = null;
$avatar = DB::find('avatar', ['actor_id' => $actor_id]); $avatar = DB::find('avatar', ['gsactor_id' => $actor_id]);
} catch (Exception $e) { // Must get old id before inserting another one
}
if ($avatar != null) { if ($avatar != null) {
$avatar->delete(); $old_file = $avatar->delete();
} else {
DB::persist($file);
DB::persist(Avatar::create(['actor_id' => $actor_id, 'file_id' => $file->getId()]));
} }
DB::persist($file);
// Can only get new id after inserting
DB::flush();
DB::persist(Avatar::create(['gsactor_id' => $actor_id, 'file_id' => $file->getId()]));
DB::flush(); DB::flush();
// Only delete files if the commit went through // Only delete files if the commit went through
File::deleteFiles($fs_files_to_delete ?? []); if ($old_file != null) {
@unlink($old_file);
}
Cache::delete('avatar-' . $actor->getNickname());
} }
return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()]; return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()];