jonnybarnes.uk/app/Services/BookmarkService.php

124 lines
4 KiB
PHP
Raw Normal View History

2017-10-12 18:33:08 +01:00
<?php
declare(strict_types=1);
namespace App\Services;
2017-10-13 12:31:31 +01:00
use App\Tag;
2017-10-12 18:33:08 +01:00
use App\Bookmark;
Squashed commit of the following: commit 6186d13cae259221f1948207acfdc109d3f71b11 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 17:01:48 2017 +0000 Fixed borwsershot so run whole test suite again commit fe8241368e514de7c28e6aaebe45757cea7f3fa0 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:56:51 2017 +0000 Use nvm to install node commit 391f4d3fa08906b3d0d905ae98448ce30e9cbc89 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:47:19 2017 +0000 specify node version 8 commit 5e4df05b3985e069d605c9287f392f3370f27c13 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:59:00 2017 +0000 Why didn’t filter work commit 9fd5f2f6b29c77ad5d9a2c2dc03dbf05702e24fa Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:53:25 2017 +0000 Move npm install to before_script and only test browsershot commit bd04bcb835f436b080c02098c68042f71511ec4f Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:43:04 2017 +0000 Remove a var_dump statement commit 3904dcced167512868817fbe530b0de017ea1bc4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:36:21 2017 +0000 Install pupeteer on Travis commit f01389fbfece436522597457c8a303756925a3d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:56 2017 +0000 Allow failures on php 7.2 commit 4eafee0b47ebe428d2471bfe3ba71d7446e35092 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:37 2017 +0000 Update chagelog commit fabe8793f50edcc8b20a2cea5b0e76be9794eb25 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:49 2017 +0000 Add a test for taking screenshots commit 888b9546ecf5c000aa96099b009c4b7cd00f08ad Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:27 2017 +0000 Move logic for taking screenshots and getting archive links to the BookmarkService class, this cleans up with acutal job commit 7650fd3eda229b1a1e1e17f0667d203294384551 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:49:33 2017 +0000 Update browsershot to v3
2017-11-13 17:26:38 +00:00
use Ramsey\Uuid\Uuid;
use GuzzleHttp\Client;
2017-10-12 18:33:08 +01:00
use Illuminate\Http\Request;
use App\Jobs\ProcessBookmark;
Squashed commit of the following: commit 6186d13cae259221f1948207acfdc109d3f71b11 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 17:01:48 2017 +0000 Fixed borwsershot so run whole test suite again commit fe8241368e514de7c28e6aaebe45757cea7f3fa0 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:56:51 2017 +0000 Use nvm to install node commit 391f4d3fa08906b3d0d905ae98448ce30e9cbc89 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:47:19 2017 +0000 specify node version 8 commit 5e4df05b3985e069d605c9287f392f3370f27c13 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:59:00 2017 +0000 Why didn’t filter work commit 9fd5f2f6b29c77ad5d9a2c2dc03dbf05702e24fa Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:53:25 2017 +0000 Move npm install to before_script and only test browsershot commit bd04bcb835f436b080c02098c68042f71511ec4f Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:43:04 2017 +0000 Remove a var_dump statement commit 3904dcced167512868817fbe530b0de017ea1bc4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:36:21 2017 +0000 Install pupeteer on Travis commit f01389fbfece436522597457c8a303756925a3d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:56 2017 +0000 Allow failures on php 7.2 commit 4eafee0b47ebe428d2471bfe3ba71d7446e35092 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:37 2017 +0000 Update chagelog commit fabe8793f50edcc8b20a2cea5b0e76be9794eb25 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:49 2017 +0000 Add a test for taking screenshots commit 888b9546ecf5c000aa96099b009c4b7cd00f08ad Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:27 2017 +0000 Move logic for taking screenshots and getting archive links to the BookmarkService class, this cleans up with acutal job commit 7650fd3eda229b1a1e1e17f0667d203294384551 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:49:33 2017 +0000 Update browsershot to v3
2017-11-13 17:26:38 +00:00
use Spatie\Browsershot\Browsershot;
2017-10-13 13:45:57 +01:00
use App\Jobs\SyndicateBookmarkToTwitter;
use App\Jobs\SyndicateBookmarkToFacebook;
Squashed commit of the following: commit 6186d13cae259221f1948207acfdc109d3f71b11 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 17:01:48 2017 +0000 Fixed borwsershot so run whole test suite again commit fe8241368e514de7c28e6aaebe45757cea7f3fa0 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:56:51 2017 +0000 Use nvm to install node commit 391f4d3fa08906b3d0d905ae98448ce30e9cbc89 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:47:19 2017 +0000 specify node version 8 commit 5e4df05b3985e069d605c9287f392f3370f27c13 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:59:00 2017 +0000 Why didn’t filter work commit 9fd5f2f6b29c77ad5d9a2c2dc03dbf05702e24fa Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:53:25 2017 +0000 Move npm install to before_script and only test browsershot commit bd04bcb835f436b080c02098c68042f71511ec4f Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:43:04 2017 +0000 Remove a var_dump statement commit 3904dcced167512868817fbe530b0de017ea1bc4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:36:21 2017 +0000 Install pupeteer on Travis commit f01389fbfece436522597457c8a303756925a3d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:56 2017 +0000 Allow failures on php 7.2 commit 4eafee0b47ebe428d2471bfe3ba71d7446e35092 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:37 2017 +0000 Update chagelog commit fabe8793f50edcc8b20a2cea5b0e76be9794eb25 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:49 2017 +0000 Add a test for taking screenshots commit 888b9546ecf5c000aa96099b009c4b7cd00f08ad Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:27 2017 +0000 Move logic for taking screenshots and getting archive links to the BookmarkService class, this cleans up with acutal job commit 7650fd3eda229b1a1e1e17f0667d203294384551 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:49:33 2017 +0000 Update browsershot to v3
2017-11-13 17:26:38 +00:00
use App\Exceptions\InternetArchiveErrorSavingException;
2017-10-12 18:33:08 +01:00
class BookmarkService
{
/**
* Create a new Bookmark.
*
* @param Request $request
*/
2017-10-13 12:31:31 +01:00
public function createBookmark(Request $request): Bookmark
2017-10-12 18:33:08 +01:00
{
if ($request->header('Content-Type') == 'application/json') {
//micropub request
$url = normalize_url($request->input('properties.bookmark-of.0'));
$name = $request->input('properties.name.0');
$content = $request->input('properties.content.0');
$categories = $request->input('properties.category');
}
if (
2017-10-13 12:31:31 +01:00
($request->header('Content-Type') == 'application/x-www-form-urlencoded')
2017-10-12 18:33:08 +01:00
||
2017-10-13 12:31:31 +01:00
(str_contains($request->header('Content-Type'), 'multipart/form-data'))
2017-10-12 18:33:08 +01:00
) {
$url = normalize_url($request->input('bookmark-of'));
$name = $request->input('name');
$content = $request->input('content');
2017-10-13 12:31:31 +01:00
$categories = $request->input('category');
2017-10-12 18:33:08 +01:00
}
$bookmark = Bookmark::create([
'url' => $url,
'name' => $name,
'content' => $content,
]);
2017-10-13 19:50:22 +00:00
foreach ((array) $categories as $category) {
2017-10-12 18:33:08 +01:00
$tag = Tag::firstOrCreate(['tag' => $category]);
$bookmark->tags()->save($tag);
}
2017-10-13 13:45:57 +01:00
$targets = array_pluck(config('syndication.targets'), 'uid', 'service.name');
$mpSyndicateTo = null;
if ($request->has('mp-syndicate-to')) {
$mpSyndicateTo = $request->input('mp-syndicate-to');
}
if ($request->has('properties.mp-syndicate-to')) {
$mpSyndicateTo = $request->input('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);
}
}
}
2017-10-12 18:33:08 +01:00
ProcessBookmark::dispatch($bookmark);
2017-10-13 12:31:31 +01:00
return $bookmark;
2017-10-12 18:33:08 +01:00
}
Squashed commit of the following: commit 6186d13cae259221f1948207acfdc109d3f71b11 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 17:01:48 2017 +0000 Fixed borwsershot so run whole test suite again commit fe8241368e514de7c28e6aaebe45757cea7f3fa0 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:56:51 2017 +0000 Use nvm to install node commit 391f4d3fa08906b3d0d905ae98448ce30e9cbc89 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 16:47:19 2017 +0000 specify node version 8 commit 5e4df05b3985e069d605c9287f392f3370f27c13 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:59:00 2017 +0000 Why didn’t filter work commit 9fd5f2f6b29c77ad5d9a2c2dc03dbf05702e24fa Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:53:25 2017 +0000 Move npm install to before_script and only test browsershot commit bd04bcb835f436b080c02098c68042f71511ec4f Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:43:04 2017 +0000 Remove a var_dump statement commit 3904dcced167512868817fbe530b0de017ea1bc4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 14:36:21 2017 +0000 Install pupeteer on Travis commit f01389fbfece436522597457c8a303756925a3d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:56 2017 +0000 Allow failures on php 7.2 commit 4eafee0b47ebe428d2471bfe3ba71d7446e35092 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:52:37 2017 +0000 Update chagelog commit fabe8793f50edcc8b20a2cea5b0e76be9794eb25 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:49 2017 +0000 Add a test for taking screenshots commit 888b9546ecf5c000aa96099b009c4b7cd00f08ad Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:50:27 2017 +0000 Move logic for taking screenshots and getting archive links to the BookmarkService class, this cleans up with acutal job commit 7650fd3eda229b1a1e1e17f0667d203294384551 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Nov 13 13:49:33 2017 +0000 Update browsershot to v3
2017-11-13 17:26:38 +00:00
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();
}
public function getArchiveLink(string $url): string
{
$client = new Client();
$response = $client->request('GET', 'https://web.archive.org/save/' . $url);
if ($response->hasHeader('Content-Location')) {
if (starts_with($response->getHeader('Content-Location')[0], '/web')) {
return $response->getHeader('Content-Location')[0];
}
}
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 InternetArchiveErrorSavingException;
}
2017-10-12 18:33:08 +01:00
}