almost finish bookmark view
This commit is contained in:
@@ -21,6 +21,10 @@ class BookmarkController extends Controller
|
||||
{
|
||||
$this->authorize('create', Bookmark::class);
|
||||
|
||||
$metas = $this->getMeta($request->url);
|
||||
$request['name'] = (empty($request->name)) ? $metas['title'] : $request->name;
|
||||
$request['favicon'] = $metas['favicon'];
|
||||
|
||||
$bookmark = request()->user()->bookmarks()->create($request->all());
|
||||
|
||||
return (new BookmarkResource($bookmark))
|
||||
@@ -54,4 +58,32 @@ class BookmarkController extends Controller
|
||||
|
||||
return response([], 204);
|
||||
}
|
||||
|
||||
private function getMeta($url)
|
||||
{
|
||||
$metas = [];
|
||||
$client = new \GuzzleHttp\Client();
|
||||
$promise = $client->requestAsync('GET', $url);
|
||||
$response = $promise->wait();
|
||||
$page = $response->getBody()->getContents();
|
||||
preg_match("/\<title\>(.*)\<\/title\>/i",$page,$title);
|
||||
$metas['title'] = $title[1];
|
||||
|
||||
|
||||
preg_match('/\<link rel="[icon|appel-touch](.*)\"\>/i',$page,$favicon);
|
||||
if(isset($favicon[1])) {
|
||||
preg_match('/href="(.*)/i',$favicon[1],$favicon);
|
||||
$metas['favicon'] = $favicon[1];
|
||||
|
||||
preg_match('/http/', $metas['favicon'], $matches);
|
||||
if(empty($matches)) {
|
||||
$metas['favicon'] = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST).$metas['favicon'];
|
||||
}
|
||||
// dd($metas['favicon'], $matches, !isset($matches[1]), empty($matches));
|
||||
} else {
|
||||
$metas['favicon'] = null;
|
||||
}
|
||||
|
||||
return $metas;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user