Take a screenshot of the bookmark
This commit is contained in:
parent
a5d3ba7829
commit
33cf91f6d5
10 changed files with 1854 additions and 1456 deletions
|
@ -7,6 +7,7 @@ use Monolog\Logger;
|
|||
use Ramsey\Uuid\Uuid;
|
||||
use App\Jobs\ProcessImage;
|
||||
use App\Services\LikeService;
|
||||
use App\Services\BookmarkService;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use App\{Like, Media, Note, Place};
|
||||
use Intervention\Image\ImageManager;
|
||||
|
|
45
app/Jobs/ProcessBookmark.php
Normal file
45
app/Jobs/ProcessBookmark.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Bookmark;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Spatie\Image\Manipulations;
|
||||
use Spatie\Browsershot\Browsershot;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class ProcessBookmark implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $bookmark;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Bookmark $bookmark)
|
||||
{
|
||||
$this->bookmark = $bookmark;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Browsershot $browsershot)
|
||||
{
|
||||
$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->save();
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Tag;
|
||||
use App\Bookmark;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Jobs\ProcessBookmark;
|
||||
|
@ -15,7 +16,7 @@ class BookmarkService
|
|||
*
|
||||
* @param Request $request
|
||||
*/
|
||||
public function createLike(Request $request): Bookmark
|
||||
public function createBookmark(Request $request): Bookmark
|
||||
{
|
||||
if ($request->header('Content-Type') == 'application/json') {
|
||||
//micropub request
|
||||
|
@ -25,14 +26,14 @@ class BookmarkService
|
|||
$categories = $request->input('properties.category');
|
||||
}
|
||||
if (
|
||||
($request->header('Content-Type') == 'x-www-url-formencoded')
|
||||
($request->header('Content-Type') == 'application/x-www-form-urlencoded')
|
||||
||
|
||||
($request->header('Content-Type') == 'multipart/form-data')
|
||||
(str_contains($request->header('Content-Type'), 'multipart/form-data'))
|
||||
) {
|
||||
$url = normalize_url($request->input('bookmark-of'));
|
||||
$name = $request->input('name');
|
||||
$content = $request->input('content');
|
||||
$categories = $request->input('category[]');
|
||||
$categories = $request->input('category');
|
||||
}
|
||||
|
||||
$bookmark = Bookmark::create([
|
||||
|
@ -41,13 +42,13 @@ class BookmarkService
|
|||
'content' => $content,
|
||||
]);
|
||||
|
||||
foreach($categories as $category) {
|
||||
foreach((array) $categories as $category) {
|
||||
$tag = Tag::firstOrCreate(['tag' => $category]);
|
||||
$bookmark->tags()->save($tag);
|
||||
}
|
||||
|
||||
ProcessBookmark::dispatch($bookmark);
|
||||
|
||||
return $Bookmark;
|
||||
return $bookmark;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
"predis/predis": "~1.0",
|
||||
"ramsey/uuid": "^3.5",
|
||||
"sensiolabs/security-checker": "^4.0",
|
||||
"spatie/browsershot": "^2.4",
|
||||
"thujohn/twitter": "~2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
|
|
3211
composer.lock
generated
3211
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -18,6 +18,7 @@ class CreateBookmarksTable extends Migration
|
|||
$table->string('url');
|
||||
$table->string('name')->nullable();
|
||||
$table->text('content')->nullable();
|
||||
$table->uuid('screenshot')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
1
public/assets/img/bookmarks/.gitignore
vendored
Normal file
1
public/assets/img/bookmarks/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.png
|
|
@ -20,6 +20,9 @@ Bookmarks «
|
|||
@isset($bookmark->content)
|
||||
<p>{{ $bookmark->content }}</p>
|
||||
@endisset
|
||||
@isset($bookmark->screenshot)
|
||||
<img src="/assets/img/bookmarks/{{ $bookmark->screenshot }}.png">
|
||||
@endisset
|
||||
@if($bookmark->tags_count > 0)
|
||||
<ul>
|
||||
@foreach($bookmark->tags as $tag)
|
||||
|
|
|
@ -18,6 +18,9 @@ Bookmark «
|
|||
@isset($bookmark->content)
|
||||
<p>{{ $bookmark->content }}</p>
|
||||
@endisset
|
||||
@isset($bookmark->screenshot)
|
||||
<img src="/assets/img/bookmarks/{{ $bookmark->screenshot }}.png">
|
||||
@endisset
|
||||
@if(count($bookmark->tags) > 0)
|
||||
<ul>
|
||||
@foreach($bookmark->tags as $tag)
|
||||
|
|
31
tests/Feature/BookmarksTest.php
Normal file
31
tests/Feature/BookmarksTest.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Tests\TestToken;
|
||||
use App\Jobs\ProcessBookmark;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class BookmarksTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions, TestToken;
|
||||
|
||||
public function test_browsershot_job_dispatches_when_bookmark_added()
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer ' . $this->getToken(),
|
||||
])->post('/api/post', [
|
||||
'h' => 'entry',
|
||||
'bookmark-of' => 'https://example.org/blog-post',
|
||||
]);
|
||||
|
||||
$response->assertJson(['response' => 'created']);
|
||||
|
||||
Queue::assertPushed(ProcessBookmark::class);
|
||||
$this->assertDatabaseHas('bookmarks', ['url' => 'https://example.org/blog-post']);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue