|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright 2009-2019 Vanilla Forums Inc. |
| 4 | + * @license GPL-2.0-only |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Vanilla\EmbeddedContent\Embeds; |
| 8 | + |
| 9 | +use Garden\Schema\Schema; |
| 10 | +use Vanilla\EmbeddedContent\AbstractEmbed; |
| 11 | +use Vanilla\Models\VanillaMediaSchema; |
| 12 | + |
| 13 | +/** |
| 14 | + * File Embed data object. |
| 15 | + */ |
| 16 | +class FileEmbed extends AbstractEmbed { |
| 17 | + |
| 18 | + const TYPE = "file"; |
| 19 | + |
| 20 | + /** |
| 21 | + * @inheritdoc |
| 22 | + */ |
| 23 | + protected function getAllowedTypes(): array { |
| 24 | + return [self::TYPE]; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @inheritdoc |
| 29 | + */ |
| 30 | + public function normalizeData(array $data): array { |
| 31 | + // The legacy file embeds have everything underneath attributes. |
| 32 | + $attributes = $data['attributes'] ?? null; |
| 33 | + if ($attributes !== null) { |
| 34 | + $data = $attributes + $data; |
| 35 | + } |
| 36 | + |
| 37 | + // The `type` field may contain the mime-type data. |
| 38 | + |
| 39 | + return $data; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Render the image out. |
| 44 | + * |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + public function renderHtml(): string { |
| 48 | + $viewPath = dirname(__FILE__) . '/FileEmbed.twig'; |
| 49 | + if (function_exists('file_embed_process_url')) { |
| 50 | + $this->data['url'] = file_embed_process_url($this->getUrl()); |
| 51 | + } |
| 52 | + return $this->renderTwig($viewPath, [ |
| 53 | + 'url' => $this->data['url'], |
| 54 | + 'name' => $this->data['name'], |
| 55 | + 'data' => $this, |
| 56 | + ]); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritdoc |
| 61 | + */ |
| 62 | + protected function schema(): Schema { |
| 63 | + return new VanillaMediaSchema(false); |
| 64 | + } |
| 65 | +} |
0 commit comments