From 036e9cb58eaf4c184fb50894b1e83f1d09953166 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Tue, 17 Aug 2021 21:46:09 +0100 Subject: [PATCH] [Avatar] Preserve uploaded filename and use Avatar's own route instead of attachment --- components/Avatar/Avatar.php | 26 ++++++++++++------------- components/Avatar/Controller/Avatar.php | 4 ++-- components/Avatar/Entity/Avatar.php | 18 +++++++++++++++++ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/components/Avatar/Avatar.php b/components/Avatar/Avatar.php index 918c231f19..677e426623 100644 --- a/components/Avatar/Avatar.php +++ b/components/Avatar/Avatar.php @@ -28,8 +28,6 @@ use App\Core\Router\Router; use App\Util\Common; use Component\Avatar\Controller as C; use Component\Avatar\Exception\NoAvatarException; -use Symfony\Component\Asset\Package; -use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy; use Symfony\Component\HttpFoundation\Request; class Avatar extends Component @@ -96,17 +94,11 @@ class Avatar extends Component /** * Get the cached avatar associated with the given GSActor id, or the current user if not given */ - public static function getAvatarUrl(?int $gsactor_id = null): string + public static function getAvatarUrl(?int $gsactor_id = null, string $size = 'full'): string { $gsactor_id = $gsactor_id ?: Common::userId(); return Cache::get("avatar-url-{$gsactor_id}", function () use ($gsactor_id) { - $attachment_id = self::getAvatarFileInfo($gsactor_id)['id']; - if ($attachment_id !== null) { - return Router::url('attachment_view', ['id' => $attachment_id]); - } else { - $package = new Package(new EmptyVersionStrategy()); - return $package->getUrl(Common::config('avatar', 'default')); - } + return Router::url('avatar', ['gsactor_id' => $gsactor_id, 'size' => 'full']); }); } @@ -120,7 +112,7 @@ class Avatar extends Component { $res = Cache::get("avatar-file-info-{$gsactor_id}", function () use ($gsactor_id) { - return DB::dql('select f.id, f.filename, f.mimetype ' . + return DB::dql('select f.id, f.filename, a.filename title, f.mimetype ' . 'from App\Entity\Attachment f ' . 'join Component\Avatar\Entity\Avatar a with f.id = a.attachment_id ' . 'where a.gsactor_id = :gsactor_id', @@ -129,10 +121,16 @@ class Avatar extends Component ); if ($res === []) { // Avatar not found $filepath = INSTALLDIR . '/public/assets/default-avatar.svg'; - return ['id' => null, 'file_path' => $filepath, 'mimetype' => 'image/svg+xml', 'title' => null]; + return [ + 'id' => null, + 'filepath' => $filepath, + 'mimetype' => 'image/svg+xml', + 'filename' => null, + 'title' => 'default_avatar.svg', + ]; } else { - $res = $res[0]; // A user must always only have one avatar. - $res['file_path'] = DB::findOneBy('attachment', ['id' => $res['id']])->getPath(); + $res = $res[0]; // A user must always only have one avatar. + $res['filepath'] = DB::findOneBy('attachment', ['id' => $res['id']])->getPath(); return $res; } } diff --git a/components/Avatar/Controller/Avatar.php b/components/Avatar/Controller/Avatar.php index 6fef8d170b..678af08010 100644 --- a/components/Avatar/Controller/Avatar.php +++ b/components/Avatar/Controller/Avatar.php @@ -53,7 +53,7 @@ class Avatar extends Controller switch ($size) { case 'full': $res = \Component\Avatar\Avatar::getAvatarFileInfo($gsactor_id); - return M::sendFile($res['file_path'], $res['mimetype'], $res['title']); + return M::sendFile($res['filepath'], $res['mimetype'], $res['title']); default: throw new Exception('Not implemented'); } @@ -114,7 +114,7 @@ class Avatar extends Controller DB::persist($attachment); // Can only get new id after inserting DB::flush(); - DB::persist(AvatarEntity::create(['gsactor_id' => $gsactor_id, 'attachment_id' => $attachment->getId()])); + DB::persist(AvatarEntity::create(['gsactor_id' => $gsactor_id, 'attachment_id' => $attachment->getId(), 'filename' => $file->getClientOriginalName()])); DB::flush(); Event::handle('AvatarUpdate', [$user->getId()]); } diff --git a/components/Avatar/Entity/Avatar.php b/components/Avatar/Entity/Avatar.php index 061799c0c4..defa860058 100644 --- a/components/Avatar/Entity/Avatar.php +++ b/components/Avatar/Entity/Avatar.php @@ -48,6 +48,7 @@ class Avatar extends Entity // @codeCoverageIgnoreStart private int $gsactor_id; private int $attachment_id; + private ?string $filename; private DateTimeInterface $created; private DateTimeInterface $modified; @@ -73,6 +74,22 @@ class Avatar extends Entity return $this->attachment_id; } + /** + * @return null|string + */ + public function getFilename(): ?string + { + return $this->filename; + } + + /** + * @param null|string $filename + */ + public function setFilename(?string $filename): void + { + $this->filename = $filename; + } + public function setCreated(DateTimeInterface $created): self { $this->created = $created; @@ -142,6 +159,7 @@ class Avatar extends Entity 'fields' => [ 'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to gsactor table'], 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to attachment table'], + 'filename' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'], 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'], ],