mirror of https://github.com/nextcloud/bookmarks
Refactor HTTP I/O to use nextcloud's IClient
=> Compat with NC 14
This commit is contained in:
parent
77a067f70c
commit
06d3ce7263
|
@ -1,8 +1,12 @@
|
|||
{
|
||||
"require": {
|
||||
"marcelklehr/link-preview": "^2",
|
||||
"marcelklehr/link-preview": "^3",
|
||||
"guzzlehttp/psr7": "^1",
|
||||
"glenscott/url-normalizer": "^1.4",
|
||||
"doctrine/inflector": "1.1.*",
|
||||
"pguardiario/phpuri": "1.0.*"
|
||||
}
|
||||
"pguardiario/phpuri": "1.0.*",
|
||||
"psr/http-message": "^1.0",
|
||||
"psr/http-factory": "^1.0",
|
||||
"psr/http-client": "^0.2.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "2b2e4153d174a8f74c090cdd2589746e",
|
||||
"content-hash": "dd0576ca6e4822c68d6d771f96f997a5",
|
||||
"packages": [
|
||||
{
|
||||
"name": "doctrine/inflector",
|
||||
|
@ -111,33 +111,42 @@
|
|||
"time": "2015-06-11T16:06:02+00:00"
|
||||
},
|
||||
{
|
||||
"name": "guzzlehttp/guzzle",
|
||||
"version": "5.3.3",
|
||||
"name": "guzzlehttp/psr7",
|
||||
"version": "1.4.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/guzzle/guzzle.git",
|
||||
"reference": "93bbdb30d59be6cd9839495306c65f2907370eb9"
|
||||
"url": "https://github.com/guzzle/psr7.git",
|
||||
"reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/93bbdb30d59be6cd9839495306c65f2907370eb9",
|
||||
"reference": "93bbdb30d59be6cd9839495306c65f2907370eb9",
|
||||
"url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
|
||||
"reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"guzzlehttp/ringphp": "^1.1",
|
||||
"php": ">=5.4.0",
|
||||
"react/promise": "^2.2"
|
||||
"psr/http-message": "~1.0"
|
||||
},
|
||||
"provide": {
|
||||
"psr/http-message-implementation": "1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"ext-curl": "*",
|
||||
"phpunit/phpunit": "^4.0"
|
||||
"phpunit/phpunit": "~4.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.4-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"GuzzleHttp\\": "src/"
|
||||
}
|
||||
"GuzzleHttp\\Psr7\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/functions_include.php"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
|
@ -148,121 +157,23 @@
|
|||
"name": "Michael Dowling",
|
||||
"email": "mtdowling@gmail.com",
|
||||
"homepage": "https://github.com/mtdowling"
|
||||
},
|
||||
{
|
||||
"name": "Tobias Schultze",
|
||||
"homepage": "https://github.com/Tobion"
|
||||
}
|
||||
],
|
||||
"description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients",
|
||||
"homepage": "http://guzzlephp.org/",
|
||||
"description": "PSR-7 message implementation that also provides common utility methods",
|
||||
"keywords": [
|
||||
"client",
|
||||
"curl",
|
||||
"framework",
|
||||
"http",
|
||||
"http client",
|
||||
"rest",
|
||||
"web service"
|
||||
"message",
|
||||
"request",
|
||||
"response",
|
||||
"stream",
|
||||
"uri",
|
||||
"url"
|
||||
],
|
||||
"time": "2018-07-31T13:33:10+00:00"
|
||||
},
|
||||
{
|
||||
"name": "guzzlehttp/ringphp",
|
||||
"version": "1.1.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/guzzle/RingPHP.git",
|
||||
"reference": "5e2a174052995663dd68e6b5ad838afd47dd615b"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/guzzle/RingPHP/zipball/5e2a174052995663dd68e6b5ad838afd47dd615b",
|
||||
"reference": "5e2a174052995663dd68e6b5ad838afd47dd615b",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"guzzlehttp/streams": "~3.0",
|
||||
"php": ">=5.4.0",
|
||||
"react/promise": "~2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"ext-curl": "*",
|
||||
"phpunit/phpunit": "~4.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-curl": "Guzzle will use specific adapters if cURL is present"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.1-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"GuzzleHttp\\Ring\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Michael Dowling",
|
||||
"email": "mtdowling@gmail.com",
|
||||
"homepage": "https://github.com/mtdowling"
|
||||
}
|
||||
],
|
||||
"description": "Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function.",
|
||||
"time": "2018-07-31T13:22:33+00:00"
|
||||
},
|
||||
{
|
||||
"name": "guzzlehttp/streams",
|
||||
"version": "3.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/guzzle/streams.git",
|
||||
"reference": "47aaa48e27dae43d39fc1cea0ccf0d84ac1a2ba5"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/guzzle/streams/zipball/47aaa48e27dae43d39fc1cea0ccf0d84ac1a2ba5",
|
||||
"reference": "47aaa48e27dae43d39fc1cea0ccf0d84ac1a2ba5",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.0-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"GuzzleHttp\\Stream\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Michael Dowling",
|
||||
"email": "mtdowling@gmail.com",
|
||||
"homepage": "https://github.com/mtdowling"
|
||||
}
|
||||
],
|
||||
"description": "Provides a simple abstraction over streams of data",
|
||||
"homepage": "http://guzzlephp.org/",
|
||||
"keywords": [
|
||||
"Guzzle",
|
||||
"stream"
|
||||
],
|
||||
"time": "2014-10-12T19:18:40+00:00"
|
||||
"time": "2017-03-20T17:10:46+00:00"
|
||||
},
|
||||
{
|
||||
"name": "illuminate/contracts",
|
||||
|
@ -365,22 +276,23 @@
|
|||
},
|
||||
{
|
||||
"name": "marcelklehr/link-preview",
|
||||
"version": "v2.0.6",
|
||||
"version": "dev-master",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/marcelklehr/link-preview.git",
|
||||
"reference": "5c13f929b7c9fadad3a14aac55b7f5cff4afcfb5"
|
||||
"reference": "b3cf3895d78d49e227497c69918e92d49fb1be82"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/marcelklehr/link-preview/zipball/5c13f929b7c9fadad3a14aac55b7f5cff4afcfb5",
|
||||
"reference": "5c13f929b7c9fadad3a14aac55b7f5cff4afcfb5",
|
||||
"url": "https://api.github.com/repos/marcelklehr/link-preview/zipball/b3cf3895d78d49e227497c69918e92d49fb1be82",
|
||||
"reference": "b3cf3895d78d49e227497c69918e92d49fb1be82",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^5.3",
|
||||
"illuminate/support": "5.4.*",
|
||||
"php": ">=5.5.0",
|
||||
"psr/http-client": "^0.2.0",
|
||||
"psr/http-message": "^1.0",
|
||||
"symfony/css-selector": "^3.0",
|
||||
"symfony/dom-crawler": "^3.0"
|
||||
},
|
||||
|
@ -414,7 +326,7 @@
|
|||
"scraping",
|
||||
"url"
|
||||
],
|
||||
"time": "2018-05-27T14:54:54+00:00"
|
||||
"time": "2018-09-01T20:53:52+00:00"
|
||||
},
|
||||
{
|
||||
"name": "paragonie/random_compat",
|
||||
|
@ -490,33 +402,33 @@
|
|||
"time": "2015-05-24T09:13:17+00:00"
|
||||
},
|
||||
{
|
||||
"name": "react/promise",
|
||||
"version": "v2.7.0",
|
||||
"name": "psr/http-client",
|
||||
"version": "0.2.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/reactphp/promise.git",
|
||||
"reference": "f4edc2581617431aea50430749db55cc3fc031b3"
|
||||
"url": "https://github.com/php-fig/http-client.git",
|
||||
"reference": "82cf0dd2052a44cab9ea400e61ea7c3f5fdc0f8f"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/reactphp/promise/zipball/f4edc2581617431aea50430749db55cc3fc031b3",
|
||||
"reference": "f4edc2581617431aea50430749db55cc3fc031b3",
|
||||
"url": "https://api.github.com/repos/php-fig/http-client/zipball/82cf0dd2052a44cab9ea400e61ea7c3f5fdc0f8f",
|
||||
"reference": "82cf0dd2052a44cab9ea400e61ea7c3f5fdc0f8f",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.8"
|
||||
"php": "^7.0",
|
||||
"psr/http-message": "^1.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.0.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"React\\Promise\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/functions_include.php"
|
||||
]
|
||||
"Psr\\Http\\Client\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
|
@ -524,20 +436,125 @@
|
|||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jan Sorgalla",
|
||||
"email": "jsorgalla@gmail.com"
|
||||
"name": "PHP-FIG",
|
||||
"homepage": "http://www.php-fig.org/"
|
||||
}
|
||||
],
|
||||
"description": "A lightweight implementation of CommonJS Promises/A for PHP",
|
||||
"description": "Common interface for HTTP clients",
|
||||
"homepage": "https://github.com/php-fig/http-client",
|
||||
"keywords": [
|
||||
"promise",
|
||||
"promises"
|
||||
"http",
|
||||
"http-client",
|
||||
"psr",
|
||||
"psr-18"
|
||||
],
|
||||
"time": "2018-06-13T15:59:06+00:00"
|
||||
"time": "2018-08-14T08:56:48+00:00"
|
||||
},
|
||||
{
|
||||
"name": "psr/http-factory",
|
||||
"version": "1.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-fig/http-factory.git",
|
||||
"reference": "378bfe27931ecc54ff824a20d6f6bfc303bbd04c"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-fig/http-factory/zipball/378bfe27931ecc54ff824a20d6f6bfc303bbd04c",
|
||||
"reference": "378bfe27931ecc54ff824a20d6f6bfc303bbd04c",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.0.0",
|
||||
"psr/http-message": "^1.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.0.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Psr\\Http\\Message\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "PHP-FIG",
|
||||
"homepage": "http://www.php-fig.org/"
|
||||
}
|
||||
],
|
||||
"description": "Common interfaces for PSR-7 HTTP message factories",
|
||||
"keywords": [
|
||||
"factory",
|
||||
"http",
|
||||
"message",
|
||||
"psr",
|
||||
"psr-17",
|
||||
"psr-7",
|
||||
"request",
|
||||
"response"
|
||||
],
|
||||
"time": "2018-07-30T21:54:04+00:00"
|
||||
},
|
||||
{
|
||||
"name": "psr/http-message",
|
||||
"version": "1.0.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-fig/http-message.git",
|
||||
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
|
||||
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.0.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Psr\\Http\\Message\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "PHP-FIG",
|
||||
"homepage": "http://www.php-fig.org/"
|
||||
}
|
||||
],
|
||||
"description": "Common interface for HTTP messages",
|
||||
"homepage": "https://github.com/php-fig/http-message",
|
||||
"keywords": [
|
||||
"http",
|
||||
"http-message",
|
||||
"psr",
|
||||
"psr-7",
|
||||
"request",
|
||||
"response"
|
||||
],
|
||||
"time": "2016-08-06T14:39:51+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/css-selector",
|
||||
"version": "v3.4.14",
|
||||
"version": "v3.4.15",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/css-selector.git",
|
||||
|
@ -590,7 +607,7 @@
|
|||
},
|
||||
{
|
||||
"name": "symfony/dom-crawler",
|
||||
"version": "v3.4.14",
|
||||
"version": "v3.4.15",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/dom-crawler.git",
|
||||
|
@ -766,7 +783,9 @@
|
|||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"stability-flags": {
|
||||
"marcelklehr/link-preview": 20
|
||||
},
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
|
|
|
@ -526,7 +526,7 @@ class Bookmarks {
|
|||
foreach (['https://', 'http://'] as $protocol) {
|
||||
$testUrl = $protocol . $url;
|
||||
$data = $this->getURLMetadata($testUrl);
|
||||
if (isset($data['title'])) {
|
||||
if (isset($data['basic']) && isset($data['basic']['title'])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -540,10 +540,10 @@ class Bookmarks {
|
|||
$url = $data['url'];
|
||||
}
|
||||
if ((!isset($title) || trim($title) === '')) {
|
||||
$title = isset($data['title'])? $data['title'] : $url;
|
||||
$title = isset($data['basic']) && isset($data['basic']['title'])? $data['basic']['title'] : $url;
|
||||
}
|
||||
if (isset($data['description']) && (!isset($description) || trim($description) === '')) {
|
||||
$description = $data['description'];
|
||||
if (isset($data['basic']['description']) && (!isset($description) || trim($description) === '')) {
|
||||
$description = $data['basic']['description'];
|
||||
}
|
||||
|
||||
// Check if it is a valid URL (after adding http(s) prefix)
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\Bookmarks\Controller\Lib\Http;
|
||||
|
||||
use OCP\Http\Client\IClient;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\Psr7;
|
||||
|
||||
class Client implements ClientInterface {
|
||||
protected $nextcloudClient;
|
||||
|
||||
public function __construct(IClient $nextcloudClient) {
|
||||
$this->nextcloudClient = $nextcloudClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a PSR-7 request and returns a PSR-7 response.
|
||||
*
|
||||
* Every technically correct HTTP response MUST be returned as-is, even if it represents an HTTP
|
||||
* error response or a redirect instruction. The only special case is 1xx responses, which MUST
|
||||
* be assembled in the HTTP client.
|
||||
*
|
||||
* The client MAY do modifications to the Request before sending it. Because PSR-7 objects are
|
||||
* immutable, one cannot assume that the object passed to ClientInterface::sendRequest() will be the same
|
||||
* object that is actually sent. For example, the Request object that is returned by an exception MAY
|
||||
* be a different object than the one passed to sendRequest, so comparison by reference (===) is not possible.
|
||||
*
|
||||
* {@link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message-meta.md#why-value-objects}
|
||||
*
|
||||
* @param RequestInterface $request
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*
|
||||
* @throws \Psr\Http\Client\ClientException If an error happens while processing the request.
|
||||
*/
|
||||
public function sendRequest(RequestInterface $request) : ResponseInterface {
|
||||
if ($request->getMethod() === 'GET' || $request->getMethod() === 'OPTIONS') {
|
||||
$ncRes = $this->nextcloudClient->{strtolower($request->getMethod())}($request->getUri());
|
||||
$res = new Response();
|
||||
|
||||
foreach ($ncRes->getHeaders() as $key => $value) {
|
||||
$res = $res->withHeader($key, $value);
|
||||
}
|
||||
|
||||
return $res
|
||||
->withStatus($ncRes->getStatusCode())
|
||||
->withBody(Psr7\stream_for($ncRes->getBody()));
|
||||
} else {
|
||||
throw new \Exception('Can only send GET or OPTIONS requests'); // XXX: How should Streams be sent using nextcloud?
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\Bookmarks\Controller\Lib\Http;
|
||||
|
||||
use Psr\Http\Message\RequestFactoryInterface;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
|
||||
class RequestFactory implements RequestFactoryInterface {
|
||||
/**
|
||||
* Create a new request.
|
||||
*
|
||||
* @param string $method The HTTP method associated with the request.
|
||||
* @param UriInterface|string $uri The URI associated with the request.
|
||||
* @return RequestInterface
|
||||
*/
|
||||
public function createRequest(string $method, $uri) : RequestInterface {
|
||||
$req = new Request($method, $uri);
|
||||
return $req;
|
||||
}
|
||||
}
|
|
@ -3,9 +3,19 @@ namespace OCA\Bookmarks\Controller\Lib;
|
|||
|
||||
use Marcelklehr\LinkPreview\Client as LinkPreview;
|
||||
use Marcelklehr\LinkPreview\Exceptions\ConnectionErrorException;
|
||||
use OCP\Http\Client\IClientService;
|
||||
use OCA\Bookmarks\Controller\Lib\Http\RequestFactory;
|
||||
use OCA\Bookmarks\Controller\Lib\Http\Client;
|
||||
use phpUri;
|
||||
|
||||
class LinkExplorer {
|
||||
private $linkPreview;
|
||||
|
||||
public function __construct(IClientService $clientService) {
|
||||
$client = $clientService->newClient();
|
||||
$this->linkPreview = new LinkPreview(new Client($client), new RequestFactory($client));
|
||||
$this->linkPreview->getParser('general')->setMinimumImageDimensions(150, 550);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Load Url and receive Metadata (Title)
|
||||
|
@ -16,27 +26,37 @@ class LinkExplorer {
|
|||
$data = ['url' => $url];
|
||||
|
||||
// Use LinkPreview to get the meta data
|
||||
$previewClient = new LinkPreview($url);
|
||||
$previewClient->getParser('general')->setMinimumImageDimension(200, 200);
|
||||
try {
|
||||
libxml_use_internal_errors(false);
|
||||
$preview = $previewClient->getPreview('general');
|
||||
$preview = $this->linkPreview->getLink($url)->getPreview();
|
||||
} catch (\Marcelklehr\LinkPreview\Exceptions\ConnectionErrorException $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::WARN);
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::DEBUG);
|
||||
return $data;
|
||||
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::WARN);
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::DEBUG);
|
||||
return $data;
|
||||
}
|
||||
|
||||
$data = $preview->toArray();
|
||||
|
||||
\OCP\Util::writeLog('bookmarks', 'getImage for URL: '.$url.' '.var_export($data, true), \OCP\Util::DEBUG);
|
||||
|
||||
if (!isset($data)) {
|
||||
return ['url' => $url];
|
||||
}
|
||||
|
||||
$data['url'] = (string) $previewClient->getUrl();
|
||||
$data['image'] = phpUri::parse($data['url'])->join($data['cover']);
|
||||
$data['favicon'] = phpUri::parse($data['url'])->join($data['favicon']);
|
||||
$data['url'] = (string) $preview->getUrl();
|
||||
if (isset($data['image'])) {
|
||||
if (isset($data['image']['small'])) {
|
||||
$data['image']['small'] = phpUri::parse($data['url'])->join($data['image']['small']);
|
||||
}
|
||||
if (isset($data['image']['large'])) {
|
||||
$data['image']['large'] = phpUri::parse($data['url'])->join($data['image']['large']);
|
||||
}
|
||||
if (isset($data['image']['favicon'])) {
|
||||
$data['image']['favicon'] = phpUri::parse($data['url'])->join($data['image']['favicon']);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
|
|
@ -21,8 +21,7 @@ namespace OCA\Bookmarks\Controller\Lib\Previews;
|
|||
|
||||
use OCP\ICache;
|
||||
use OCP\ICacheFactory;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use OCP\Http\Client\IClientService;
|
||||
use OCA\Bookmarks\Controller\Lib\LinkExplorer;
|
||||
|
||||
class DefaultPreviewService implements IPreviewService {
|
||||
|
@ -41,9 +40,10 @@ class DefaultPreviewService implements IPreviewService {
|
|||
* @param ICacheFactory $cacheFactory
|
||||
* @param LinkExplorer $linkExplorer
|
||||
*/
|
||||
public function __construct(ICacheFactory $cacheFactory, LinkExplorer $linkExplorer) {
|
||||
public function __construct(ICacheFactory $cacheFactory, LinkExplorer $linkExplorer, IClientService $clientService) {
|
||||
$this->cache = $cacheFactory->create('bookmarks.DefaultPreviewService');
|
||||
$this->linkExplorer = $linkExplorer;
|
||||
$this->client = $clientService->newClient();
|
||||
}
|
||||
|
||||
private function buildKey($url) {
|
||||
|
@ -59,10 +59,14 @@ class DefaultPreviewService implements IPreviewService {
|
|||
return null;
|
||||
}
|
||||
$site = $this->scrapeUrl($bookmark['url']);
|
||||
if (!isset($site['image'])) {
|
||||
return null;
|
||||
\OCP\Util::writeLog('bookmarks', 'getImage for URL: '.$bookmark['url'].' '.var_export($site, true), \OCP\Util::DEBUG);
|
||||
if (isset($site['image']['small'])) {
|
||||
return $this->getOrFetchImageUrl($site['image']['small']);
|
||||
}
|
||||
return $this->getOrFetchImageUrl($site['image']);
|
||||
if (isset($site['image']['large'])) {
|
||||
return $this->getOrFetchImageUrl($site['image']['large']);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function scrapeUrl($url) {
|
||||
|
@ -93,14 +97,8 @@ class DefaultPreviewService implements IPreviewService {
|
|||
];
|
||||
}
|
||||
|
||||
try {
|
||||
// Fetch image from remote server
|
||||
$image = $this->fetchImage($url);
|
||||
} catch (\Exception $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::WARN);
|
||||
// TODO: We could return an error image here
|
||||
return null;
|
||||
}
|
||||
// Fetch image from remote server
|
||||
$image = $this->fetchImage($url);
|
||||
|
||||
if (is_null($image)) {
|
||||
$json = json_encode(null);
|
||||
|
@ -125,27 +123,21 @@ class DefaultPreviewService implements IPreviewService {
|
|||
private function fetchImage($url) {
|
||||
$body = $contentType = '';
|
||||
try {
|
||||
$client = new \GuzzleHTTP\Client();
|
||||
$request = $client->get($url, ['timeout' => self::HTTP_TIMEOUT]);
|
||||
$body = $request->getBody();
|
||||
$contentType = $request->getHeader('Content-Type');
|
||||
} catch (\GuzzleHttp\Exception\RequestException $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::WARN);
|
||||
if ($e->hasResponse()) {
|
||||
if ($e->getResponse()->getStatusCode() === 404) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
throw $e;
|
||||
$response = $this->client->get($url, ['timeout' => self::HTTP_TIMEOUT]);
|
||||
} catch (\Exception $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::DEBUG);
|
||||
return null;
|
||||
}
|
||||
$body = $response->getBody();
|
||||
$contentType = $response->getHeader('Content-Type');
|
||||
|
||||
// Some HTPP Error occured :/
|
||||
if (200 != $request->getStatusCode()) {
|
||||
if (200 != $response->getStatusCode()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// It's not actually an image, doh.
|
||||
if (!$contentType || stripos($contentType, 'image') !== 0) {
|
||||
if (!isset($contentType) || stripos($contentType, 'image') !== 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,8 +22,7 @@ namespace OCA\Bookmarks\Controller\Lib\Previews;
|
|||
|
||||
use OCP\ICache;
|
||||
use OCP\ICacheFactory;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use OCP\Http\Client\IClientService;
|
||||
use OCA\Bookmarks\Controller\Lib\LinkExplorer;
|
||||
|
||||
class FaviconPreviewService extends DefaultPreviewService {
|
||||
|
@ -32,8 +31,8 @@ class FaviconPreviewService extends DefaultPreviewService {
|
|||
* @param ICacheFactory $cacheFactory
|
||||
* @param LinkExplorer $linkExplorer
|
||||
*/
|
||||
public function __construct(ICacheFactory $cacheFactory, LinkExplorer $linkExplorer) {
|
||||
parent::__construct($cacheFactory, $linkExplorer);
|
||||
public function __construct(ICacheFactory $cacheFactory, LinkExplorer $linkExplorer, IClientService $clientService) {
|
||||
parent::__construct($cacheFactory, $linkExplorer, $clientService);
|
||||
$this->cache = $cacheFactory->create('bookmarks.FaviconPreviewService');
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,9 @@
|
|||
*/
|
||||
namespace OCA\Bookmarks\Controller\Lib\Previews;
|
||||
|
||||
use Wnx\ScreeenlyClient\Screenshot;
|
||||
use OCP\ICache;
|
||||
use OCP\IConfig;
|
||||
use OCP\Http\Client\IClientService;
|
||||
use OCP\ICacheFactory;
|
||||
|
||||
class ScreenlyPreviewService implements IPreviewService {
|
||||
|
@ -32,6 +32,8 @@ class ScreenlyPreviewService implements IPreviewService {
|
|||
|
||||
private $apiKey;
|
||||
|
||||
private $client;
|
||||
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
|
||||
|
@ -45,11 +47,12 @@ class ScreenlyPreviewService implements IPreviewService {
|
|||
/**
|
||||
* @param ICacheFactory $cacheFactory
|
||||
*/
|
||||
public function __construct(ICacheFactory $cacheFactory, IConfig $config) {
|
||||
public function __construct(ICacheFactory $cacheFactory, IConfig $config, IClientService $clientService) {
|
||||
$this->config = $config;
|
||||
$this->apiUrl = $config->getAppValue('bookmarks', 'previews.screenly.url', 'http://screeenly.com/api/v1/fullsize');
|
||||
$this->apiKey = $config->getAppValue('bookmarks', 'previews.screenly.token', '');
|
||||
$this->cache = $cacheFactory->create('bookmarks.ScreenlyPreviewService');
|
||||
$this->client = $clientService->newClient();
|
||||
}
|
||||
|
||||
private function buildKey($url) {
|
||||
|
@ -82,14 +85,8 @@ class ScreenlyPreviewService implements IPreviewService {
|
|||
];
|
||||
}
|
||||
|
||||
try {
|
||||
// Fetch image from remote server
|
||||
$image = $this->fetchScreenshot($url);
|
||||
} catch (\Exception $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::WARN);
|
||||
// TODO: We could return an error image here
|
||||
return null;
|
||||
}
|
||||
// Fetch image from remote server
|
||||
$image = $this->fetchScreenshot($url);
|
||||
|
||||
if (is_null($image)) {
|
||||
$json = json_encode(null);
|
||||
|
@ -109,8 +106,7 @@ class ScreenlyPreviewService implements IPreviewService {
|
|||
|
||||
public function fetchScreenshot($url) {
|
||||
try {
|
||||
$client = new \GuzzleHTTP\Client();
|
||||
$request = $client->post($this->apiUrl, ['body' => [
|
||||
$response = $this->client->post($this->apiUrl, ['body' => [
|
||||
'key' => $this->apiKey,
|
||||
'url' => $url,
|
||||
'width' => $this->width,
|
||||
|
@ -118,19 +114,14 @@ class ScreenlyPreviewService implements IPreviewService {
|
|||
],
|
||||
'timeout' => self::HTTP_TIMEOUT
|
||||
]);
|
||||
$body = $request->json();
|
||||
} catch (\GuzzleHttp\Exception\RequestException $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::WARN);
|
||||
if ($e->hasResponse()) {
|
||||
if ($e->getResponse()->getStatusCode() === 404) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
throw $e;
|
||||
$body = json_decode($response->getBody(), true);
|
||||
} catch (\Exception $e) {
|
||||
\OCP\Util::writeLog('bookmarks', $e, \OCP\Util::DEBUG);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Some HTPP Error occured :/
|
||||
if (200 != $request->getStatusCode()) {
|
||||
if (200 != $response->getStatusCode()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue