Skip to content

Commit

Permalink
Generate screenshots for links Kovah#18
Browse files Browse the repository at this point in the history
First step into implementing a "screenshot" of the added links, so this commits makes possible to retrieve the images that exists on the metatags and store it as the thumbnail.
  • Loading branch information
jeop10 committed Apr 16, 2021
1 parent a36e683 commit 98567b8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
20 changes: 18 additions & 2 deletions app/Helper/HtmlMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,42 @@ 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')) {
if (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 Down
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

0 comments on commit 98567b8

Please sign in to comment.