[Avatar] Preserve uploaded filename and use Avatar's own route instead of attachment

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-17 21:46:09 +01:00 committed by Hugo Sales
parent f70eb8f12d
commit 036e9cb58e
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 32 additions and 16 deletions

View File

@ -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;
}
}

View File

@ -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()]);
}

View File

@ -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'],
],