Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions vanilla/library/Vanilla/EmbeddedContent/Embeds/FileEmbed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* @copyright 2009-2019 Vanilla Forums Inc.
* @license GPL-2.0-only
*/

namespace Vanilla\EmbeddedContent\Embeds;

use Garden\Schema\Schema;
use Vanilla\EmbeddedContent\AbstractEmbed;
use Vanilla\Models\VanillaMediaSchema;

/**
* File Embed data object.
*/
class FileEmbed extends AbstractEmbed {

const TYPE = "file";

/**
* @inheritdoc
*/
protected function getAllowedTypes(): array {
return [self::TYPE];
}

/**
* @inheritdoc
*/
public function normalizeData(array $data): array {
// The legacy file embeds have everything underneath attributes.
$attributes = $data['attributes'] ?? null;
if ($attributes !== null) {
$data = $attributes + $data;
}

// The `type` field may contain the mime-type data.

return $data;
}

/**
* Render the image out.
*
* @return string
*/
public function renderHtml(): string {
$viewPath = dirname(__FILE__) . '/FileEmbed.twig';
if (function_exists('file_embed_process_url')) {
$this->data['url'] = file_embed_process_url($this->getUrl());
}
return $this->renderTwig($viewPath, [
'url' => $this->data['url'],
'name' => $this->data['name'],
'data' => $this,
]);
}

/**
* @inheritdoc
*/
protected function schema(): Schema {
return new VanillaMediaSchema(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public function renderQuote(): string {
} elseif ($embedType == 'file') {
$fileName = $data['name'] ?? "";
$size = $data['size'] ?? "";
if (function_exists('file_embed_process_url')) {
$sanitizedUrl = file_embed_process_url($sanitizedUrl);
}
if($fileName && $size) {
$formattedSize = Gdn_Upload::formatFileSize($size,2);
return "<p><a href=\"$sanitizedUrl\">$fileName ($formattedSize)</a></p>";
Expand Down