AttachmentEmbed.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. /**
  19. * OembedPlugin implementation for GNU social
  20. *
  21. * @package GNUsocial
  22. *
  23. * @author Stephen Paul Weber
  24. * @author Mikael Nordfeldth
  25. * @author Diogo Cordeiro <diogo@fc.up.pt>
  26. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\Embed\Entity;
  30. use App\Core\Entity;
  31. use App\Core\GSFile;
  32. use App\Core\Router\Router;
  33. use App\Util\Common;
  34. use DateTimeInterface;
  35. /**
  36. * Table Definition for attachment_embed
  37. *
  38. * @author Hugo Sales <hugo@hsal.es>
  39. * @copyright 2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  40. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  41. */
  42. class AttachmentEmbed extends Entity
  43. {
  44. // {{{ Autocode
  45. // @codeCoverageIgnoreStart
  46. private int $attachment_id;
  47. private ?string $mimetype;
  48. private ?string $filename;
  49. private ?string $provider;
  50. private ?string $provider_url;
  51. private ?int $width;
  52. private ?int $height;
  53. private ?string $html;
  54. private ?string $title;
  55. private ?string $author_name;
  56. private ?string $author_url;
  57. private ?string $media_url;
  58. private \DateTimeInterface $modified;
  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 setMimetype(?string $mimetype): self
  69. {
  70. $this->mimetype = $mimetype;
  71. return $this;
  72. }
  73. public function getMimetype(): ?string
  74. {
  75. return $this->mimetype;
  76. }
  77. public function setFilename(?string $filename): self
  78. {
  79. $this->filename = $filename;
  80. return $this;
  81. }
  82. public function getFilename(): ?string
  83. {
  84. return $this->filename;
  85. }
  86. public function setProvider(?string $provider): self
  87. {
  88. $this->provider = $provider;
  89. return $this;
  90. }
  91. public function getProvider(): ?string
  92. {
  93. return $this->provider;
  94. }
  95. public function setProviderUrl(?string $provider_url): self
  96. {
  97. $this->provider_url = $provider_url;
  98. return $this;
  99. }
  100. public function getProviderUrl(): ?string
  101. {
  102. return $this->provider_url;
  103. }
  104. public function setWidth(?int $width): self
  105. {
  106. $this->width = $width;
  107. return $this;
  108. }
  109. public function getWidth(): ?int
  110. {
  111. return $this->width;
  112. }
  113. public function setHeight(?int $height): self
  114. {
  115. $this->height = $height;
  116. return $this;
  117. }
  118. public function getHeight(): ?int
  119. {
  120. return $this->height;
  121. }
  122. public function setHtml(?string $html): self
  123. {
  124. $this->html = $html;
  125. return $this;
  126. }
  127. public function getHtml(): ?string
  128. {
  129. return $this->html;
  130. }
  131. public function setTitle(?string $title): self
  132. {
  133. $this->title = $title;
  134. return $this;
  135. }
  136. public function getTitle(): ?string
  137. {
  138. return $this->title;
  139. }
  140. public function setAuthorName(?string $author_name): self
  141. {
  142. $this->author_name = $author_name;
  143. return $this;
  144. }
  145. public function getAuthorName(): ?string
  146. {
  147. return $this->author_name;
  148. }
  149. public function setAuthorUrl(?string $author_url): self
  150. {
  151. $this->author_url = $author_url;
  152. return $this;
  153. }
  154. public function getAuthorUrl(): ?string
  155. {
  156. return $this->author_url;
  157. }
  158. public function setMediaUrl(?string $media_url): self
  159. {
  160. $this->media_url = $media_url;
  161. return $this;
  162. }
  163. public function getMediaUrl(): ?string
  164. {
  165. return $this->media_url;
  166. }
  167. public function setModified(DateTimeInterface $modified): self
  168. {
  169. $this->modified = $modified;
  170. return $this;
  171. }
  172. public function getModified(): DateTimeInterface
  173. {
  174. return $this->modified;
  175. }
  176. // @codeCoverageIgnoreEnd
  177. // }}} Autocode
  178. public function getAttachmentUrl()
  179. {
  180. return Router::url('attachment_view', ['id' => $this->getAttachmentId()]);
  181. }
  182. public function isImage()
  183. {
  184. return isset($this->mimetype) && GSFile::mimetypeMajor($this->mimetype) == 'image';
  185. }
  186. /**
  187. * Get the HTML attributes for this attachment
  188. */
  189. public function getImageHTMLAttributes(array $orig = [], bool $overwrite = true)
  190. {
  191. if ($this->isImage()) {
  192. $attrs = [
  193. 'height' => $this->getHeight(),
  194. 'width' => $this->getWidth(),
  195. 'src' => $this->getAttachmentUrl(),
  196. ];
  197. return $overwrite ? array_merge($orig, $attrs) : array_merge($attrs, $orig);
  198. } else {
  199. return false;
  200. }
  201. }
  202. public function getFilepath()
  203. {
  204. return Common::config('storage', 'dir') . $this->filename;
  205. }
  206. public static function schemaDef()
  207. {
  208. return [
  209. 'name' => 'attachment_embed',
  210. 'fields' => [
  211. 'attachment_id' => ['type' => 'int', 'not null' => true, 'description' => 'Embed for that URL/file'],
  212. 'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
  213. 'filename' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'],
  214. 'provider' => ['type' => 'text', 'description' => 'name of this oEmbed provider'],
  215. 'provider_url' => ['type' => 'text', 'description' => 'URL of this oEmbed provider'],
  216. 'width' => ['type' => 'int', 'description' => 'width of oEmbed resource when available'],
  217. 'height' => ['type' => 'int', 'description' => 'height of oEmbed resource when available'],
  218. 'html' => ['type' => 'text', 'description' => 'html representation of this Embed resource when applicable'],
  219. 'title' => ['type' => 'text', 'description' => 'title of Embed resource when available'],
  220. 'author_name' => ['type' => 'text', 'description' => 'author name for this Embed resource'],
  221. 'author_url' => ['type' => 'text', 'description' => 'author URL for this Embed resource'],
  222. 'media_url' => ['type' => 'text', 'description' => 'URL for this Embed resource when applicable (photo, link)'],
  223. 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'],
  224. ],
  225. 'primary key' => ['attachment_id'],
  226. 'foreign keys' => [
  227. 'attachment_embed_attachment_id_fkey' => ['attachment', ['attachment_id' => 'id']],
  228. ],
  229. ];
  230. }
  231. }