Skip to content

Commit

Permalink
feat(files_sharing): Add twitter meta tags
Browse files Browse the repository at this point in the history
Adds the following twitter meta tags
- `twitter:card`: `summary_large_image` if the shared file is an image & it has a preview, otherwise `summary`
- `twitter:title`: same as `og:title`
- `twitter:description`: same as `og:description`
- `twitter:image`: same as `og:image`

Fixes #49871

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
  • Loading branch information
solonovamax committed Dec 15, 2024
1 parent 16104dc commit 8e7cdf9
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions apps/files_sharing/lib/DefaultPublicShareTemplateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public function renderPage(IShare $share, string $token, string $path): Template
// Allow external apps to register their scripts
$this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($share));

// OpenGraph Support: http://ogp.me/
$this->addOpenGraphHeaders($share);
$this->addMetaHeaders($share);

// CSP to allow office
$csp = new ContentSecurityPolicy();
Expand Down Expand Up @@ -190,15 +189,17 @@ public function renderPage(IShare $share, string $token, string $path): Template
* Add OpenGraph headers to response for preview
* @param IShare $share The share for which to add the headers
*/
protected function addOpenGraphHeaders(IShare $share): void {
protected function addMetaHeaders(IShare $share): void {
$shareNode = $share->getNode();
$token = $share->getToken();
$shareUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]);

// Handle preview generation for OpenGraph
$hasImagePreview = false;
if ($this->previewManager->isMimeSupported($shareNode->getMimetype())) {
// For images we can use direct links
if ($shareNode->getMimePart() === 'image') {
$hasImagePreview = true;
$ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]);
// Whatsapp is kind of picky about their size requirements
if ($this->request->isUserAgent(['/^WhatsApp/'])) {
Expand Down Expand Up @@ -226,11 +227,22 @@ protected function addOpenGraphHeaders(IShare $share): void {
$ogPreview = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png'));
}

Util::addHeader('meta', ['property' => 'og:title', 'content' => $shareNode->getName()]);
Util::addHeader('meta', ['property' => 'og:description', 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]);
Util::addHeader('meta', ['property' => 'og:site_name', 'content' => $this->defaults->getName()]);
$title = $shareNode->getName();
$siteName = $this->defaults->getName();
$description = $siteName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '');

// OpenGraph Support: http://ogp.me/
Util::addHeader('meta', ['property' => 'og:title', 'content' => $title]);
Util::addHeader('meta', ['property' => 'og:description', 'content' => $description]);
Util::addHeader('meta', ['property' => 'og:site_name', 'content' => $siteName]);
Util::addHeader('meta', ['property' => 'og:url', 'content' => $shareUrl]);
Util::addHeader('meta', ['property' => 'og:type', 'content' => 'object']);
Util::addHeader('meta', ['property' => 'og:image', 'content' => $ogPreview]);

// Twitter Support: https://developer.x.com/en/docs/x-for-websites/cards/overview/markup
Util::addHeader('meta', ['property' => 'twitter:title', 'content' => $title]);
Util::addHeader('meta', ['property' => 'twitter:description', 'content' => $description]);
Util::addHeader('meta', ['property' => 'twitter:card', 'content' => $hasImagePreview ? 'summary_large_image' : 'summary']);
Util::addHeader('meta', ['property' => 'twitter:image', 'content' => $ogPreview]);
}
}

0 comments on commit 8e7cdf9

Please sign in to comment.