Squashed commit of the following: commit 74ed84617fcbecf661695763323e50d049a88db7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:46:29 2018 +0000 Test passes so remove the dump statement commit a7d3323be02da64f76e8ec88713e3de84a13ded7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:40:35 2018 +0000 Values with spaces need to be quoted commit 58a120bb238f14346793c388b948b7351d3b51fd Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:37:23 2018 +0000 We need a diplay name for the tests to work now we are using strict type checking commit b46f177053bd697db9a4835d073f2f37e088b26f Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:31:29 2018 +0000 Get travis to show more info about failing test commit 60323f3ce5a0561329a1721ee94821571cdcc86a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:23:27 2018 +0000 Remove un-used namnepsace imports commit 096d3505920bc94ff8677c77430eca0aae0be58a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:21:55 2018 +0000 we need php7.2 for object type-hint commit bb818bc19c73d02d510af9f002199f5718a54608 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:15:48 2018 +0000 Added lots of strict_types
132 lines
4.2 KiB
PHP
132 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
use GuzzleHttp\Client;
|
|
use Illuminate\Http\Request;
|
|
use App\Jobs\ProcessBookmark;
|
|
use App\Models\{Bookmark, Tag};
|
|
use Spatie\Browsershot\Browsershot;
|
|
use App\Jobs\SyndicateBookmarkToTwitter;
|
|
use App\Jobs\SyndicateBookmarkToFacebook;
|
|
use GuzzleHttp\Exception\ClientException;
|
|
use App\Exceptions\InternetArchiveException;
|
|
|
|
class BookmarkService
|
|
{
|
|
/**
|
|
* Create a new Bookmark.
|
|
*
|
|
* @param array $request Data from request()->all()
|
|
* @return Bookmark $bookmark
|
|
*/
|
|
public function createBookmark(array $request): Bookmark
|
|
{
|
|
if (array_get($request, 'properties.bookmark-of.0')) {
|
|
//micropub request
|
|
$url = normalize_url(array_get($request, 'properties.bookmark-of.0'));
|
|
$name = array_get($request, 'properties.name.0');
|
|
$content = array_get($request, 'properties.content.0');
|
|
$categories = array_get($request, 'properties.category');
|
|
}
|
|
if (array_get($request, 'bookmark-of')) {
|
|
$url = normalize_url(array_get($request, 'bookmark-of'));
|
|
$name = array_get($request, 'name');
|
|
$content = array_get($request, 'content');
|
|
$categories = array_get($request, 'category');
|
|
}
|
|
|
|
$bookmark = Bookmark::create([
|
|
'url' => $url,
|
|
'name' => $name,
|
|
'content' => $content,
|
|
]);
|
|
|
|
foreach ((array) $categories as $category) {
|
|
$tag = Tag::firstOrCreate(['tag' => $category]);
|
|
$bookmark->tags()->save($tag);
|
|
}
|
|
|
|
$targets = array_pluck(config('syndication.targets'), 'uid', 'service.name');
|
|
$mpSyndicateTo = null;
|
|
if (array_get($request, 'mp-syndicate-to')) {
|
|
$mpSyndicateTo = array_get($request, 'mp-syndicate-to');
|
|
}
|
|
if (array_get($request, 'properties.mp-syndicate-to')) {
|
|
$mpSyndicateTo = array_get($request, 'properties.mp-syndicate-to');
|
|
}
|
|
if (is_string($mpSyndicateTo)) {
|
|
$service = array_search($mpSyndicateTo, $targets);
|
|
if ($service == 'Twitter') {
|
|
SyndicateBookmarkToTwitter::dispatch($bookmark);
|
|
}
|
|
if ($service == 'Facebook') {
|
|
SyndicateBookmarkToFacebook::dispatch($bookmark);
|
|
}
|
|
}
|
|
if (is_array($mpSyndicateTo)) {
|
|
foreach ($mpSyndicateTo as $uid) {
|
|
$service = array_search($uid, $targets);
|
|
if ($service == 'Twitter') {
|
|
SyndicateBookmarkToTwitter::dispatch($bookmark);
|
|
}
|
|
if ($service == 'Facebook') {
|
|
SyndicateBookmarkToFacebook::dispatch($bookmark);
|
|
}
|
|
}
|
|
}
|
|
|
|
ProcessBookmark::dispatch($bookmark);
|
|
|
|
return $bookmark;
|
|
}
|
|
|
|
/**
|
|
* Given a URL, use browsershot to save an image of the page.
|
|
*
|
|
* @param string $url
|
|
* @return string The uuid for the screenshot
|
|
*/
|
|
public function saveScreenshot(string $url): string
|
|
{
|
|
$browsershot = new Browsershot();
|
|
|
|
$uuid = Uuid::uuid4();
|
|
|
|
$browsershot->url($url)
|
|
->setIncludePath('$PATH:/usr/local/bin')
|
|
->noSandbox()
|
|
->windowSize(960, 640)
|
|
->save(public_path() . '/assets/img/bookmarks/' . $uuid . '.png');
|
|
|
|
return $uuid->toString();
|
|
}
|
|
|
|
/**
|
|
* Given a URL, attempt to save it to the Internet Archive.
|
|
*
|
|
* @param string $url
|
|
* @return string
|
|
*/
|
|
public function getArchiveLink(string $url): string
|
|
{
|
|
$client = resolve(Client::class);
|
|
try {
|
|
$response = $client->request('GET', 'https://web.archive.org/save/' . $url);
|
|
} catch (ClientException $e) {
|
|
//throw an exception to be caught
|
|
throw new InternetArchiveException;
|
|
}
|
|
if ($response->hasHeader('Content-Location')) {
|
|
if (starts_with(array_get($response->getHeader('Content-Location'), 0), '/web')) {
|
|
return $response->getHeader('Content-Location')[0];
|
|
}
|
|
}
|
|
|
|
//throw an exception to be caught
|
|
throw new InternetArchiveException;
|
|
}
|
|
}
|