Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display a little thumbnail next to the title, using the image from the metatags #262

Merged
merged 3 commits into from
Apr 17, 2021
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
22 changes: 20 additions & 2 deletions app/Helper/HtmlMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,43 @@ public static function getFromUrl(string $url, bool $flashAlerts = false): array
return self::$fallback;
}

return self::buildLinkMeta($meta);
return self::buildLinkMeta($meta, $url);
}

/**
* Build a response array containing the link meta including a success flag.
*
* @param array $metaTags
* @param string $url
* @return array
*/
protected static function buildLinkMeta(array $metaTags): array
protected static function buildLinkMeta(array $metaTags, string $url): array
{
$metaTags['description'] = $metaTags['description']
?? $metaTags['og:description']
?? $metaTags['twitter:description']
?? null;

$thumbnail = $metaTags['og:image']
?? $metaTags['twitter:image']
?? null;

//Edge case of Youtube only (because of Youtube EU cookie consent)
if (str_contains($url, 'youtube')
&& str_contains($url, 'v=')
&& is_null($thumbnail)
) {
//Formula based on https://stackoverflow.com/a/2068371
$explode = explode('v=', $url);
//https://img.youtube.com/vi/[video-id]/mqdefault.jpg
$thumbnail = 'https://img.youtube.com/vi/' . $explode[1] . '/mqdefault.jpg';
}

return [
'success' => true,
'title' => $metaTags['title'] ?? self::$fallback['title'],
'description' => $metaTags['description'],
'thumbnail' => $thumbnail,
];
}

Expand All @@ -79,6 +96,7 @@ protected static function buildFallback(string $url): void
'success' => false,
'title' => parse_url($url, PHP_URL_HOST) ?? $url,
'description' => false,
'thumbnail' => null,
];
}
}
1 change: 1 addition & 0 deletions app/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Link extends Model
'is_private',
'status',
'check_disabled',
'thumbnail',
];

protected $casts = [
Expand Down
1 change: 1 addition & 0 deletions app/Repositories/LinkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function create(array $data, bool $flashAlerts = false): Link
$data['description'] = $data['description'] ?? $linkMeta['description'];
$data['user_id'] = auth()->user()->id;
$data['icon'] = LinkIconMapper::mapLink($data['url']);
$data['thumbnail'] = $linkMeta['thumbnail'];

// If the meta helper was not successfull, disable future checks and set the status to broken
if ($linkMeta['success'] === false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddThumbnailColumnToLinksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('links', function (Blueprint $table) {
$table->string('thumbnail', 255)->nullable()->default(null)->after('icon');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('links', function (Blueprint $table) {
$table->dropColumn('thumbnail');
});
}
}
19 changes: 19 additions & 0 deletions resources/assets/sass/custom/_app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,22 @@ code {
#loader {
display: none !important;
}

.link-thumbnail-detail {
width: 180px;
height: 100px;
background-size: cover !important;
}

.link-thumbnail-list-holder {
width: 100%;
@include media-breakpoint-up('md') {
width: auto;
}

.link-thumbnail-list {
width: 100px;
height: 75px;
background-size: cover !important;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div class="card mb-4">

<div class="card-header">
<div class="d-flex align-items-top flex-wrap">
<div class="d-flex align-items-top flex-wrap flex-md-nowrap">
@if($link->thumbnail)
<div class="d-flex justify-content-center mr-2 mb-2 mb-md-0 link-thumbnail-list-holder">
<a href="{{ $link->url }}" {!! linkTarget() !!} class="border rounded d-block link-thumbnail-list" style="background: url('{{ $link->thumbnail }}') no-repeat center;">
</a>
</div>
@endif
<div class="mr-2 mw-100">
@if($link->is_private)
<span>
Expand Down
10 changes: 9 additions & 1 deletion resources/views/models/links/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
<div class="col-12 col-md-8">
<div class="card">
<div class="card-body">
<div class="d-sm-flex mb-3">
<div class="d-flex flex-column flex-lg-row mb-3">
@if($link->thumbnail)
<div class="mt-1 mr-2 align-self-center">
<a href="{{ $link->url }}" {!! linkTarget() !!}
class="border rounded d-block link-thumbnail-detail" style="background: url('{{ $link->thumbnail }}') no-repeat center;">
</a>
</div>

@endif
<div class="d-sm-inline-block mt-1 mb-2 mb-sm-0">
{!! $link->getIcon('mr-1 mr-sm-2') !!}
@if($link->is_private)
Expand Down