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
This commit is contained in:
Jonny Barnes 2017-11-13 17:26:38 +00:00
parent a5fc87a135
commit b2c112eb91
8 changed files with 1538 additions and 1526 deletions

View file

@ -36,6 +36,10 @@ php:
- 7.1 - 7.1
- 7.2 - 7.2
matrix:
allow_failures:
- php: 7.2
before_install: before_install:
- printf "\n" | pecl install imagick - printf "\n" | pecl install imagick
- cp .env.travis .env - cp .env.travis .env
@ -49,6 +53,10 @@ install:
- if [[ $setup = 'stable' ]]; then travis_retry composer update --no-interaction --prefer-dist --prefer-stable; fi - if [[ $setup = 'stable' ]]; then travis_retry composer update --no-interaction --prefer-dist --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --no-interaction --prefer-dist --prefer-lowest --prefer-stable; fi - if [[ $setup = 'lowest' ]]; then travis_retry composer update --no-interaction --prefer-dist --prefer-lowest --prefer-stable; fi
- travis/install-nginx.sh - travis/install-nginx.sh
- . $HOME/.nvm/nvm.sh
- nvm install stable
- nvm use stable
- npm i puppeteer
before_script: before_script:
- php artisan key:generate - php artisan key:generate

View file

@ -0,0 +1,10 @@
<?php
namespace App\Exceptions;
use Exception;
class InternetArchiveErrorSavingException extends Exception
{
//
}

View file

@ -3,14 +3,12 @@
namespace App\Jobs; namespace App\Jobs;
use App\Bookmark; use App\Bookmark;
use Ramsey\Uuid\Uuid;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Spatie\Browsershot\Browsershot;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use App\Exceptions\InternetArchiveErrorSavingException;
class ProcessBookmark implements ShouldQueue class ProcessBookmark implements ShouldQueue
{ {
@ -33,24 +31,18 @@ class ProcessBookmark implements ShouldQueue
* *
* @return void * @return void
*/ */
public function handle(Browsershot $browsershot, Client $client) public function handle()
{ {
//save a local screenshot $uuid = (new BookmarkService())->saveScreenshot($this->bookmark->url);
$uuid = Uuid::uuid4();
$browsershot->url($this->bookmark->url)
->windowSize(960, 640)
->save(public_path() . '/assets/img/bookmarks/' . $uuid . '.png');
$this->bookmark->screenshot = $uuid; $this->bookmark->screenshot = $uuid;
//get an internet archive link try {
$response = $client->request('GET', 'https://web.archive.org/save/' . $this->bookmark->url); $archiveLink = (new BookmarkService())->getArchiveLink($this->bookmark->url);
if ($response->hasHeader('Content-Location')) { } catch (InternetArchiveErrorSavingException $e) {
if (starts_with($response->getHeader('Content-Location')[0], '/web')) { $archiveLink = null;
$this->bookmark->archive = $response->getHeader('Content-Location')[0];
}
} }
$this->bookmark->archive = $archiveLink;
//save
$this->bookmark->save(); $this->bookmark->save();
} }
} }

View file

@ -6,10 +6,14 @@ namespace App\Services;
use App\Tag; use App\Tag;
use App\Bookmark; use App\Bookmark;
use Ramsey\Uuid\Uuid;
use GuzzleHttp\Client;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Jobs\ProcessBookmark; use App\Jobs\ProcessBookmark;
use Spatie\Browsershot\Browsershot;
use App\Jobs\SyndicateBookmarkToTwitter; use App\Jobs\SyndicateBookmarkToTwitter;
use App\Jobs\SyndicateBookmarkToFacebook; use App\Jobs\SyndicateBookmarkToFacebook;
use App\Exceptions\InternetArchiveErrorSavingException;
class BookmarkService class BookmarkService
{ {
@ -82,4 +86,38 @@ class BookmarkService
return $bookmark; return $bookmark;
} }
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;
}
} }

View file

@ -1,5 +1,8 @@
# Changelog # Changelog
## Version {next}
- Update Browsershot to v3, uses puppeteer to control Chrome
## Version 0.12.6.1 (2017-11-13) ## Version 0.12.6.1 (2017-11-13)
- `.1` fixes a typo - `.1` fixes a typo
- Fix issue with generating image links from images uploaded to `/api/media` - Fix issue with generating image links from images uploaded to `/api/media`

View file

@ -29,7 +29,7 @@
"predis/predis": "~1.0", "predis/predis": "~1.0",
"ramsey/uuid": "^3.5", "ramsey/uuid": "^3.5",
"sensiolabs/security-checker": "^4.0", "sensiolabs/security-checker": "^4.0",
"spatie/browsershot": "^2.4", "spatie/browsershot": "~3.0",
"thujohn/twitter": "~2.0" "thujohn/twitter": "~2.0"
}, },
"require-dev": { "require-dev": {

2970
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -28,4 +28,13 @@ class BookmarksTest extends TestCase
Queue::assertPushed(ProcessBookmark::class); Queue::assertPushed(ProcessBookmark::class);
$this->assertDatabaseHas('bookmarks', ['url' => 'https://example.org/blog-post']); $this->assertDatabaseHas('bookmarks', ['url' => 'https://example.org/blog-post']);
} }
public function test_screenshot_of_google()
{
$url = 'https://www.google.co.uk';
$uuid = (new \App\Services\BookmarkService())->saveScreenshot($url);
$this->assertTrue(file_exists(public_path() . '/assets/img/bookmarks/' . $uuid . '.png'));
}
} }