Skip to content

Commit

Permalink
Merge pull request #261 from Kovah/dev
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
Kovah authored Apr 18, 2021
2 parents 31dc29a + 4542669 commit 85461ae
Show file tree
Hide file tree
Showing 67 changed files with 508 additions and 313 deletions.
2 changes: 1 addition & 1 deletion app/Actions/ImportHtmlBookmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function run(string $data, string $userId, bool $generateMeta = true): bo
}

if ($generateMeta) {
$linkMeta = HtmlMeta::getFromUrl($link['uri']);
$linkMeta = (new HtmlMeta)->getFromUrl($link['uri']);
$title = $link['title'] ?: $linkMeta['title'];
$description = $link['note'] ?: $linkMeta['description'];
} else {
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/CleanupLinkHistoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CleanupLinkHistoriesCommand extends Command
{
protected $signature = 'link:cleanup-histories {field?}';
protected $signature = 'links:cleanup-histories {field?}';

protected $description = 'Removes all but the last 5 entries in the link histories.
{field : If provided, only history entries of that field are deleted}';
Expand Down
51 changes: 51 additions & 0 deletions app/Console/Commands/UpdateLinkThumbnails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Console\Commands;

use App\Helper\HtmlMeta;
use App\Models\Link;
use Illuminate\Console\Command;

class UpdateLinkThumbnails extends Command
{
protected $signature = 'links:update-thumbnails';

protected $description = 'Updates the thumbnails for all existing links, done in batches.';

public function handle()
{
$this->confirm('This command updates the thumbnail for all links with the status "ok". This can take a long time, depending on the amount of links you have saved. Do you want to proceed?');

$totalCount = Link::where('status', Link::STATUS_OK)->count();
$processedLinks = 0;

if ($totalCount === 0) {
$this->warn('No links with status "ok" found. Aborting');
}

$this->comment("Started processing of $totalCount links...");

Link::where('status', Link::STATUS_OK)->latest()
->chunk(100, function ($links) use ($processedLinks, $totalCount) {
foreach ($links as $link) {
$this->updateThumbnailForLink($link);
sleep(1); // Rate limiting of outgoing traffic
}

$processedLinks += count($links);
$this->comment("Processed $processedLinks of $totalCount links.");
});

$this->info('Finished processing all links.');
}

protected function updateThumbnailForLink(Link $link): void
{
$meta = (new HtmlMeta)->getFromUrl($link->url);

if ($meta['thumbnail'] !== null) {
$link->thumbnail = $meta['thumbnail'];
$link->save();
}
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Console\Commands\RegisterUserCommand;
use App\Console\Commands\ResetPasswordCommand;
use App\Console\Commands\ImportCommand;
use App\Console\Commands\UpdateLinkThumbnails;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -28,6 +29,7 @@ class Kernel extends ConsoleKernel
ResetPasswordCommand::class,
CleanupLinkHistoriesCommand::class,
ImportCommand::class,
UpdateLinkThumbnails::class,
];

/**
Expand Down
83 changes: 64 additions & 19 deletions app/Helper/HtmlMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

class HtmlMeta
{
/** @var string */
protected $url;

/** @var array */
protected $fallback;

/** @var array */
protected static $fallback;
protected $meta;

/**
* Get the title and description of an URL.
Expand All @@ -25,60 +31,99 @@ class HtmlMeta
* @param bool $flashAlerts
* @return array
*/
public static function getFromUrl(string $url, bool $flashAlerts = false): array
public function getFromUrl(string $url, bool $flashAlerts = false): array
{
self::buildFallback($url);
$this->url = $url;
$this->buildFallback();

try {
$meta = \Kovah\HtmlMeta\Facades\HtmlMeta::forUrl($url);
$this->meta = \Kovah\HtmlMeta\Facades\HtmlMeta::forUrl($url);
} catch (InvalidUrlException $e) {
Log::warning($url . ': ' . $e->getMessage());
if ($flashAlerts) {
flash(trans('link.added_connection_error'), 'warning');
}
return self::$fallback;
return $this->fallback;
} catch (UnreachableUrlException $e) {
Log::warning($url . ': ' . $e->getMessage());
if ($flashAlerts) {
flash(trans('link.added_request_error'), 'warning');
}
return self::$fallback;
return $this->fallback;
}

return self::buildLinkMeta($meta);
return $this->buildLinkMeta();
}

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

return [
'success' => true,
'title' => $metaTags['title'] ?? self::$fallback['title'],
'description' => $metaTags['description'],
'title' => $this->meta['title'] ?? $this->fallback['title'],
'description' => $this->meta['description'],
'thumbnail' => $this->getThumbnail(),
];
}

/**
* The fallback is used in case of errors while trying to get the link meta.
*
* @param string $url
*/
protected static function buildFallback(string $url): void
protected function buildFallback(): void
{
self::$fallback = [
$this->fallback = [
'success' => false,
'title' => parse_url($url, PHP_URL_HOST) ?? $url,
'title' => parse_url($this->url, PHP_URL_HOST) ?? $this->url,
'description' => false,
'thumbnail' => null,
];
}

/**
* Try to get the thumbnail from the meta tags and handle specific cases where we know how to get a proper image
* from the website.
*
* @return string|null
*/
protected function getThumbnail(): ?string
{
$thumbnail = $this->meta['og:image']
?? $this->meta['twitter:image']
?? null;

if (!is_null($thumbnail) && parse_url($thumbnail, PHP_URL_HOST) === null) {
// If the thumbnail does not contain the domain, add it in front of it
$urlInfo = parse_url($this->url);
$baseUrl = sprintf('%s://%s/', $urlInfo['scheme'], $urlInfo['host']);
$thumbnail = $baseUrl . trim($thumbnail, '/');
}

/*
* Edge case of Youtube only (because of Youtube EU cookie consent)
* Formula based on https://stackoverflow.com/a/2068371, returns Youtube image url
* https://img.youtube.com/vi/[video-id]/mqdefault.jpg
*/
if (is_null($thumbnail)) {
if (str_contains($this->url, 'youtube.com') && str_contains($this->url, 'v=')) {
preg_match('/v=([a-zA-Z0-9]+)/', $this->url, $matched);
$thumbnail = isset($matched[1]) ? 'https://img.youtube.com/vi/' . $matched[1] . '/mqdefault.jpg' : null;
}

if (str_contains($this->url, 'youtu.be')) {
preg_match('/youtu.be\/([a-zA-Z0-9_]+)/', $this->url, $matched);
$thumbnail = isset($matched[1]) ? 'https://img.youtube.com/vi/' . $matched[1] . '/mqdefault.jpg' : null;
}
}

return $thumbnail;
}
}
12 changes: 3 additions & 9 deletions app/Helper/UpdateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class UpdateHelper
{
protected static $releaseApiUrl = 'https://api.github.com/repos/kovah/linkace/releases';
protected const RELEASE_API_URL = 'https://updates.linkace.org/api/current-version';

/**
* Get the current version from the package.json file and cache it for a day.
Expand Down Expand Up @@ -71,14 +71,8 @@ public static function checkForUpdates(bool $cacheResult = false)
*/
protected static function getCurrentVersionFromAPI(): ?string
{
$response = Http::get(self::$releaseApiUrl);
$response = Http::get(self::RELEASE_API_URL);

if (!$response->successful()) {
return null;
}

$releases = $response->json();

return $releases[0]['tag_name'] ?? null;
return $response->successful() ? $response->body() : null;
}
}
2 changes: 1 addition & 1 deletion app/Helper/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function usersettings(string $key = '')
* Retrieve system settings
*
* @param string $key
* @return null|Collection
* @return null|Collection|string
*/
function systemsettings(string $key = '')
{
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/App/BookmarkletController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function getLinkAddForm(Request $request)

session(['bookmarklet.create' => true]);

return view('actions.bookmarklet.create', [
return view('app.bookmarklet.create', [
'bookmark_url' => $newUrl,
'bookmark_title' => $newTitle,
'bookmark_description' => $newDescription,
Expand All @@ -56,7 +56,7 @@ public function getLinkAddForm(Request $request)
*/
public function getCompleteView(): View
{
return view('actions.bookmarklet.complete');
return view('app.bookmarklet.complete');
}

/**
Expand All @@ -66,6 +66,6 @@ public function getCompleteView(): View
*/
public function getLoginForm(): View
{
return view('actions.bookmarklet.login');
return view('app.bookmarklet.login');
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/App/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ExportController extends Controller
*/
public function getExport(): View
{
return view('actions.export.export');
return view('app.export.export');
}

/**
Expand All @@ -39,7 +39,7 @@ public function doHtmlExport(Request $request): StreamedResponse
{
$links = Link::orderBy('title', 'asc')->with('tags')->get();

$fileContent = view()->make('actions.export.html-export', ['links' => $links])->render();
$fileContent = view()->make('app.export.html-export', ['links' => $links])->render();
$fileName = config('app.name') . '_export.html';

return response()->streamDownload(function () use ($fileContent) {
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/App/FeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function links(Request $request): Response
'id' => $request->fullUrl(),
];

return new Response(view('actions.feed.links', [
return new Response(view('app.feed.links', [
'meta' => $meta,
'links' => $links,
]), 200, ['Content-Type' => 'application/xml']);
Expand All @@ -37,7 +37,7 @@ public function lists(Request $request): Response
'id' => $request->fullUrl(),
];

return new Response(view('actions.feed.lists', [
return new Response(view('app.feed.lists', [
'meta' => $meta,
'lists' => $lists,
]), 200, ['Content-Type' => 'application/xml']);
Expand All @@ -53,7 +53,7 @@ public function listLinks(Request $request, LinkList $list): Response
'id' => $request->fullUrl(),
];

return new Response(view('actions.feed.links', [
return new Response(view('app.feed.links', [
'meta' => $meta,
'links' => $links,
]), 200, ['Content-Type' => 'application/xml']);
Expand All @@ -69,7 +69,7 @@ public function tags(Request $request): Response
'id' => $request->fullUrl(),
];

return new Response(view('actions.feed.tags', [
return new Response(view('app.feed.tags', [
'meta' => $meta,
'tags' => $tags,
]), 200, ['Content-Type' => 'application/xml']);
Expand All @@ -85,7 +85,7 @@ public function tagLinks(Request $request, Tag $tag): Response
'id' => $request->fullUrl(),
];

return new Response(view('actions.feed.links', [
return new Response(view('app.feed.links', [
'meta' => $meta,
'links' => $links,
]), 200, ['Content-Type' => 'application/xml']);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/App/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ImportController extends Controller
*/
public function getImport(): View
{
return view('actions.import.import');
return view('app.import.import');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/App/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SearchController extends Controller
*/
public function getSearch(): View
{
return view('actions.search.search')
return view('app.search.search')
->with('results', collect([]))
->with('order_by_options', $this->orderByOptions)
->with('query_settings', [
Expand Down Expand Up @@ -46,7 +46,7 @@ public function doSearch(SearchRequest $request): View
$search = $this->buildDatabaseQuery($request);
$results = $search->paginate(getPaginationLimit());

return view('actions.search.search')
return view('app.search.search')
->with('results', $results)
->with('order_by_options', $this->orderByOptions)
->with('query_settings', [
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/App/SystemSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SystemSettingsController extends Controller
*/
public function getSystemSettings(): View
{
return view('actions.settings.system', [
return view('app.settings.system', [
'linkaceVersion' => UpdateHelper::currentVersion(),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/App/TrashController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function index(): View
->byUser(auth()->id())
->get();

return view('actions.trash.index', [
return view('app.trash.index', [
'links' => $links,
'lists' => $lists,
'tags' => $tags,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/App/UserSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getUserSettings(): View
{
$bookmarkletCode = LinkAce::generateBookmarkletCode();

return view('actions.settings.user', [
return view('app.settings.user', [
'user' => auth()->user(),
'bookmarklet_code' => $bookmarkletCode,
]);
Expand Down
Loading

0 comments on commit 85461ae

Please sign in to comment.