Avatar.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Avatar\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use App\Core\Router\Router;
  24. use Component\Attachment\Entity\Attachment;
  25. use App\Util\Common;
  26. use DateTimeInterface;
  27. /**
  28. * Entity for user's avatar
  29. *
  30. * @category DB
  31. * @package GNUsocial
  32. *
  33. * @author Zach Copley <zach@status.net>
  34. * @copyright 2010 StatusNet Inc.
  35. * @author Mikael Nordfeldth <mmn@hethane.se>
  36. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  37. * @author Hugo Sales <hugo@hsal.es>
  38. * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
  39. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  40. */
  41. class Avatar extends Entity
  42. {
  43. // {{{ Autocode
  44. // @codeCoverageIgnoreStart
  45. private int $actor_id;
  46. private int $attachment_id;
  47. private ?string $title;
  48. private DateTimeInterface $created;
  49. private DateTimeInterface $modified;
  50. public function setActorId(int $actor_id): self
  51. {
  52. $this->actor_id = $actor_id;
  53. return $this;
  54. }
  55. public function getActorId(): int
  56. {
  57. return $this->actor_id;
  58. }
  59. public function setAttachmentId(int $attachment_id): self
  60. {
  61. $this->attachment_id = $attachment_id;
  62. return $this;
  63. }
  64. public function getAttachmentId(): int
  65. {
  66. return $this->attachment_id;
  67. }
  68. public function getTitle(): ?string
  69. {
  70. return $this->title;
  71. }
  72. public function setTitle(?string $title): void
  73. {
  74. $this->title = $title;
  75. }
  76. public function setCreated(DateTimeInterface $created): self
  77. {
  78. $this->created = $created;
  79. return $this;
  80. }
  81. public function getCreated(): DateTimeInterface
  82. {
  83. return $this->created;
  84. }
  85. public function setModified(DateTimeInterface $modified): self
  86. {
  87. $this->modified = $modified;
  88. return $this;
  89. }
  90. public function getModified(): DateTimeInterface
  91. {
  92. return $this->modified;
  93. }
  94. // @codeCoverageIgnoreEnd
  95. // }}} Autocode
  96. private ?Attachment $attachment = null;
  97. public function getUrl(string $size = 'full', int $type = Router::ABSOLUTE_PATH): string
  98. {
  99. $actor_id = $this->getActorId();
  100. return Cache::get("avatar-url-{$actor_id}-{$size}-{$type}", fn () => Router::url('avatar_actor', ['actor_id' => $actor_id, 'size' => $size], $type));
  101. }
  102. public function getAttachment(): Attachment
  103. {
  104. $this->attachment ??= DB::findOneBy('attachment', ['id' => $this->attachment_id]);
  105. return $this->attachment;
  106. }
  107. public static function getFilePathStatic(string $filename): string
  108. {
  109. return Common::config('avatar', 'dir') . $filename;
  110. }
  111. public function getPath(): string
  112. {
  113. return Common::config('avatar', 'dir') . $this->getAttachment()->getFileName();
  114. }
  115. /**
  116. * Delete this avatar and kill corresponding attachment
  117. */
  118. public function delete(): bool
  119. {
  120. DB::remove($this);
  121. $attachment = $this->getAttachment();
  122. $attachment->kill();
  123. DB::flush();
  124. return true;
  125. }
  126. public static function schemaDef(): array
  127. {
  128. return [
  129. 'name' => 'avatar',
  130. 'fields' => [
  131. 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to actor table'],
  132. 'attachment_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Attachment.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'foreign key to attachment table'],
  133. 'title' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'],
  134. 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created', 'default' => 'CURRENT_TIMESTAMP'],
  135. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified', 'default' => 'CURRENT_TIMESTAMP'],
  136. ],
  137. 'primary key' => ['actor_id'],
  138. 'indexes' => [
  139. 'avatar_attachment_id_idx' => ['attachment_id'],
  140. ],
  141. ];
  142. }
  143. }